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 Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

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

public class Main {

    private static FastReader in = new FastReader(System.in);
    private static PrintWriter out = new PrintWriter(System.out);

    private static final long MOD = (long) 1e9 + 7;

    public static void main(String[] args) {
        solve();
        out.flush();
    }

    private static void solve() {
        int k = in.nextInt();

        int cumulative = 0;
        int p = 0, d = 0;
        init:
        for (int place = 0; place < 11; place++) {
            count(place, 0);
            for (int digit = 1; digit < 10; digit++) {
                int toAdd = count(place, digit);
                cumulative += toAdd;
                if (cumulative >= k) {
                    cumulative -= toAdd;
                    p = place;
                    d = digit;
                    break init;
                }
            }
        }

        // out.println("Init place " + p + " digit " + d);

        long ans = 0;
        while (p > 0) {
            ans += Math.pow(10, p) * d;

            p--;
            int oldDigit = d;
            for (d = Math.max(0, oldDigit - 1); d < Math.min(10, oldDigit + 2); d++) {
                int toAdd = count(p, d);
                cumulative += toAdd;
                if (cumulative >= k) {
                    cumulative -= toAdd;
                    break;
                }
            }
        }

        ans += d;
        out.println(ans);
    }

    private static int[][] counts = new int[11][10];

    static {
        for (int i = 0; i < 10; i++) {
            counts[0][i] = 1;
        }
    }

    private static int count(int place, int digit) {
        if (place == 0) {
            return 1;
        }

        int ret = 0;
        if (digit != 0) {
            ret += counts[place - 1][digit - 1];
        }
        ret += counts[place - 1][digit];
        if (digit != 9) {
            ret += counts[place - 1][digit + 1];
        }

        counts[place][digit] = ret;
        // out.println("Place " + place + " digit " + digit + ", count " + ret);
        return ret;
    }

    // https://www.geeksforgeeks.org/fast-io-in-java-in-competitive-programming/
    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        FastReader(InputStream inputStream) {
            br = new BufferedReader(new
                    InputStreamReader(inputStream));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException  e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

        // https://qiita.com/masakinpo/items/5f77f2a879578a85a0bb
        public int[] nextIntArray(int n) {
            int[] a = new int[n];
            for (int i = 0; i < n; i++)
                a[i] = nextInt();
            return a;
        }

        public long[] nextLongArray(int n) {
            long[] a = new long[n];
            for (int i = 0; i < n; i++)
                a[i] = nextLong();
            return a;
        }
    }
}