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)


/*
  AUTHOR:SOURABH
  CREATED:03:04:20
*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define ordered_set tree<pll , null_type, less< pll >, rb_tree_tag, tree_order_statistics_node_update>
#define ll long long int
#define ld long double
#define fi first
#define se second
#define pb push_back
#define pob pop_back
#define W while
#define fn(i,x) for(i=0;i<x;i++)
#define fs(i,s,x) for(i=s;i<x;i++)
#define fr(i,x) for(i=x;i>=0;i--)
#define fit(it,s) for(it=s.begin();it!=s.end();it++)
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vii vector<int>
#define vll vector<ll>
#define mii map<int,int>
#define mll map<ll,ll>
#define mod 998244353
#define MAX 100005
#define M 32

using namespace std;
using namespace __gnu_pbds;

queue<ll> q;
vll v;
ll c,k;

void bfs()
{
    ll n,d;
    W(!q.empty())
    {
        n=q.front();
        q.pop();
        d=n%10;
        //cout<<n<<" "<<n<<endl;
        if(d==0)
        {
            v.pb(n*10);
            q.push(n*10);
            v.pb(n*10+1);
            q.push(n*10+1);
            c+=2;
        }
        else if(d==9)
        {
            v.pb(n*10+8);
            q.push(n*10+8);
            v.pb(n*10+9);
            q.push(n*10+9);
            c+=2;
        }
        else
        {
            v.pb(n*10+d-1);
            q.push(n*10+d-1);
            v.pb(n*10+d);
            q.push(n*10+d);
            v.pb(n*10+d+1);
            q.push(n*10+d+1);
            c+=3;
        }
        if(c>=k)
            return;
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

    ll t,i;
    t=1;
    //cin>>t;
    W(t--)
    {
        cin>>k;
        fs(i,1,10)
        {
            v.pb(i);
            q.push(i);
        }
        c=9;
        bfs();
        //sort(v.begin(),v.end());
        cout<<v[k-1];
    }
    return 0;
}