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 Java (OpenJDK 11.0.6) (judge verdict: Runtime Error)

import java.util.*;
import java.io.*;

public class Main {
    static StringTokenizer st;
    static BufferedReader br;  
    static PrintWriter out;
 
    // this should be DP Frog.
    // we aslo can use queue
    // Lunlun Number abcde
    // we can build from abcd

    public static void main(String[] args) throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
 
        // useQueue();
        useDP();
    }
    public static void useDP() throws IOException {
        int K = nextInt();
        
        long dp[] = new long[3*K+1]; //max
   
        for (int x=1; x<10; x++) {
            dp[x] = x;
        }

        int index = 10;
        for (int x=1; x<=K; x++) {
            long cur = dp[x];
            long mod = cur % 10;
            if(mod != 0){
                dp[index] = 10*cur+mod-1;
                index++;
                if(index>K){break;}
            }
            dp[index] = 10*cur+mod;
            index++;
            if(index>K){break;}
            if(mod != 9){
                dp[index] = 10*cur+mod+1;
                index++;
                if(index>K){break;}
            }
        }

        System.out.println(dp[K]); 
    }

    public static void useQueue() throws IOException {
        int K = nextInt();
        
        LinkedList<Long> queue = new LinkedList<Long>();

        for (int x=1; x<10; x++) {
            queue.add((long)x);
        }

        for (int x=1; x<K; x++) {
            long cur = queue.poll();
            long mod = cur % 10;
            if(mod != 0){
                queue.add(10*cur+mod-1);
            }
            queue.add(10*cur+mod);
            if(mod != 9){
                queue.add(10*cur+mod+1);
            }
        }

        System.out.println(queue.poll()); 
    }
 

    public static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }
    public static long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    public static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    static String next() throws IOException {
        while (st == null || !st.hasMoreTokens())
            st = new StringTokenizer(br.readLine().trim());
        return st.nextToken();
    }
}