Problem p02720 (python)

Fixed submission:
# -*- coding: utf-8 -*-
"""
Created on Sat Apr  4 20:58:44 2020

@author: ctiuser
"""
import sys

K = int(input())

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

M = [0,[1,1,1,1,1,1,1,1,1,1]]
s = 9
while K > s:    
    m = [0 for _ in range(10)]
    for i in range(10):
        if i == 0:
            m[i] += M[-1][i] + M[-1][i+1]
        elif i == 9:
            m[i] += M[-1][i-1] + M[-1][i]
        else:
            m[i] += M[-1][i-1] + M[-1][i] + M[-1][i+1]
    M.append(m)
    s += sum(m[1:])
s -= sum(m[1:])
keta = len(M) - 1
ans = ''

i = 0
while K > s:
    i += 1
    s += M[keta][i]

s -= M[keta][i]
ans += str(i)
keta -= 1
d = K - s
#print(ans)
#print(i, d)
while keta > 0:
    if i == 0:
        d -= M[keta][i]
        if d <= 0:
            i = i
            ans += str(i)
            d += M[keta][i]            
        else:
            d -= M[keta][i+1]
            if d <= 0:
                i = i+1
                ans += str(i)
                d += M[keta][i]
    elif i == 9:
        d -= M[keta][i-1]
        if d <= 0:
            i = i-1
            ans += str(i)
            d += M[keta][i]
        else:
            d -= M[keta][i]
            if d <= 0:
                i = i
                ans += str(i)
                d += M[keta][i]
    else:
        d -= M[keta][i-1]
        if d <= 0:
            i = i-1
            ans += str(i)
            d += M[keta][i]
        else:
            d -= M[keta][i]
            if d <= 0:
                i = i
                ans += str(i)
                d += M[keta][i]
            else:
                d -= M[keta][i+1]
                if d <= 0:
                    i = i+1
                    ans += str(i)
                    d += M[keta][i]
        
        
    keta -= 1
#    print(ans)
#    print(i,d)


print(ans)

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
"""
Created on Sat Apr  4 20:58:44 2020

@author: ctiuser
"""

K = int(input())

M = [0,[1,1,1,1,1,1,1,1,1,1]]
s = 9
while K > s:    
    m = [0 for _ in range(10)]
    for i in range(10):
        if i == 0:
            m[i] += M[-1][i] + M[-1][i+1]
        elif i == 9:
            m[i] += M[-1][i-1] + M[-1][i]
        else:
            m[i] += M[-1][i-1] + M[-1][i] + M[-1][i+1]
    M.append(m)
    s += sum(m[1:])
s -= sum(m[1:])
keta = len(M) - 1
ans = ''

i = 0
while K > s:
    i += 1
    s += M[keta][i]

s -= M[keta][i]
ans += str(i)
keta -= 1
d = K - s
#print(ans)
#print(i, d)
while keta > 0:
    if i == 0:
        d -= M[keta][i]
        if d <= 0:
            i = i
            ans += str(i)
            d += M[keta][i]            
        else:
            d -= M[keta][i+1]
            if d <= 0:
                i = i+1
                ans += str(i)
                d += M[keta][i]
    elif i == 9:
        d -= M[keta][i-1]
        if d <= 0:
            i = i-1
            ans += str(i)
            d += M[keta][i]
        else:
            d -= M[keta][i]
            if d <= 0:
                i = i
                ans += str(i)
                d += M[keta][i]
    else:
        d -= M[keta][i-1]
        if d <= 0:
            i = i-1
            ans += str(i)
            d += M[keta][i]
        else:
            d -= M[keta][i]
            if d <= 0:
                i = i
                ans += str(i)
                d += M[keta][i]
            else:
                d -= M[keta][i+1]
                if d <= 0:
                    i = i+1
                    ans += str(i)
                    d += M[keta][i]
        
        
        keta -= 1
#        print(ans)
#        print(i,d)


print(ans)