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 Python (3.4.3) (judge verdict: Wrong Answer)

#!/usr/bin/env python3

K = int(input())
limit_list = []
upper_limit = 9
limit_list.append(upper_limit)
t_dict = {i: 1 for i in range(10)}
# 2桁目の場合
s_dict = {k: 3 for k in range(1, 9)}
s_dict[9] = 2
s_dict[0] = 2
upper_limit = upper_limit + sum(s_dict.values()) - s_dict[0]
limit_list.append(upper_limit)
ww_dict = {1: t_dict, 2: s_dict}  # 2桁目

i = 3
max_limit = 10 ** 5
while upper_limit <= max_limit:
    # 3桁目
    w_dict = {
        k: ww_dict[i - 1][k - 1] + ww_dict[i - 1][k] + ww_dict[i - 1][k]
        for k in range(1, 9)
    }
    w_dict[0] = ww_dict[i - 1][0] + ww_dict[i - 1][1]
    w_dict[9] = ww_dict[i - 1][8] + ww_dict[i - 1][9]
    ww_dict[i] = w_dict
    upper_limit = limit_list[-1] + sum(w_dict.values()) - w_dict[0]
    limit_list.append(upper_limit)
    i += 1

# K = 208644

for i, l in enumerate(limit_list):
    if l >= K:
        keta = i + 1
        break

keta_list = list(range(1, keta + 1))

ans_list = []

# i-1桁目のMAX値
t = limit_list[keta - 2]

for i in keta_list[::-1]:
    # i桁目の数字を決めたい
    if i == keta_list[-1]:
        # iが最大桁の場合の候補の数字
        number_list = list(range(1, 10))
    else:
        if ans_list[-1] == 9:
            max_n = 9
        else:
            max_n = ans_list[-1] + 1

        min_n = 0 if ans_list[-1] == 0 else ans_list[-1] - 1

        number_list = list(range(min_n, max_n + 1))
    for n in number_list:
        if i == 1:
            w = t + 1
        else:
            w = t + ww_dict[i][n]
        if w >= K:
            ans_list.append(n)
            break
        # 超えていなかったら更新
        t = w

ans = "".join([str(i) for i in ans_list])
print(ans)