Problem:
Score : 400 points

Problem Statement

A positive integer X is said to be a lunlun number if and only if the following condition is satisfied:

In the base ten representation of X (without leading zeros), for every pair of two adjacent digits, the absolute difference of those digits is at most 1.

For example, 1234, 1, and 334 are lunlun numbers, while none of 31415, 119, or 13579 is.

You are given a positive integer K. Find the K-th smallest lunlun number.

Constraints

1 \leq K \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

K

Output

Print the answer.

Sample Input 1

15

Sample Output 1

23

We will list the 15 smallest lunlun numbers in ascending order:

1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
21,
22,
23.

Thus, the answer is 23.

Sample Input 2

1

Sample Output 2

1

Sample Input 3

13

Sample Output 3

21

Sample Input 4

100000

Sample Output 4

3234566667

Note that the answer may not fit into the 32-bit signed integer type.

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
#define INF 1000000000
#define LINF 1000000000000000000
#define MOD 1000000007
#define mod 1000007
#define INF63 1061109567
#define INF127 9187201950435737471
#define UINF 18446744073709551615
#define F first
#define S second
#define ll long long
#define N 20
using namespace std;
ll k,dp[N][N],sum[N];
string s="";
int main(){
	ll i,j,x;
	cin>>k;
	memset(sum,0,sizeof(sum));
	memset(dp,0,sizeof(dp));
	for(i=0;i<20;i++)
	{
		if(i!=0)
		{
			dp[i][19]=sum[i-1];
			sum[i]=dp[i][19];
		}
		for(j=0;j<10;j++)
		{
			if(i==0)
			{
				dp[i][j]=1;
			}
			else
			{
				dp[i][j]=dp[i-1][j];
				if(j>0)
				{
					dp[i][j]+=dp[i-1][j-1];
				}
				if(j<9)
				{
					dp[i][j]+=dp[i-1][j+1];
				}
			}
			if(j!=0)
			{
				sum[i]+=dp[i][j];
			}
		}
		if(k<=sum[i])
		{
			break;
		}
	}
	x=i;
	for(i=0;i<20;i++)
	{
		dp[i][0]=0;
	}
	i=x;
	while(i>=0)
	{
		if(s=="")
		{
			for(j=0;j<10;j++)
			{
				if(k>dp[i][j]+dp[i][20-j-1])
				{
					k-=dp[i][j]+dp[i][20-j-1];
				}
				else
				{
					s+=(char)('0'+j);
					break;
				}
			}
		}
		else
		{
			x=s[s.size()-1]-'0';
			for(j=max((ll)0,x-1);j<=min((ll)9,x+1);j++)
			{
				if(k>dp[i][j]+dp[i][20-j-1])
				{
					k-=dp[i][j]+dp[i][20-j-1];
				}
				else
				{
					s+=(char)('0'+j);
					break;
				}
			}
		}
		i--;
	}
	while(s[0]=='0')
	{
		s.erase(s.begin());
	}
	cout<<s<<endl;
	return 0;
}