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 % 500 == 0 {
            f = true;
        }
        if check(i) {
            if f {
                print!(",{}", i);
                f = false;
            }
            j+=1;
        } 
    }
}

fn main() {
    //precompute();
    //return;
    let k : isize = read();
    let p = vec![-1,33322,110111,343211,565566,788989,1122111,2221010,3223212,3445677,4454545,5455543,5677899,6678999,7766767,8787667,9987676,11110123,12123344,21010010,22111101,22343332,23345454,32221100,33223223,33454322,34454556,43234434,44321101,44544322,45544456,54323432,54554344,55554556,56555654,56778898,65566544,66566778,67654543,67878888,76667876,77677777,78767898,87665556,87898788,88999878,98876565,99998788,101121210,110122210,111233456,112332212,121221234,122234343,123322121,210011001,211211010,212223433,221112212,222211112,223221112,223444434,232234455,233322233,234323323,234554321,321221122,322233445,323321222,332110012,332334565,333344543,334345555,343210010,343433454,344434544,345443234,345665667,432234345,433322123,434323221,434545655,443333334,444334432,444556787,445565555,454344454,454567665,455567877,456654454,543221010,543444333,544444567,545445565,545676555,554455445,555455665,555678878,556765545,565544567,566545665,566776678,567778887,654432334,655433332,655656543,656656777,665444456,665667667,666676544,667677888,676554343,676777656,677787656,678878766,765544543,766545555,766776654,767778777,776654345,777656545,777888776,778988998,787789998,788888877,876545667,876776766,877778889,878877789,887766667,888778999,889987654,898987789,987667877,988766555,989877887,998878887,1000001223,1001123454,1011001001,1012101223,1012334455,1101110121,1110111211,1111223343,1112321234,1121211100,1122223444,1123233345,1123456665,1211122112,1212221011,1221101011,1222122221,1223210101,1223434322,1232232121,1233234321,1233456777,1234543345,2101000111,2110001123,2111123432,2112222333,2121111111,2122123455,2123212221,2123443334,2211012233,2212121101,2212345454,2221234556,2222333234,2223343212,2232210111,2233212333,2233443454,2234444544,2321110122,2322123343,2323212101,2323443222,2332233321,2333321011,2334322111,2334544545,2343332232,2344333322,2344555677,2345556667,3210112121,3211232101,3212322222,3221211223,3222232210,3223234332,3223456788,3232333322,3233343222,3234343456,3234566667];

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