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 C# (Mono 4.6.2.0) (judge verdict: Runtime Error)

using System;
using System.Collections;
using System.Collections.Generic;

namespace 練習問題
{
    class Program {
        static void Main(string[] args) {
            debug Debug = new debug();
            
            List<long> lunlun = new List<long>();
            int k = 0;//
            if (true) {
                k = int.Parse(Console.ReadLine());;
            } else {
                k = 9;
            }
            long rem = k;
            long num = 0;
            long ruijou = 1;
            /*
            while (rem > 0){
                num++;
                bool isLunLunFlag = isLunLun(num);
                if (isLunLunFlag == true){
                    lunlun.Add(num);
                    rem--;
                }
            }
            bool test = isLunLun(k);
            */
            //Debug.Log(test);
            long result = LunLun(k);
            Console.WriteLine(result);
        }

        static bool isLunLun(long num){
            long halve = 1;
            long numLength = num.ToString().Length;
            bool result = true;
            string numstr = num.ToString();
            for (int i = 0; i < num.ToString().Length-1; i++)
            {
                long numSubSt = long.Parse(numstr.Substring(i,1));
                Console.WriteLine(numSubSt);

                long a = long.Parse(numstr.Substring(i+1,1));
                Console.WriteLine(a);

                if (a + 1 == numSubSt){
                    result = true;
                } else if (a - 1 == numSubSt){
                    result = true;
                } else if (a == numSubSt){
                    result = true;
                } else {
                    result = false;
                    break;
                }
            }
            return result;
        }

        static long LunLun(int k){
            debug Debug = new debug();
            List<long> List_old = new List<long>();
            for (long j = 1; j < 10; j++) {
                List_old.Add(j);
            }
            // Debug.Log(List_old);
            int list_old_length = List_old.Count;
            // Debug.Log(424);
            if (list_old_length >= k){
                return List_old[k - 1];
            }
            while(true) {
                List<long> List_new = new List<long>();
                // Debug.Log("while Start");
                for (int i = 0; i < List_old.Count; i++) {
                    // Debug.Log("For Start");
                    
                    string strNum = List_old[i].ToString();
                    string LastHitoketa = strNum.Substring(strNum.Length - 1,1);
                    if (LastHitoketa == "0") {
                        // Debug.Log(12);
                        List_new.Add(long.Parse(List_old[i].ToString() + LastHitoketa));
                        List_new.Add(long.Parse(List_old[i].ToString() + LastHitoketa) + 1);
                    } else if (LastHitoketa == "9") {
                        List_new.Add(long.Parse(List_old[i].ToString() + LastHitoketa) - 1);
                        List_new.Add(long.Parse(List_old[i].ToString() + LastHitoketa));
                        // Debug.Log(21);
                    } else {
                        // Debug.Log("else");
                        // Debug.Log(List_old[i].ToString());
                        // Debug.Log(LastHitoketa);
                        List_new.Add(long.Parse(List_old[i].ToString() + LastHitoketa) - 1);
                        List_new.Add(long.Parse(List_old[i].ToString() + LastHitoketa));
                        List_new.Add(long.Parse(List_old[i].ToString() + LastHitoketa) + 1);
                        // Debug.Log(123);
                    }
                    
                    // Debug.Log(List_old);
                    // Debug.Log(List_new);
                    //return 1;
                }
                // Debug.Log("test")
                //k -= List_old.Count + 1;
                // Debug.Log("====");
                // Debug.Log(List_old);
                // Debug.Log(List_new);
                // Debug.Log(list_old_length);
                // Debug.Log(418);
                if (list_old_length + List_old.Count - 1 >= k){
                    // Debug.Log(k);
                    // Debug.Log(List_old.Count);
                    return List_new[k - list_old_length - 1];
                }
                list_old_length += List_new.Count;
                List_old = new List<long>(List_new);

                //return 1;
                //break;
            }
            return 1;
            //  return List_old[k];
        }
/*
        static void bubble_sort(List<List<long>> a) {
            long count=0;
            for (long i = 0; i < a.Count; i++) {
                for (long j = 0; j < a.Count-i-1; j++) {
                    // if (a[j] == a[a.Count - 1]){
                    //     continue;
                    // }
                    count++;
                    if (a[j][1] > a[j+1][1]){
                        long localsave = 0;
                        localsave = a[j+1][1];
                        a[j+1][1] = a[j][1];
                        a[j][1] = localsave;
                        localsave = a[j][0];
                        a[j][0] = a[j+1][0];
                        a[j+1][0] = localsave;
                    }
                }
                //debug(a,"途中" + i);
            }
            // debug(a,"result");
            //Console.WriteLine("回数"+count);
            for (long i = 0; i < a.Count; i++)
            {
                Console.WriteLine(a[i][0]+" "+a[i][1]);
            }
        }
        /* 標準入力と標準出力が使えるかのチェック
        static void echo() {
            string echotest = Console.ReadLine();
            Console.WriteLine(echotest);
        }

        static void HelloWorld(){
            Console.WriteLine("Hello World!");
        }
        */
    }

    public class debug{
        public void Log(string str){
            Console.WriteLine(str);
        }
        public void Log(long str){
            Console.WriteLine(str);
        }
        public void Log(object obj){
            Console.WriteLine(obj);
        }

        public void Log(long[] arr){
            Console.WriteLine(string.Join(",", arr));
        }
        public void Log(string[] arr){
            Console.WriteLine(string.Join(",", arr));
        }
        public void Log(List<long> arr){
            string shuturyoku = "";
            for (int i = 0; i < arr.Count; i++) {
                shuturyoku += arr[i] + " ";
            }
            Console.WriteLine(shuturyoku.TrimEnd(' '));
        }
    }
}