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 Ruby (2.7.1) (judge verdict: Accepted)

K=gets.to_i

MEMO={}
COUNT={}
L_MAX={}
def count(k, n)
    return 1 if k == 0
    key = "#{k}_#{n}"
    return COUNT[key] if COUNT[key]

    ret = 0
    ret += count(k-1, n+1) if n != 9
    ret += count(k-1, n)
    ret += count(k-1, n-1) if n != 0
    COUNT[key] = ret
    return ret
end

def lunlun_max(k,n)
    return n if k == 0
    key = "#{k}_#{n}"
    return L_MAX[key] if L_MAX[key]

    base = n*10**k
    ret = nil
    if n == 9
        ret = base + lunlun_max(k-1,9)
    else
        ret = base + lunlun_max(k-1,n+1)
    end
    L_MAX[k] = ret
    return ret
end

MEMO[0] = 0
max_i = 0
max_pre_i = 0
break_flg = false
k=0
loop do
    (1..9).each do |i|
        c = count(k,i)
        max_i = lunlun_max(k,i)
        ret = MEMO[max_pre_i] + count(k,i)
        MEMO[max_i] = ret
        max_pre_i = max_i

        if ret >= K
            break_flg = true
            break
        end
    end
    break if break_flg
    k+=1
end

sup = max_i
inf = max_pre_i
rets = Array.new(k+1, nil)
rets[k] = max_i.to_s[0].to_i
k -= 1
memo = MEMO[sup]
loop do
    break if k < 0
    if rets[k+1] != 9
        c = count(k, rets[k+1]+1)
        if memo -c < K
            rets[k] = rets[k+1]+1
            k-=1
            next
        end
        memo -= c
    end

    c = count(k, rets[k+1])
    if memo -c < K
        rets[k] = rets[k+1]
        k-=1
        next
    end
    memo -= c

    c = count(k, rets[k+1])
    if rets[k+1] != 0
        c = count(k, rets[k+1]-1)
        rets[k] = rets[k+1]-1
        k-=1
        next
    end
end

ret = 0
rets.reverse.each do |i|
    ret *= 10
    ret += i
end
puts ret