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 PyPy3 (7.3.0) (judge verdict: Accepted)

import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())


K = I()

B = []

#(i, j): iから始まるj桁のルンルン数の個数
lun = {(0, 1): 1, (1, 1): 1, (2, 1): 1, (3, 1): 1, (4, 1): 1, (5, 1): 1, (6, 1): 1, (7, 1): 1, (8, 1): 1, (9, 1): 1}
for j in range(2, 11):
    for i in range(10):
        if i == 0:
            lun[(i, j)] = lun[(i, j - 1)] + lun[(i + 1, j - 1)]
        elif i == 9 :
            lun[(i, j)] = lun[(i - 1, j - 1)] + lun[(i, j - 1)]
        else:
            lun[(i, j)] = lun[(i - 1, j - 1)] + lun[(i, j - 1)] + lun[(i + 1, j - 1)]

cnt = 0
for j in range(1, 10):
    for i in range(1, 10):
        cnt += lun[(i, j)]
    B.append(cnt)

b = bisect.bisect_right(B, K)

ans = []
if b == 0:
    print(K)
    exit()
if K == B[b - 1]:
    print(pow(10, b) - 1)
    exit()
else:
    K -= B[b - 1]
    i = 1
    while K > 0:
        K -= lun[(i, b + 1)]
        i += 1
    if K == 0:
        for j in range(b + 1):
            c = i - 1 + j
            if c <= 9:
                ans.append(c)
            else:
                ans.append(9)
    else:
        K += lun[(i - 1, b + 1)]
        ans.append(i - 1)
        for l in range(b):
            if ans[-1] == 0:
                k = 0
            else:
                k = ans[-1] - 1
            while K > 0:
                K -= lun[(k, b - l)]
                k += 1
            if K == 0:
                for j in range(b - l):
                    c = k - 1 + j
                    if c <= 9:
                        ans.append(c)
                    else:
                        ans.append(9)
                break
            else:
                K += lun[(k - 1, b - l)]
                ans.append(k - 1)
print(''.join(map(str, ans)))