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 PHP7 (7.0.15) (judge verdict: Wrong Answer)

<?php
 
function input($func = null)
{
    if ($func === null) {
        return trim(fgets(STDIN));
    } else {
        return $func(trim(fgets(STDIN)));
    }
}

function input_array($func = null)
{
    if ($func === null) {
        return explode(' ', input());
    } else {
        return array_map(function ($v) use ($func) {
            return $func($v);
        }, explode(' ', input()));
    }
}

define('NEXT_DIGITS', [
    0 => [1, 2, 3, 4, 5, 6, 7, 8, 9],
    1 => [0, 1, 2],
    2 => [1, 2, 3],
    3 => [2, 3, 4],
    4 => [3, 4, 5],
    5 => [4, 5, 6],
    6 => [5, 6, 7],
    7 => [6, 7, 8],
    8 => [7, 8, 9],
    9 => [8, 9],
]);

define('START_NUMBERS', [
    '1',
    '2',
    '3',
    '4',
    '5',
    '6',
    '7',
    '8',
    '9',
    '10',
    '21',
    '32',
    '43',
    '54',
    '65',
    '76',
    '87',
    '98',
    '100',
    '210',
    '321',
    '432',
    '543',
    '654',
    '765',
    '876',
    '987',
    '1000',
    '2100',
    '3210',
    '4321',
    '5432',
    '6543',
    '7654',
    '8765',
    '9876',
    '10000',
    '21000',
    '32100',
    '43210',
    '54321',
    '65432',
    '76543',
    '87654',
    '98765',
    '100000',
    '210000',
    '321000',
    '432100',
    '543210',
    '654321',
    '765432',
    '876543',
    '987654',
    '1000000',
    '2100000',
    '3210000',
    '4321000',
    '5432100',
    '6543210',
    '7654321',
    '8765432',
    '9876543',
    '10000000',
    '21000000',
    '32100000',
    '43210000',
    '54321000',
    '65432100',
    '76543210',
    '87654321',
    '98765432',
    '100000000',
    '210000000',
    '321000000',
    '432100000',
    '543210000',
    '654321000',
    '765432100',
    '876543210',
    '987654321',
    '1000000000',
    '2100000000',
    '3210000000',
    '4321000000',
    '5432100000',
    '6543210000',
    '7654321000',
    '8765432100',
    '9876543210',
]);

$k = input('intval');

$current_start_number_index = 0;
$current_lunlun_number = [1];

for ($lunlun_number_count = 1; ; $lunlun_number_count++) {
    if ($lunlun_number_count === $k) {
        echo implode('', $current_lunlun_number) . "\n";
        break;
    }
    
    $could_not_increment = true;
    
    for ($j = count($current_lunlun_number) - 1; $j > 0; $j--) {
        $candidate_digits = NEXT_DIGITS[$current_lunlun_number[$j - 1]];
        if (max($candidate_digits) === $current_lunlun_number[$j]) {
            continue;
        }
        $could_not_increment = false;
        $current_lunlun_number[$j]++;
        for ($i = $j + 1; $i < count($current_lunlun_number); $i++) {
            if ($current_lunlun_number[$i - 1] > 0) {
                $current_lunlun_number[$i] = $current_lunlun_number[$i - 1] - 1;
            } else {
                $current_lunlun_number[$i] = 0;
            }
        }
        break;
    }
    
    if ($could_not_increment === true) {
        $current_start_number_index++;
        $current_lunlun_number = array_map('intval', str_split(START_NUMBERS[$current_start_number_index]));
        continue;
    }
}