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: Time Limit Exceeded)

#[allow(unused_imports)]
use std::cmp::{max, min};
#[allow(unused_imports)]
use std::collections::{HashMap, HashSet};
#[allow(unused_imports)]
use std::io::*;
#[allow(unused_imports)]
use std::str::*;

#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

#[allow(dead_code)]
fn read<T: FromStr>() -> T {
    let cin = stdin();
    let cin = cin.lock();
    let s: String = cin
        .bytes() // Bytes
        .map(|c| c.expect("failed reading char") as char)
        .skip_while(|c| c.is_whitespace()) // c が whitespace である限り skip
        .take_while(|c| !c.is_whitespace()) // c が whitespace でない限り要素を返す
        .collect();
    s.parse().ok().expect("failed parsing")
}

fn check(mut x: isize) -> bool {
    let mut a = x % 10;
    loop {
        x /= 10;
        if x == 0 { break; }
        let b = x % 10;
        if b != a && b != a - 1 && b != a + 1 {
            return false;
        }
        a = b;
    }
    true
}

fn precompute() {
    let mut j = 1;
    let mut f = false;
    for i in 1..1123456789012 {
        if j == 100001 {
            break;
        }
        if j % 1000 == 0 {
            f = true;
        }
        if check(i) {
            if f {
                print!(",{}", i);
                f = false;
            }
            j+=1;
        } 
    }
}

fn main() {
    let k : isize = read();
    let p = vec![-1, 110111,565566,1122111,3223212,4454545,5677899,7766767,9987676,12123344,22111101,23345454,33223223,34454556,44321101,45544456,54554344,56555654,65566544,67654543,76667876,78767898,87898788,98876565,101121210,111233456,121221234,123322121,211211010,221112212,223221112,232234455,234323323,321221122,323321222,332334565,334345555,343433454,345443234,432234345,434323221,443333334,444556787,454344454,455567877,543221010,544444567,545676555,555455665,556765545,566545665,567778887,655433332,656656777,665667667,667677888,676777656,678878766,766545555,767778777,777656545,778988998,788888877,876776766,878877789,888778999,898987789,988766555,998878887,1001123454,1012101223,1101110121,1111223343,1121211100,1123233345,1211122112,1221101011,1223210101,1232232121,1233456777,2101000111,2111123432,2121111111,2123212221,2211012233,2212345454,2222333234,2232210111,2233443454,2321110122,2323212101,2332233321,2334322111,2343332232,2344555677,3210112121,3212322222,3222232210,3223456788,3233343222,3234566667];

    let mut j = 1;
    let h = k / 1000;
    let mut s = 1;
    if h != 0 {
       s = p[h as usize];
       j = h * 1000; 
    }
    for i in s..1123456789012 {
        if check(i) {
            if j == k {
                println!("{}", i);
                break;
            }
            j+=1;
        } 
    }
}