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.3.3) (judge verdict: Accepted)

def c9(k); 9 < k ? 9 : k end
def f0(k); k < 0 ? 0 : k end

k = gets.to_i

c = 0
(1..9).each do |i|
  c += 1
  if k == c
    puts i
  end
end

(1..9).each do |i|
  ((i-1)..c9(i+1)).each do |j|
    c += 1
    if k == c 
      puts i * 10 + j 
    end
  end
end

(1..9).each do |i|
  ((i-1)..c9(i+1)).each do |j|
    (f0(j-1)..c9(j+1)).each do |x|
      c += 1
      if k == c
        puts i * 100 + j * 10 + x 
      end
    end
  end
end

(1..9).each do |i|
  ((i-1)..c9(i+1)).each do |j|
    (f0(j-1)..c9(j+1)).each do |x|
      (f0(x-1)..c9(x+1)).each do |l|
        c += 1
        if k == c
          puts i * 1000 + j * 100 + x * 10 + l 
        end
      end
    end
  end
end

(1..9).each do |i|
  (i-1..c9(i+1)).each do |j|
    (f0(j-1)..c9(j+1)).each do |x|
      (f0(x-1)..c9(x+1)).each do |l|
        (f0(l-1)..c9(l+1)).each do |m|
          c += 1
          if k == c
            puts i * 10000 + j * 1000 + x * 100 + l * 10  + m 
          end
        end
      end
    end
  end
end

(1..9).each do |i|
  (i-1..c9(i+1)).each do |j|
    (f0(j-1)..c9(j+1)).each do |x|
      (f0(x-1)..c9(x+1)).each do |l|
        (f0(l-1)..c9(l+1)).each do |m|
          (f0(m-1)..c9(m+1)).each do |n|
            c += 1
            if k == c
              puts i * 100000 + j * 10000 + x * 1000 + l * 100  + m * 10 + n 
            end
          end
        end
      end
    end
  end
end

(1..9).each do |i|
  (i-1..c9(i+1)).each do |j|
    (f0(j-1)..c9(j+1)).each do |x|
      (f0(x-1)..c9(x+1)).each do |l|
        (f0(l-1)..c9(l+1)).each do |m|
          (f0(m-1)..c9(m+1)).each do |n|
            (f0(n-1)..c9(n+1)).each do |o|
              c += 1
              if k == c
                puts i * 1000000 + j * 100000 + x * 10000 + l * 1000  + m * 100 + n * 10 + o
              end
            end
          end
        end
      end
    end
  end
end

(1..9).each do |i|
  (i-1..c9(i+1)).each do |j|
    (f0(j-1)..c9(j+1)).each do |x|
      (f0(x-1)..c9(x+1)).each do |l|
        (f0(l-1)..c9(l+1)).each do |m|
          (f0(m-1)..c9(m+1)).each do |n|
            (f0(n-1)..c9(n+1)).each do |o|
              (f0(o-1)..c9(o+1)).each do |q|
                c += 1
                if k == c
                  puts i * 10000000 + j * 1000000 + x * 100000 + l * 10000  + m * 1000 + n * 100 + o * 10 + q
                end
              end
            end
          end
        end
      end
    end
  end
end

(1..9).each do |i|
  ((i-1)..c9(i+1)).each do |j|
    (f0(j-1)..c9(j+1)).each do |x|
      (f0(x-1)..c9(x+1)).each do |l|
        (f0(l-1)..c9(l+1)).each do |m|
          (f0(m-1)..c9(m+1)).each do |n|
            (f0(n-1)..c9(n+1)).each do |o|
              (f0(o-1)..c9(o+1)).each do |q|
                (f0(q-1)..c9(q+1)).each do |r|
                  c += 1
                  if k == c
                    puts i * 100000000 + j * 10000000 + x * 1000000 + l * 100000  + m * 10000 + n * 1000 + o * 100 + q * 10 + r
                  end
                end
              end
            end
          end
        end
      end
    end
  end
end

(1..9).each do |i|
  (i-1..c9(i+1)).each do |j|
    (f0(j-1)..c9(j+1)).each do |x|
      (f0(x-1)..c9(x+1)).each do |l|
        (f0(l-1)..c9(l+1)).each do |m|
          (f0(m-1)..c9(m+1)).each do |n|
            (f0(n-1)..c9(n+1)).each do |o|
              (f0(o-1)..c9(o+1)).each do |q|
                (f0(q-1)..c9(q+1)).each do |r|
                  (f0(r-1)..c9(r+1)).each do |s|
                    c += 1
                    if k == c
                      puts i * 1000000000 + j * 100000000 + x * 10000000 + l * 1000000  + m * 100000 + n * 10000 + o * 1000 + q * 100 + r * 10 + s
                    end
                  end
                end
              end
            end
          end
        end
      end
    end
  end
end