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.



Sample input:
15


Sample output:
23

Submission status: Runtime Error

Solution code:
K = int(input())

def get_next_lun(before_number,keta):  

  if keta == 1: #終了条件を忘れない
      if before_number == 9:
        return [8,9]
      if before_number == 0:
        return [1,2]
      else:
        return [before_number-1,before_number,before_number+1]
  else:
      if before_number == 9:
        for i in get_next_lun(before_number,keta-1):
        return []
      if before_number == 0:
        return []
      else:
        return [before_number-1,before_number,before_number+1]
      
      return

Submission status: Runtime Error

Solution code:
K = int(input())
ls = [i for i in range(10)]
for i in range(1,10):
    s = str(i)
    for j in graph[i]:
        s += str(j)
        ls.append(int(s))
        
for i in range(1,10):
    s = str(i)
    for j in graph[i]:
        s += str(j)
        for k in graph[j]:
            s += str(k)
            ls.append(int(s))
        
for i in range(1,10):
    s = str(i)
    for j in graph[i]:
        s += str(j)
        for k in graph[j]:
            s += str(k)
            for l in graph[k]:
                s += str(l)
                ls.append(int(s))
                
for i in range(1,10):
    s = str(i)
    for j in graph[i]:
        s += str(j)
        for k in graph[j]:
            s += str(k)
            for l in graph[k]:
                s += str(l)
                for m in graph[l]:
                    s += str(m)
                    ls.append(int(s))
                    
for i in range(1,10):
    s = str(i)
    for j in graph[i]:
        s += str(j)
        for k in graph[j]:
            s += str(k)
            for l in graph[k]:
                s += str(l)
                for m in graph[l]:
                    s += str(m)
                    for n in graph[m]:
                        s += str(n)
                        ls.append(int(s))

for i in range(1,10):
    s = str(i)
    for j in graph[i]:
        s += str(j)
        for k in graph[j]:
            s += str(k)
            for l in graph[k]:
                s += str(l)
                for m in graph[l]:
                    s += str(m)
                    for n in graph[m]:
                        s += str(n)
                        for o in graph[n]:
                            s += str(o)
                            ls.append(int(s))
for i in range(1,10):
    s = str(i)
    for j in graph[i]:
        s += str(j)
        for k in graph[j]:
            s += str(k)
            for l in graph[k]:
                s += str(l)
                for m in graph[l]:
                    s += str(m)
                    for n in graph[m]:
                        s += str(n)
                        for o in graph[n]:
                            s += str(o)
                            for p in graph[o]:
                                s += str(p)
                                ls.append(int(s))
                        
print(ls[K-1])

Submission status: Runtime Error

Solution code:
from collections import deque
a=int(input())
q=deque([1,2,3,4,5,6,7,8,9])
def dfs(k):
  while k=a:
    s=q.popleft()
    if s-1>=0:
      q.aapend(s*10+s-1)
      n=n+1
    q.append(s*10+s)
    k=k+1
    if s+1<=9:
      q.append(s*10+s+1)
      k=k+1
  print(q.pop())
 
if a<=9:
  print(a)
else:
  dfs(9)

Submission status: Runtime Error

Solution code:
num = int(input())
count = 0
def check(x):
    f= True
    if(x < 10):
        return True
    x = str(x)
    for i in range(len(x)-1):
        if( int(x[i]) - int(x[i + 1]) != 0):
            f = False
        if( int(x[i]) - int(x[i + 1]) != 1 ):
            f = false
        if( int(x[i]) - int(x[i + 1]) != -1 ):
            f = false
        if(f == false):
            return false
    return True
i = 0    
while(True):
    i = i + 1
    if(check(i)):
    #    print(i)
        count = count + 1 
        if(count == num ):
            print(i)
            exit()

Submission status: Runtime Error

Solution code:
import sys

K = int(input())

NLUNLUN = [[-1 for _ in range(11)] for _ in range(10)]
def num_lunlun(n, digit):
    if digit == 1:
        NLUNLUN[n][digit] = 1
        return NLUNLUN[n][digit]

    if digit == 2:
        if 0 < n < 9:
            NLUNLUN[n][digit] = 3
        else:
            NLUNLUN[n][digit] = 2
        return NLUNLUN[n][digit]

    n2 = 0
    if n - 1 >= 0:
        n2 += NLUNLUN[n-1][digit-1]
    n2 += NLUNLUN[n][digit-1]
    if n + 1 <= 9:
        n2 += NLUNLUN[n+1][digit-1]
    NLUNLUN[n][digit] = n2
    return NLUNLUN[n][digit]


if K < 10:
    print(K)
    sys.exit(0)

k = 0
for digit in range(1, 11):
    for n in range(10):
        tmp = num_lunlun(n, digit)
        if n > 0:
            if k + tmp >= K:
                ans = str(n)
                n2 = n
                for digit2 in range(digit-1, 1, -1):
                    if n2 - 1 >= 0:
                        tmp = NLUNLUN[n2 - 1][digit2]
                        if k + tmp >= K:
                            ans += str(n2 - 1)
                            n2 -= 1
                            continue
                        k += tmp

                    tmp = NLUNLUN[n2][digit2]
                    if k + tmp >= K:
                        ans += str(n2)
                        continue
                    k += tmp

                    if n2 + 1 <= 9:
                        tmp = NLUNLUN[n2 + 1][digit2]
                        if k + tmp >= K:
                            ans += str(n2 + 1)
                            n2 += 1
                            continue
                        k += tmp

                ans = int(ans + str(n2 + K - k - 2))
                print(ans)

                sys.exit(0)
            k += tmp

Submission status: Runtime Error

Solution code:
def lun(s):
    ch = 1
    temp = int(s[0])
    for i in s[1: ]:
        i = int(i)
        if abs(i - temp) > 1:
            ch = 0
            break
        temp = i
    return ch
k = int(input())
d = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
l = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
while 1:
    for i in l:
        for j in d:
            # print(i, j)
            s = i + str(j)
            if lun(s): l.append(s)
        if len(l) >= 10000: break
    if len(l) >= 10000: break
k = input()
print(l[k])

Submission status: Runtime Error

Solution code:
N = int(input())

def asym(bdict):
    pdict = {}
    pdict[0] = bdict[1]
    pdict[1] = bdict[0] + bdict[1] + bdict[2]
    pdict[2] = bdict[1] + bdict[2] + bdict[3]
    pdict[3] = bdict[2] + bdict[3] + bdict[4]
    pdict[4] = bdict[3] + bdict[4] + bdict[5]
    pdict[5] = bdict[4] + bdict[5] + bdict[6]
    pdict[6] = bdict[5] + bdict[6] + bdict[7]
    pdict[7] = bdict[6] + bdict[7] + bdict[8]
    pdict[8] = bdict[7] + bdict[8] + bdict[9]
    pdict[9] = bdict[8] + bdict[9]
    return pdict
    
if N <= 9:
    print(N)
else:
    bdict = {0:0, 1:1, 2:1, 3:1, 4:1, 5:1, 6:1, 7:1, 8:1, 9:1}
    cnt = 0
    n = 1
    while True:
        values = list(bdict.values())
        pcnt = cnt + sum(values)
        if N <= pcnt:
            ind = N - cnt  #n桁のルンルン数のうちind番目の数を求めれば良い
            cnt2 = 0
            ind2 = 0
            while True:
                esd = sum(values[:cnt2+1])
                if esd >= ind:
                    ind2 = ind - sum(values[:cnt2]) #一の位がcnt2であり、かつ桁数がnであるもののうち、ind２番目の数を求めれば良い
                    break
                else:
                    cnt2 += 1
                    
            break
                    
        else:
            cnt += sum(values)
            bdict = asym(bdict)
            n += 1
        
    alist = [str(cnt2)]
    
    def bous(alist):
        if len(alist[0]) == n:
            return alist
        else:
            blist = []
            for num in alist:
                top = int(num[0])
                plu, minu = top+1, top-1
                if plu < 10:
                    blist.append(str(plu)+num)
                if minu >= 0:
                    blist.append(str(minu)+num)
                alist = blist
            return(bous(alist))
        
    anc = bous(alist)
    blist = []
    for num in anc:
        if num[0] != "0":
            blist.append(int(num))
            
    blist.sort()
    print(blist[ind2])

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
ans = [0] * 10
ans[1] = '1'
for i in range(2, 10):
    ans[i] = str(i)
alpha = "0123456789"
def dfs(s):
    if len(s) == N:
        ans.append(s)
        if len(ans) >= 100001:
            return
    else:
        for c in range(int(s[-1])-1,int(s[-1])+2):
            if c >= 0:
                dfs(s+str(c))
            if c == '9':
                break
K = int(input())
N = 2
while len(ans) < 100001:
    for i in range(1, 10):
        dfs(str(i))
        if len(ans) >= 100001:
            break
    N+=1

print(ans[K])

Submission status: Runtime Error

Solution code:
K = int(input())

from itertools import product
d = {}
num_list = []
for i in range(10):
    nums = set([min((i-1)%10, i), i, max((i+1)%10, i)])
#     nums = set([i, min(10-(i-1)%10, i),max((i+1)%10, i)])
    nums = [str(el) for el in nums]
    d[i] = list(product([str(i)], list(nums)))
    d[i] = [int(''.join(el)) for el in d[i]]
    d[i] = ['{:02}'.format(el) for el in d[i]]
    
    num_list.extend(d[i])
num_list = sorted(num_list)
dd = [None, None, d, 
      {i:[] for i in range(1,10)},
      {i:[] for i in range(1,10)},
      {i:[] for i in range(1,10)},
      {i:[] for i in range(1,10)},
      {i:[] for i in range(1,10)},
      {i:[] for i in range(1,10)},
      {i:[] for i in range(1,10)},
      {i:[] for i in range(1,10)},
      {i:[] for i in range(1,10)},
      {i:[] for i in range(1,10)},
      {i:[] for i in range(1,10)},
      {i:[] for i in range(1,10)},
      {i:[] for i in range(1,10)},
      {i:[] for i in range(1,10)},
      {i:[] for i in range(1,10)},
      {i:[] for i in range(1,10)},
      {i:[] for i in range(1,10)},
      {i:[] for i in range(1,10)},
      {i:[] for i in range(10)}]

def gen_2ll():
    count = 0
    ret = 0
    while int(ret)!=99:
        if int(ret)< 9 :
            ret += 1
            count += 1
            yield ret
        else:
            for i in range(1,10):
                for ret in d[i]:
                    count += 1
                    yield ret

ll2_list = list(gen_2ll())
                   
def get_digit_top_list(num_digit, top_num):
    ret = dd[num_digit][top_num]
    if ret:
        return ret

    second_nums = sorted(set([min((top_num-1)%10, top_num), top_num, max((top_num+1)%10, top_num)]))    
    for second_num in second_nums:
        rem_nums = get_digit_top_list(num_digit-1, int(second_num))
        for rem_num in rem_nums:
            ll = str(top_num) + rem_num
            ret.append(ll)
    dd[num_digit][top_num] = ret

    
    return ret

def get_ll(K):

def get_kth_ll(K):
    if K<=len(ll2_list):
        print(ll2_list[K-1])
    else:
        K -= len(ll2_list)
        for num_digit in range(3,100):
            for top_num in range(1,10):
                _ll_list = get_digit_top_list(num_digit, top_num)
                if K-len(_ll_list)<0:
                    print(sorted([int(el) for el in _ll_list])[K])
                else:
                    K -= len(_ll_list)
get_kth_ll(K)

Submission status: Runtime Error

Solution code:
import sys
from functools import lru_cache
sys.setrecursionlimit(10**9)
k = int(input())
lunlun = []


@lru_cache(maxsize=100000000)
def func(i):
    if int(i) > 3234566667 + 10 or len(i) > 6:
        return
    lunlun.append(int(i))
    f = int(i[0])
    b = int(i[-1])
    if f != 0:
        func(f'{f-1}{i}')
    if f != 9:
        func(f'{f+1}{i}')
    func(f'{f}{i}')
    if b != 0:
        func(f'{i}{b-1}')
    if b != 9:
        func(f'{i}{b+1}')
    func(f'i}{b}')


for i in range(10):
    func(str(i))
lunlun = sorted(set(lunlun))
print(len(lunlun))
print(lunlun[k])

Submission status: Runtime Error

Solution code:
k=int(input())

from collections import deque
g=deque((1,2,3,4,5,6,7,8,9))

b=[[str(k) for k in range(10) if -2<=k-i<3] for i in range(10)]

for i in range(10,1000000):
  i1=list(str(i))
  e1=len(i1)
  for h in range(e1-1):
    if not i1[h+1] in b[int(i1[h])]:
      break
  else:g.append(i)


print(g[k-1])

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
ans = [0] * 10
ans[1] = '1'
for i in range(2, 10):
    ans[i] = str(i)
alpha = "0123456789"
def dfs(s):
    if len(s) == N:
        ans.append(s)
        if len(ans) >= 100001:
            return
    else:
        for c in (int(s[-1])-1,int(s[-1])+2):
            if c >= 0:
                dfs(s+str(c))
            if c == '9':
                break
K = int(input())
N = 2
while len(ans) < 100001:
    for i in range(1, 10):
        dfs(str(i))
        if len(ans) >= 100001:
            break
    N+=1

print(ans[K])

Submission status: Runtime Error

Solution code:
#n桁のるんるん数のrisutoを求める
import sys

sys.setrecursionlimit(10**9)
def runrun(n):
    if n == 1:
        return [['1','2','3','4','5','6','7','8','9']]
    else:
        li = runrun(n-1)
        li2 = []
        for i in range(len(li[n-2])):
            p = int(li[n-2][i][-1])
            if p == 0:
                li2.append(li[n-2][i]+'0')
                li2.append(li[n-2][i]+'1')
            elif p == 9:
                li2.append(li[n-2][i]+str(8))
                li2.append(li[n-2][i]+'9')
            else:
                li2.append(li[n-2][i]+str(p-1))
                li2.append(li[n-2][i]+str(p))
                li2.append(li[n-2][i]+str(p+1))
        li2 =[li2]
        li.extend(li2)
        return li
    


x = int(input())
t = True
k=1
b = 0
while t:
    
    a = runrun(k)
    #k桁までに何個るんるん数があるか数えるもの
    for i in range(len(a)):
        b+=(len(a[i])-1)
        #k桁ということが確定したあと以下の処理を行う
    if x <= b:
        t = False
        if k == 1:
            print(x)
        else:
            tmp = runrun(k-1)
            c = 0
            for i in range(len(tmp)):
                c+=len(tmp[i])
            s = a[k-1][x-c-1]
            print(int(s))
    else:
        b += 1
        k += 1

Submission status: Runtime Error

Solution code:
K=int(input())
n=0
c=0
while 1:
  j=True
    if len(n_str) > 1 and abs(int(n_str[0]) - int(n_str[1])) > 1:
    n = (int(str(int(n_str[0])+1)+str(int(n_str[0]))))*(10**(len(n_str)-2))
  else:
    n+=1
  n_str=str(n)
  for i in range(len(n_str)-1):
    if abs(int(n_str[i]) - int(n_str[i+1])) > 1:
      j=False
  if j:
    c+=1
  if c==K:
    print(n)
    break

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3

k = int(input())
lnln = [ i for i in range(1, 13) ]

def next_lnln(n):
    n_list = list(map(int, str(n)))
    d = n_list[len(n_list)-1] - n_list[len(n_list)-2]
    if d == -1 or d == 0:
        n_list[len(n_list)-1] += 1
    elif d == 1:
        n_list[len(n_list)-1] = n_list[len(n_list)-2]
        n_list[len(n_list)-2] += 1
 

if k > 13:
    for _ in range(k-13):
        lnln.append(next_lnln(lnln[-1]))

print(lnln[k-1])

Submission status: Runtime Error

Solution code:
import copy
n,k,c = map(int,input().split())
dp = [0]*(n+c+2)
s = input()
idx = []
for i in range(n):
    if s[i]=='o':
        idx.append(i+1)
for i in idx:
    dp[i] += 1
    dp[i+c+1] -= 1
for i in range(1,len(dp)):
    dp[i] = dp[i]+dp[i-1]       

for i in idx:
    tmp = copy.deepcopy(dp)
    for j in range(i, i+c+1):
        tmp[j] -= 1
    cnt = 0
    flg = False
    for j in range(len(tmp[1:-1])):
        if tmp[1+j]!=0 and flg==False:
            flg = True
            cnt += 1
        if tmp[1+j]!=0 and flg:
            continue
        if tmp[1+j]==0:
            flg = False
    if cnt<k:
        print(i)

Submission status: Runtime Error

Solution code:
k = int(input())
table = [1,2,3,4,5,6,7,8,9,10,11,12,21,22,23,32,33,34,43,44,45,54,55,56,65,66,67,76,77,78,87,88,89,98]
key1, key2,key3 = 0,0,0
for i in range(1,9):
    key1=int(str(i)+str(i-1)+str(i-1))
    key2=int(str(i)+str(i)+str(i))
    key3=int(str(i)+str(i+1)+str(i+1))
    table.append(key1-1)
    table.append(key1)
    table.append(key1+1)
    table.append(key2-1)
    table.append(key2)
    table.append(key2+1)
    table.append(key3-1)
    table.append(key3)
    table.append(key3+1)
table.pop()
table.append(987)
table.append(988)
table.append(989)
table.append(998)
table.append(999)
table.append(1000)
table.append(1001)
table.append(1010)
table.append(1011)
table.append(1012)
table.append(1100)
table.append(1101)
table.append(1110)
table.append(1111)
table.append(1112)
table.append(1121)
table.append(1122)
table.append(1123)
table.append(1210)
table.append(1211)
table.append(1212)
table.append(1221)
table.append(1222)
table.append(1223)
table.append(1232)
table.append(1233)
table.append(1234)
for j in range(2,9):
        key1=int(str(j)+str(j-1)+str(j-1)+str(j-1))
        key2=int(str(j)+str(j)+str(j)+str(j))
        key3=int(str(j)+str(j+1)+str(j+1)+str(j+1))    
    table.append(key1-1)
    table.append(key1)
    table.append(key1+1)
    table.append(key2-1)
    table.append(key2)
    table.append(key2+1)
    table.append(key3-1)
    table.append(key3)
    table.append(key3+1)
table.pop()
table.append(9887)
table.append(9888)
table.append(9889)
table.append(9998)
table.append(9999)
print(table[k-1])

Submission status: Runtime Error

Solution code:
K = int(input)
ls = [i for i in range(10)]
for i in range(1,10):
    s = str(i)
    for j in graph[i]:
        s += str(j)
        ls.append(int(s))
        
for i in range(1,10):
    s = str(i)
    for j in graph[i]:
        s += str(j)
        for k in graph[j]:
            s += str(k)
            ls.append(int(s))
        
for i in range(1,10):
    s = str(i)
    for j in graph[i]:
        s += str(j)
        for k in graph[j]:
            s += str(k)
            for l in graph[k]:
                s += str(l)
                ls.append(int(s))
                
for i in range(1,10):
    s = str(i)
    for j in graph[i]:
        s += str(j)
        for k in graph[j]:
            s += str(k)
            for l in graph[k]:
                s += str(l)
                for m in graph[l]:
                    s += str(m)
                    ls.append(int(s))
                    
for i in range(1,10):
    s = str(i)
    for j in graph[i]:
        s += str(j)
        for k in graph[j]:
            s += str(k)
            for l in graph[k]:
                s += str(l)
                for m in graph[l]:
                    s += str(m)
                    for n in graph[m]:
                        s += str(n)
                        ls.append(int(s))

for i in range(1,10):
    s = str(i)
    for j in graph[i]:
        s += str(j)
        for k in graph[j]:
            s += str(k)
            for l in graph[k]:
                s += str(l)
                for m in graph[l]:
                    s += str(m)
                    for n in graph[m]:
                        s += str(n)
                        for o in graph[n]:
                            s += str(o)
                            ls.append(int(s))
for i in range(1,10):
    s = str(i)
    for j in graph[i]:
        s += str(j)
        for k in graph[j]:
            s += str(k)
            for l in graph[k]:
                s += str(l)
                for m in graph[l]:
                    s += str(m)
                    for n in graph[m]:
                        s += str(n)
                        for o in graph[n]:
                            s += str(o)
                            for p in graph[o]:
                                s += str(p)
                                ls.append(int(s))
                        
print(ls[K-1])

Submission status: Runtime Error

Solution code:
import sys

K = int(input())

NLUNLUN = [[-1 for _ in range(11)] for _ in range(10)]
def num_lunlun(n, digit):
    if digit == 1:
        NLUNLUN[n][digit] = 1
        return NLUNLUN[n][digit]

    if digit == 2:
        if 0 < n < 9:
            NLUNLUN[n][digit] = 3
        else:
            NLUNLUN[n][digit] = 2
        return NLUNLUN[n][digit]

    n2 = 0
    if n - 1 >= 0:
        n2 += NLUNLUN[n-1][digit-1]
    n2 += NLUNLUN[n][digit-1]
    if n + 1 <= 9:
        n2 += NLUNLUN[n+1][digit-1]
    NLUNLUN[n][digit] = n2
    return NLUNLUN[n][digit]


if K < 10:
    print(K)

k = 0
for digit in range(1, 11):
    for n in range(10):
        tmp = num_lunlun(n, digit)
        if n > 0:
            if k + tmp >= K:
                ans = str(n)
                n2 = n
                for digit2 in range(digit-1, 1, -1):
                    if n2 - 1 >= 0:
                        tmp = NLUNLUN[n2 - 1][digit2]
                        if k + tmp >= K:
                            ans += str(n2 - 1)
                            n2 -= 1
                            continue
                        k += tmp

                    tmp = NLUNLUN[n2][digit2]
                    if k + tmp >= K:
                        ans += str(n2)
                        continue
                    k += tmp

                    if n2 + 1 <= 9:
                        tmp = NLUNLUN[n2 + 1][digit2]
                        if k + tmp >= K:
                            ans += str(n2 + 1)
                            n2 += 1
                            continue
                        k += tmp

                ans = int(ans + str(n2 + K - k - 2))
                print(ans)

                sys.exit(0)
            k += tmp

Submission status: Runtime Error

Solution code:
k = int(input())

cnt = 0
key = 1
for i in range(10**8):
    key = 1
    imoji = str(i)
    if len(imoji) == 2:
        if abs(int(imoji[0]) - int(imoji[1])) > 1:
            key = 0
    if len(imoji) == 3:
        if max(abs(int(imoji[0]) - int(imoji[1])),abs(int(imoji[2]) - int(imoji[1])))  > 1:
            key = 0
    if len(imoji) == 4:
        if max(abs(int(imoji[0]) - int(imoji[1])),abs(int(imoji[2]) - int(imoji[1])),abs(int(imoji[2]) - int(imoji[3])))  > 1:
            key = 0
    if len(imoji) == 5:
        if max(abs(int(imoji[0]) - int(imoji[1])),abs(int(imoji[2]) - int(imoji[1])),abs(int(imoji[2]) - int(imoji[3])),abs(int(imoji[4]) - int(imoji[3])))  > 1:
            key = 0
    if len(imoji) == 6:
        if max(abs(int(imoji[0]) - int(imoji[1])),abs(int(imoji[2]) - int(imoji[1])),abs(int(imoji[2]) - int(imoji[3])),abs(int(imoji[4]) - int(imoji[3])),abs(int(imoji[4]) - int(imoji[5])))  > 1:
            key = 0
    if key == 1:
        cnt += 1
        ans = i+1
    if cnt == k:
        break

print(ans)

input()

Submission status: Runtime Error

Solution code:
j = 0
lunlun = []
for i in range(K):
  man = i//10000
  senn = (i - man*10000)//1000
  hyaku = (i - man*10000-senn*1000)//100
  ju = (i - man*10000 - senn*1000 - hyaku*100)//10
  iti =i - man*10000 - senn*1000 - hyaku*100 -ju*10

  if abs(man - senn) < 2:
    q1 = 1
  else:
    q1 = 0

  if abs(senn - hyaku) < 2:
    q2 = 1
  else:
    q2 = 0

  if abs(hyaku - ju) < 2:
    q3 = 1
  else:
    q3 = 0

  if abs(hyaku - ju) < 2:
    q4 = 1
  else:
    q4 = 0

  if q1 == 1 and q2 == 1 and q3 == 1 and q4 == 1:
    lunlun.append(i)
    if j == K-1:
      break
    else:
      j =j+1

print(lunlun[j])

Submission status: Runtime Error

Solution code:
N = int(input())

def asym(bdict):
    pdict = {}
    pdict[0] = bdict[1]
    pdict[1] = bdict[0] + bdict[1] + bdict[2]
    pdict[2] = bdict[1] + bdict[2] + bdict[3]
    pdict[3] = bdict[2] + bdict[3] + bdict[4]
    pdict[4] = bdict[3] + bdict[4] + bdict[5]
    pdict[5] = bdict[4] + bdict[5] + bdict[6]
    pdict[6] = bdict[5] + bdict[6] + bdict[7]
    pdict[7] = bdict[6] + bdict[7] + bdict[8]
    pdict[8] = bdict[7] + bdict[8] + bdict[9]
    pdict[9] = bdict[8] + bdict[9]
    return pdict
    
if N <= 9:
    print(N)
else:
    bdict = {0:0, 1:1, 2:1, 3:1, 4:1, 5:1, 6:1, 7:1, 8:1, 9:1}
    cnt = 0
    n = 1
    while True:
        values = list(bdict.values())
        pcnt = cnt + sum(values)
        if N <= pcnt:
            ind = N - cnt  #n桁のルンルン数のうちind番目の数を求めれば良い
            cnt2 = 0
            ind2 = 0
            while True:
                esd = sum(values[:cnt2+1])
                if esd >= ind:
                    ind2 = ind - sum(values[:cnt2]) #一の位がcnt2であり、かつ桁数がnであるもののうち、ind２番目の数を求めれば良い
                    break
                else:
                    cnt2 += 1
                    
            break
                    
        else:
            cnt += sum(values)
            bdict = asym(bdict)
            n += 1
        
    alist = [str(cnt2)]
    
    def bous(alist):
        if len(alist[0]) == n:
            return alist
        else:
            blist = []
            for num in alist:
                top = int(num[0])
                plu, minu = top+1, top-1
                if plu < 10:
                    blist.append(str(plu)+num)
                if minu >= 0:
                    blist.append(str(minu)+num)
                alist = blist
            return(bous(alist))
        
    anc = bous(alist)
    blist = []
    for num in anc:
        if num[0] != "0":
            blist.append(int(num))
            
    blist.sort()
    print(blist[ind2]