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: Accepted)

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <cstring>
#include <map>
#include <queue>
#include <cmath>
#include <complex> // complex<double> a(1.2 , 2.3);// real(): 1.2, imag()2.3
using namespace std;

#define MOD 1000000007
#define EPS 1e-10
#define ll long long
#define ld long double
#define FOR(i,a,b) for(ll i=(ll)a;i<(ll)b;i++)
#define rep(i,n) FOR(i,0,n)
#define pb push_back
#define mp make_pair
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define rmsame(a) sort(all(a)),a.erase(unique(all(a)), a.end())
#define rmvector(a,b) rep(i,a.size())rep(j,b.size())if(a[i]==b[j]){a.erase(a.begin()+i);i--;break;}
#define first_more_idx(ve, num) upper_bound(all(ve), num)-ve.begin() // idx = first_more_idx(ve,num); if(idx = ve.size()) , then not exist
#define first_lessequal_idx(ve, num) lower_bound(all(ve), num)-ve.begin()
#define pq_pair_tB priority_queue <pair<ll,ll>, vector<pair<ll,ll> > , greater<pair<ll,ll> > >
#define pq_pair_ts priority_queue <pair<ll,ll> > // only sort first param
template<typename X> bool exist(vector<X> vec, X item){return find(all(vec), item)!=vec.end();}
ll gcd(ll a, ll b){if(b==0)return a;return gcd(b,a%b);}
ll lcm(ll a, ll b){return a/gcd(a,b)*b;}
ll power(ll x, ll n){if(n == 0) return 1;return  (n%2)? x*power(x, n-1)%MOD : power(x*x%MOD, n/2);}
ll dx[]={0, 0, -1, 1};
ll dy[]={1, -1, 0, 0};// up,down,left right

vector<ll> ans;
vector<ll> num;

void plusOne(){
    ll c = 1;
    
    ll idx = num.size() - 1;
    
    while(c != 0){
        num[idx] ++;
        c = num[idx] / 10;
        num[idx] %= 10;
        idx --;
    }
}
bool chk(){
    ll idx = 0;
    rep(i, num.size()){
        if(num[i] != 0){
            idx = i;
            break;
        }
    }
    for(ll i = idx+1; i < num.size(); i++){
        if( abs(num[i] - num[i - 1]) > 1)  return false;
    }

    return true;
}

void addNum(){
    ll ret = 0;
    rep(i, num.size()){
        ret *= 10;
        ret += num[i];
    }
    ans.pb(ret);
}

void nxtNum(){
    ll idx = 0 ;
    rep(i, num.size()){
        if(num[i] != 0){
            idx = i;
            break;
        }
    }
    for(int i = idx; i < num.size() - 1; i++){
        if(abs(num[i] - num[i + 1]) > 1) {
            idx = i;
            break;
        }
    }
    
    // 12[3]5 -> 12[4]3
    if(num[idx] < num[idx + 1]){
        num[idx] ++;
    }
    // 12[3]5 ->12[4]3
    // 12[3]1 -> 12[3]2
    for(int i = idx + 1; i < num.size() ; i++){
        num[i] = max(0LL, num[i-1] - 1);
    }
}

void cal(){
    while(ans.size()!= 100000){
        if(chk()){
            addNum();
            plusOne();
        }else{
            nxtNum();
        }
    }
}


int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    rep(i, 11) num.pb(0); plusOne();
    
    ll K ;
    
    cin >> K;
    cal();

    cout << ans[K-1] << endl;
    //cout << fixed << setprecision(16) << ans << endl;
    return 0;
}