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: Time Limit Exceeded)

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<long long,long long>
#define mod 1000000007
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#pragma warn -rvl /* return value */ 
#pragma warn -par /* parameter never used */ 
#pragma warn -rch /*unreachable code */ 

ll power(ll a,ll b){
	ll ans=1;
	while(b>0){
		if(b%2!=0){
			ans=(ans*a)%mod;
		}
		a=(a*a)%mod;
		b>>=1;
	}
	return ans;
}

int main() {
	IOS;
	int t=1;
	//cin>>t;
	while(t--){
		int k;
		cin>>k;
		int cnt=0;
		if(k<=32660)
		{
			cnt=0;
			for(ll i=1;i<=234567899;i++){
				ll ha=i;
				int o=1;
				int last=ha%10;
				ha/=10;
				while(ha){
					int p=ha%10;
					if(abs(p-last)>1){
						o=0;
						break;
					}
					last=p;
					ha/=10;
				}
				cnt+=o;
				if(cnt==k){
					cout<<i;
					return 0;
				}
			}
			return 0;
		}
		
		if(k<=51241)
		{
			cnt=32660;
			for(ll i=321000000;i<=567899999;i++){
				ll ha=i;
				int o=1;
				int last=ha%10;
				ha/=10;
				while(ha){
					int p=ha%10;
					if(abs(p-last)>1){
						o=0;
						break;
					}
					last=p;
					ha/=10;
				}
				cnt+=o;
				if(cnt==k){
					cout<<i;
					return 0;
				}
			}
			return 0;
		}
		if(k<=66345)
		{
			cnt=51241;
			for(ll i=654321000;i<900000000;i++){
				ll ha=i;
				int o=1;
				int last=ha%10;
				ha/=10;
				while(ha){
					int p=ha%10;
					if(abs(p-last)>1){
						o=0;
						break;
					}
					last=p;
					ha/=10;
				}
				cnt+=o;
				if(cnt==k){
					cout<<i;
					return 0;
				}
			}
			return 0;
		}
		
		
		if(k<=79724)
		{
			cnt=66345;
			
			for(ll i=987654321;i<=1234567900;i++){
				ll ha=i;
				int o=1;
				int last=ha%10;
				ha/=10;
				while(ha){
					int p=ha%10;
					if(abs(p-last)>1){
						o=0;
						break;
					}
					last=p;
					ha/=10;
				}
				cnt+=o;
				if(cnt==k){
					cout<<i;
					return 0;
				}
			}
			
			return 0;
		}
		
		
		
		
		if(k<=94828)
		{
			cnt=79724;
			if(k<=83647)
			{
				cnt=79724;
				for(ll i=2100000000;i<=2123456789;i++){
					ll ha=i;
					int o=1;
					int last=ha%10;
					ha/=10;
					while(ha){
						int p=ha%10;
						if(abs(p-last)>1){
							o=0;
							break;
						}
						last=p;
						ha/=10;
					}
					cnt+=o;
					if(cnt==k){
						cout<<i;
						return 0;
					}
				}
			}
			else if(k<=88857){
				cnt=83647;
				for(ll i=2210000000;i<=2234567899;i++){
					ll ha=i;
					int o=1;
					int last=ha%10;
					ha/=10;
					while(ha){
						int p=ha%10;
						if(abs(p-last)>1){
							o=0;
							break;
						}
						last=p;
						ha/=10;
					}
					cnt+=o;
					if(cnt==k){
						cout<<i;
						return 0;
					}
				}
			}
			else{
				cnt=88857;
				for(ll i=2321000000;i<=2345678999;i++){
					ll ha=i;
					int o=1;
					int last=ha%10;
					ha/=10;
					while(ha){
						int p=ha%10;
						if(abs(p-last)>1){
							o=0;
							break;
						}
						last=p;
						ha/=10;
					}
					cnt+=o;
					if(cnt==k){
						cout<<i;
						return 0;
					}
				}
			}
			return 0;
		}
		if(k>1)
		{
			cnt=94828;
			for(ll i=3210000000;i<=3456789999;i++){
				ll ha=i;
				int o=1;
				int last=ha%10;
				ha/=10;
				while(ha){
					int p=ha%10;
					if(abs(p-last)>1){
						o=0;
						break;
					}
					last=p;
					ha/=10;
				}
				cnt+=o;
				if(cnt==k){
					cout<<i;
					return 0;
				}
			}
			return 0;
		}
	}
	return 0;
}