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>
 
using namespace std;
 
template <class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";}
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')
    cerr<<*sdbg++;
  cerr<<"="<<h<<","; 
  _dbg(sdbg+1, a...);
}
 
template<class T> ostream & operator<<(ostream & os, vector<T> V){
  os<<"[";
  for(auto vv: V) os << vv <<",";
  return os << "]";
}
template<class L, class R> ostream & operator <<(ostream & os, pair<L,R> P){
  return os <<"("<<P.first <<","<<P.second <<")";
}
 
#ifdef DEBUG
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#define cerr if(0)cout
#endif
 
const int N = 100005;
 
int k, dp[N][15];
int tot[N];
 
int getDP(int i, int d) {
    if (d < 0 || d > 9) {
        return 0;
    }
    return dp[i][d];
}
 
int main() {
    
    scanf("%d", &k);
    
    for (int i = 1; i <= k; i++) {
        for (int d = 0; d < 10; d++) {
            dp[i][d] = min(2 * k, i == 1 ? 1 : getDP(i - 1, d - 1) + getDP(i - 1, d) + getDP(i - 1, d + 1));
            debug(i, d, dp[i][d]);
            tot[i] += dp[i][d];
        }
        tot[i] += tot[i - 1] - dp[i][0];
        tot[i] = min(tot[i], 2 * k);
        debug(i, tot[i]);
    }
    
    int start = 0;
    int sum = 0;
    for (int i = 1; i <= k; i++) {
        for (int d = 1; d < 10; d++) {
            sum += dp[i][d];
        }
        if (sum >= k) {
            start = i;
            break;
        }
    }
    debug(start);
    string number;
    int rem = k;
    dp[start][0] = tot[start - 1];
    int mini = 0;
    int maxi = 9;
    for (int i = start; i >= 1; i--) {
        for (int d = mini; d <= maxi; d++) {
            debug(i, d, dp[i][d], rem);
            if (dp[i][d] >= rem) {
                mini = max(0, d - 1);
                maxi = min(9, d + 1);
                if (number.empty() && d == 0) {
                    break;
                }
                number += (char)(d + '0');
                break;
            } else {
                rem -= dp[i][d];
            }
        }
    }
    
    if (number.empty()) number = "0";
    
    printf("%s\n", number.c_str());
    
    return 0;
}