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 <bits/stdc++.h>
#define pi acos(-1)
#define pb push_back
using namespace std;
typedef long long ll;
string text="abcdefghijklmnopqrstuvwxyz";
const int mx=1e5+7;
const ll m=1e9+7;
ll lazy[4*mx];
int a[mx];
ll tree[4*mx];
//void init(int node,int b,int e)
//{
//    if(b==e){
//        tree[node]=a[b];
//        return;
//    }
//    int l=node*2;
//    int r=node*2+1;
//    int m=(b+e)/2;
//    init(l,b,m);
//    init(r,m+1,e);
//    tree[node]=tree[l]*tree[r];
//}
//int query(int node,int b,int e,int i,int j)
//{
//    if(i>e || j<b)return 1;
//    if(b>=i && e<=j){
//        return tree[node];
//    }
//    int l=node*2;
//    int r=node*2+1;
//    int m=(b+e)/2;
//    int p1=query(l,b,m,i,j);
//    int p2=query(r,m+1,e,i,j);
//    return p1*p2;
//}
//void update(ll node, ll b, ll e, ll i, ll j, ll x)
//{
//    if (i > e || j < b)
//        return;
//    if (b >= i && e <= j)
//    {
//        tree[node].sum += ((e - b + 1) * x);
//        tree[node].prop += x;
//        return;
//    }
//    ll Left = node * 2;
//    ll Right = (node * 2) + 1;
//    ll mid = (b + e) / 2;
//    update(Left, b, mid, i, j, x);
//    update(Right, mid + 1, e, i, j, x);
//    tree[node].sum = (tree[Left].sum+ tree[Right].sum + ((e - b + 1) * tree[node].prop));
//}
//void update(int node, int b, int e, int i, int newvalue)
//{
//    if (i > e || i < b)
//        return;
//    if (b >= i && e <= i) {
//        tree[node] = newvalue;
//        return;
//    }
//    int l = node * 2;
//    int r = node * 2 + 1;
//    int m = (b + e) / 2;
//    update(l, b, m, i, newvalue);
//    update(r, m + 1, e, i, newvalue);
//    tree[node] = (tree[l] * tree[r]);
//}
//void update(int node,int b,int e,int l,int r,int val)
//{
//    if(lazy[node]!=0){
//        tree[node]+=(e-b+1)*lazy[node];
//        if(b!=e){
//            lazy[node*2]+=lazy[node];
//            lazy[node*2+1]+=lazy[node];
//        }
//        lazy[node]=0;
//    }
//    if(b>e || b>r || e<l)return;
//    if(b>=l && e<=r){
//        tree[node]+=(e-b+1)*val;
//        if(b!=e){
//            lazy[node*2]+=val;
//            lazy[node*2+1]+=val;
//        }
//        return;
//    }
//    int mid=(b+e)/2;
//    update(node*2,b,mid,l,r,val);
//    update(node*2+1,mid+1,e,l,r,val);
//    tree[node]=tree[node*2]+tree[node*2+1];
//}
//int query(ll node,ll b,ll e,ll l,ll r)
//{
//     if(b>e || b>r || e<l)return 0;
//     if(lazy[node]!=0){
//        tree[node]+=(e-b+1)*lazy[node];
//        if(b!=e){
//            lazy[node*2]+=lazy[node];
//            lazy[node*2+1]+=lazy[node];
//        }
//        lazy[node]=0;
//    }
//    if(b>=l && e<=r){
//        return tree[node];
//    }
//    ll mid=(b+e)/2;
//    ll p1=query(node*2,b,mid,l,r);
//    ll p2=query(node*2+1,mid+1,e,l,r);
//    return p1+p2;
//}
vector <ll> q;
int main()
{
    for(ll i=1;i<10;i++)q.push_back(i);
    ll n;
    cin>>n;
    ll last = 0;
    ll cnt=0;
    while(cnt<n){
        ll a=q[cnt];
        cnt++;
        last = a%10;
        if(last == 0){
            q.push_back(10*a);
            q.push_back(10*a + 1);
        }
        else if(last == 9){
            q.push_back(10*a + 8);
            q.push_back(10*a + 9);
        }
        else{
            q.push_back(10*a + last - 1);
            q.push_back(10*a + last);
            q.push_back(10*a + last + 1);
        }
    }
    cout<<q[n-1]<<endl;
}