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 Rust (1.15.1) (judge verdict: Runtime Error)

#![allow(unused)]

use std::io::{Read, StdinLock};
use std::iter::{FromIterator, Iterator};
use std::str::{FromStr};
use std::string::String;

macro_rules! debug {
    ($($a:expr),*) => {
	println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

pub fn read_bytes<T: FromIterator<char>>(s: &mut StdinLock) -> T {
    s.bytes()
        .map(|c| c.unwrap() as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect::<T>()
}

pub fn reads(s: &mut StdinLock) -> String {
    read_bytes::<String>(s)
}

pub fn read<T: FromStr>(s: &mut StdinLock) -> T {
    reads(s).parse().ok().unwrap()
}

//==============================================================================
//
// Problem specific code
//
//==============================================================================

fn reconstruct(
    dp: &Vec<Vec<usize>>, digit: usize, mut i: usize, mut c: usize
) -> usize {
    let mut ans: usize = 0;

    for d in (0..digit).map(|x| digit - x) {
        ans = ans * 10 + i;
        if d == 1 {
            assert!(c == 1); 
        } else {
            for x in (i - 1)..(i + 2) {
                if c <= dp[d - 1][x] {
                    i = x;
                    break;
                }
                c -= dp[d - 1][x];
            }
        }
    }
    ans
}

fn main() {
    let sin = std::io::stdin();
    let mut sin = sin.lock();

    let k: usize = read(&mut sin);

    if k <= 9 {
        println!("{}", k);
        return;
    }

    let mut dp: Vec<Vec<usize>> =
        vec![vec![], vec![1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0]];
    let mut c = 9;

    for d in 2.. {
        dp.push(vec![0; 11]);
        dp[d][0] = dp[d - 1][0] + dp[d - 1][1];
        for i in 1..10 {
            dp[d][i] = dp[d - 1][i - 1] + dp[d - 1][i] + dp[d - 1][i + 1];
            if c < k && k <= c + dp[d][i] {
                println!("{}", reconstruct(&dp, d, i, k - c));
                return;
            }
            c += dp[d][i];
        }
    }
}