Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
a = [int(input()) for _ in range(M)]

nums = [0]*(N+1)
nums[0] = 1
if 1 not in a:
    nums[1] = 1
for i in range(2, N+1):
    if not i in a:
      nums[i] = nums[i-1] + nums[i-2]

print(nums[N]%(1000000007))


Fixed submission:
N, M = map(int, input().split())
a = set([int(input()) for _ in range(M)])

nums = [0]*(N+1)
nums[0] = 1
if 1 not in a:
    nums[1] = 1
for i in range(2, N+1):
    if not i in a:
      nums[i] = nums[i-1] + nums[i-2]

print(nums[N]%(1000000007))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
a = [int(input()) for _ in range(M)]

nums = [0]*(N+1)
nums[0] = 1
if 1 not in a:
    nums[1] = 1
for i in range(2, N+1):
    if not i in a:
      nums[i] = nums[i-1] + nums[i-2]

print(nums[N]%(1000000007))


Fixed submission:
N, M = map(int, input().split())
a = set([int(input()) for _ in range(M)])

nums = [0]*(N+1)
nums[0] = 1
if 1 not in a:
    nums[1] = 1
for i in range(2, N+1):
    if not i in a:
      nums[i] = nums[i-1] + nums[i-2]

print(nums[N]%(1000000007))

Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
a = [int(input()) for _ in range(M)]

nums = [0]*(N+1)
nums[0] = 1
if 1 not in a:
    nums[1] = 1
for i in range(2, N+1):
    print(nums)
    if not i in a:
      nums[i] = nums[i-1] + nums[i-2]

print(nums[N]%(1000000007))


Fixed submission:
N, M = map(int, input().split())
a = set([int(input()) for _ in range(M)])

nums = [0]*(N+1)
nums[0] = 1
if 1 not in a:
    nums[1] = 1
for i in range(2, N+1):
    if not i in a:
      nums[i] = nums[i-1] + nums[i-2]

print(nums[N]%(1000000007))

Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
route = [int(x) for x in range(N)]
a = [int(input()) for _ in range(M)]
route = [item for index, item in enumerate(route) if index not in a]

nums = [0]*(N+1)
nums[0] = 1
if 1 not in a:
    nums[1] = 1
for i in range(2, N+1):
    print(nums)
    if not i in a:
      nums[i] = nums[i-1] + nums[i-2]

print(nums[N]%(1000000007))


Fixed submission:
N, M = map(int, input().split())
a = set([int(input()) for _ in range(M)])

nums = [0]*(N+1)
nums[0] = 1
if 1 not in a:
    nums[1] = 1
for i in range(2, N+1):
    if not i in a:
      nums[i] = nums[i-1] + nums[i-2]

print(nums[N]%(1000000007))
Buggy python submission (verdict: Runtime Error):
def main():

    N = int(input())
    if 0 > N or N > 100:
        print("input N error")
        exit()
        
    a = []
    for i in range(N):
        b = input().split()
        if len(b[0]) > 10 or len(b[0]) < 1:
            print("input S error")
            exit()

        if int(b[1]) < 0 or int(b[1]) > 100:
            print("input P error")
            exit()

        b.append(i + 1)
        a.append(b)
        
    a.sort()
    print(a)
    c = []
    answer = []
    
    for i in range(len(a)):
        c.append(a[i])

        if len(a) == 1:
         answer += c   
        
        elif a[i][0] != a[i+1][0] and i != len(a) - 2:
            c.sort(key=lambda x: int(x[1]), reverse=True)
            answer += c
            c = []

        elif a[i][0] != a[i+1][0] and i == len(a) - 2:
            c.sort(key=lambda x: int(x[1]), reverse=True)
            answer += c
            c = []
            c.append(a[i+1])
            answer += c
            break

        elif a[i][0] == a[i+1][0] and i == len(a) - 2:
            c.append(a[i+1])
            c.sort(key=lambda x: int(x[1]), reverse=True)
            answer += c
            break

    print(answer)
    for i in range(len(answer)):
        print(answer[i][2])
        
        
if __name__ == '__main__':

    main()

    


Fixed submission:
def main():

    apple, apple_piece = (int(val) for val in input().split())

    apple_piece += apple * 3
    apple_pie = int(apple_piece / 2)

    print(apple_pie)

if __name__ == '__main__':

    main()

Buggy python submission (verdict: Runtime Error):
def main():

    N = int(input())
    if 0 > N or N > 100:
        print("input N error")
        exit()
        
    a = []
    for i in range(N):
        b = input().split()
        if len(b[0]) > 10 or len(b[0]) < 1:
            print("input S error")
            exit()

        if int(b[1]) < 0 or int(b[1]) > 100:
            print("input P error")
            exit()

        b.append(i + 1)
        a.append(b)
        
    a.sort()
    c = []
    answer = []
    
    for i in range(len(a) - 1):
        c.append(a[i])

        if i == len(a) - 2:
            elif a[i][0] != a[i+1][0]:
                c.sort(key=lambda x: int(x[1]), reverse=True)
                answer += c
                answer.append(a[i+1])

            else:
                c.append(a[i+1])
                c.sort(key=lambda x: int(x[1]), reverse=True)
                answer += c
                
        elif a[i][0] != a[i+1][0]:
            c.sort(key=lambda x: int(x[1]), reverse=True)
            answer += c
            c = []

            
    for i in range(len(answer)):
        print(answer[i][2])
        
        
if __name__ == '__main__':

    main()

    


Fixed submission:
def main():

    apple, apple_piece = (int(val) for val in input().split())

    apple_piece += apple * 3
    apple_pie = int(apple_piece / 2)

    print(apple_pie)

if __name__ == '__main__':

    main()
Buggy python submission (verdict: Time Limit Exceeded):
import heapq
n, m = map(int, input().split())
a = list(map(int, input().split()))
heapq.heapify(a)

for _ in range(m):
    b, c = map(int, input().split())
    for i in range(n):
        min0 = heapq.heappop(a)
        if min0 < c:
            heapq.heappush(a, c)
            b -= 1
            if b == 0:
                break
        else:
            heapq.heappush(a, min0)
print(sum(a))
        



Fixed submission:
import sys
n, m = map(int, input().split())
a = sorted(list(map(int, input().split())))
b = []

for _ in range(m):
    b += [list(map(int, input().split()))]
b = sorted(b, key=lambda x: x[1], reverse=True)

for i in range(m):
    for j in range(b[i][0]):
        if a[0] < b[i][1]:
            a.pop(0)
            a.append(b[i][1])
        else:
            print(sum(a))
            sys.exit()
print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
a = sorted(list(map(int, input().split())))

for _ in range(m):
    b, c = map(int, input().split())
    for i in range(n):
        if a[i] < c:
            a[i] = c
            b -= 1
            if b == 0:
                a = sorted(a)
                break
print(sum(a))
        



Fixed submission:
import sys
n, m = map(int, input().split())
a = sorted(list(map(int, input().split())))
b = []

for _ in range(m):
    b += [list(map(int, input().split()))]
b = sorted(b, key=lambda x: x[1], reverse=True)

for i in range(m):
    for j in range(b[i][0]):
        if a[0] < b[i][1]:
            a.pop(0)
            a.append(b[i][1])
        else:
            print(sum(a))
            sys.exit()
print(sum(a))
Buggy python submission (verdict: Time Limit Exceeded):
N, Q = map(int, input().split())
S = input()
for _ in range(Q):
    a, b = map(int, input().split())
    print(S[a-1:b].count('AC'))
    


Fixed submission:
N, Q = map(int, input().split())
S = input()
a = [0]*(N+1)
for i in range(N):
    if S[i:i+2] == 'AC':
        a[i+1] = a[i] + 1
    else:
        a[i+1] = a[i]
for _ in range(Q):
    b, c = map(int, input().split())
    print(a[c-1]-a[b-1])
Buggy python submission (verdict: Time Limit Exceeded):
import re
S = input()
ans = 0
a = len(S)
while True:
    S = re.sub('01|10|1001|0110', '', S)
    if ans - len(S) == 0:
        print(a-ans)
        break
    else:
        ans = len(S)



Fixed submission:
import re
S = input()
print(2*min(S.count('0'), S.count('1')))

Buggy python submission (verdict: Time Limit Exceeded):
import re
S = input()
ans = 0
a = len(S)
while True:
    S = re.sub('01|10', '', S)
    if ans - len(S) == 0:
        print(a-ans)
        break
    else:
        ans = len(S)



Fixed submission:
import re
S = input()
print(2*min(S.count('0'), S.count('1')))

Buggy python submission (verdict: Time Limit Exceeded):
import re
S = input()
ans = 0

while True:
    num0 = S.count('01')
    S = re.sub('01', '', S)
    num1 = S.count('10')
    S = re.sub('10', '', S)
    ans += 2*(num0+num1)
    if num0+num1 == 0:
        print(ans)
        break
    


Fixed submission:
import re
S = input()
print(2*min(S.count('0'), S.count('1')))
Buggy python submission (verdict: Runtime Error):
n = int(input())
a, b, c = [], [], []
for _ in range(n):
    x, y = map(int, input().split())
    a.append(max(y-x, x))
    b.append(max(x-y, y))
    c.append([x, y])
num = [x for x in range(n)]
a, a0 = zip(*sorted(zip(a, num)))
b, b0 = zip(*sorted(zip(b, num)))
ans = 0
a, a0, b, b0 = list(a), list(a0), list(b), list(b0)
for i in range(n):
    if i%2 == 0:
       val = a0.pop()
       ans += c[val][0]
       a.pop()
       b.pop(val)
       b0.pop(val)
    else:
       val = b0.pop()
       ans -= c[val][1]
       b.pop()
       a.pop(val)
       a0.pop(val)
print(ans)


Fixed submission:
n = int(input())
ans, a, b = 0, [], []
for _ in range(n):
    x, y = map(int, input().split())
    a.append(x+y)
    b.append([x, y])
num = [x for x in range(n)]
a, a0 = zip(*sorted(zip(a, num)))
a, a0 = list(a), list(a0)

for i in range(n):
    val = a0.pop()
    a.pop()
    if i%2 == 0:
       ans += b[val][0]
    else:
       ans -= b[val][1]
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
x, y = map(int, input().split())
dp = [[0]*(y+1) for _ in range(x+1)]

for i in range(1, x+1):
    w, v = map(int, input().split())
    for j in range(y+1):
        num0 = dp[i-1][j]
        if j-w >= 0:
            num1 = dp[i-1][j-w]+v
            if num1 > num0:
                dp[i][j] = num1
            else:
                dp[i][j] = num0
        else:
            dp[i][j] = num0
print(max(dp[-1]))


Fixed submission:
def num():
    x, y = map(int, input().split())
    dp = [0]*(y+1)

    for i in range(x):
        w, v = map(int, input().split())
        for j in range(y, w-1, -1):
            num = dp[j-w]+v
            if num > dp[j]:
                dp[j] = num
    print(dp[-1])

num()

Buggy python submission (verdict: Time Limit Exceeded):
x, y = map(int, input().split())
dp = [[-1]*(y+1) for _ in range(x+1)]

for i in range(y+1):
    dp[0][i] = 0

for i in range(1, x+1):
    w, v = map(int, input().split())
    for j in range(y+1):
        if j-w >= 0:
            dp[i][j] = max(dp[i-1][j-w]+v, dp[i-1][j])
        else:
            dp[i][j] = dp[i-1][j]
print(max(dp[-1]))


Fixed submission:
def num():
    x, y = map(int, input().split())
    dp = [0]*(y+1)

    for i in range(x):
        w, v = map(int, input().split())
        for j in range(y, w-1, -1):
            num = dp[j-w]+v
            if num > dp[j]:
                dp[j] = num
    print(dp[-1])

num()
Buggy python submission (verdict: Runtime Error):
import numpy as np

N, W = map(int, input().split())

dp = np.zeros(W+1, dtype=np.int64)

for i in range(N):
    w, v = map(int, input().split())
    dp[w:] = np.maximum(dp[w:], dp[:-w]+v)
print(dp[-1])
    


Fixed submission:
import numpy as np

N, W = map(int, input().split())
a = [list(map(int,input().split())) for _ in range(N)]
vmax = sum([a[i][1] for i in range(N)])
INF = float('inf')
dp = np.full(vmax+1, INF)
dp[0] = 0

for i in range(N):
  w, v = a[i-1]
  dp[v:] = np.minimum(dp[v:], dp[:-v]+w)
print(max([i for i in range(vmax+1) if dp[i] <= W]))

Buggy python submission (verdict: Runtime Error):
import numpy as np

N, W = map(int, input().split())

dp = np.zeros(W + 1, dtype = np.int64)

for i in range(N):
    w, v = map(int, input().split())
    dp[w:] = np.maximum(dp[w:], dp[:-w] + v)

print(dp.max())
    


Fixed submission:
import numpy as np

N, W = map(int, input().split())
a = [list(map(int,input().split())) for _ in range(N)]
vmax = sum([a[i][1] for i in range(N)])
INF = float('inf')
dp = np.full(vmax+1, INF)
dp[0] = 0

for i in range(N):
  w, v = a[i-1]
  dp[v:] = np.minimum(dp[v:], dp[:-v]+w)
print(max([i for i in range(vmax+1) if dp[i] <= W]))

Buggy python submission (verdict: Runtime Error):
import numpy as np

N, W = map(int, input().split())

dp = np.zeros(W + 1, dtype = np.int64)

for i in range(N):
    w, v = map(int, input().split())
    dp[w:] = np.maximum(dp[w:], dp[:-w] + v)

    print(dp.max())
    


Fixed submission:
import numpy as np

N, W = map(int, input().split())
a = [list(map(int,input().split())) for _ in range(N)]
vmax = sum([a[i][1] for i in range(N)])
INF = float('inf')
dp = np.full(vmax+1, INF)
dp[0] = 0

for i in range(N):
  w, v = a[i-1]
  dp[v:] = np.minimum(dp[v:], dp[:-v]+w)
print(max([i for i in range(vmax+1) if dp[i] <= W]))
Buggy python submission (verdict: Runtime Error):
n = int(input())
a = [int(input()) for _ in range(n)]
a.sort()
num = a[-1]-a[-2]
if num%2 == 0:
    print('second')
else:
    print('first')


Fixed submission:
n = int(input())
a = [int(input()) for _ in range(n)]
ans = [x for x in a if x%2 != 0]

if len(ans) == 0:
    print('second')
else:
    print('first')
Buggy python submission (verdict: Runtime Error):
from itertools import permutations
N = int(input())

val = []
for _ in range(N):
    val.append(int(input()))
ans = 10**10
for i in permutations(val):
    for j in range(1, N):
        ans = min(max(sum(i[:j]), sum(i[j:])), ans)
if N == 1:
    print(val[0])
else:
    print(ans)
    

    


Fixed submission:
S = input()
ans = 0
count = 0
for i in S:
    if i == 'B':
        count += 1
    elif i == 'W':
        ans += count
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import math
import sys
n, m = map(int, input().split())
s = list(input())
t = list(input())
a = (n*m)//math.gcd(n, m)
b0 = [i*(a//n)+1 for i in range(n)]
b1 = [i*(a//m)+1 for i in range(m)]
b2 = set(b0)
b3 = set(b1)

for j, i in enumerate(b0):
    if i in b3:
        if s[b0.index(i)] != t[b1.index(i)]:
            print(-1)
            sys.exit()
            
for j, i in enumerate(b1):
    if i in b2:
        if s[b0.index(i)] != t[b1.index(i)]:
            print(-1)
            sys.exit()
print(a)


Fixed submission:
import math
import sys
n, m = map(int, input().split())
s = list(input())
t = list(input())
a = (n*m)//math.gcd(n, m)
b0 = [i*(a//n)+1 for i in range(n)]
b1 = [i*(a//m)+1 for i in range(m)]
b2 = set(b0)
b3 = set(b1)

for j, i in enumerate(b0):
    if i in b3:
        if s[j] != t[b1.index(i)]:
            print(-1)
            sys.exit()
            
for j, i in enumerate(b1):
    if i in b2:
        if s[b0.index(i)] != t[j]:
            print(-1)
            sys.exit()
print(a)

Buggy python submission (verdict: Time Limit Exceeded):
import math
import sys
n, m = map(int, input().split())
s = list(input())
t = list(input())
a = (n*m)//math.gcd(n, m)
b0 = [i*(a//n)+1 for i in range(n)]
b1 = [i*(a//m)+1 for i in range(m)]

for j, i in enumerate(b0):
    if i in b1:
        if s[b0.index(i)] != t[b1.index(i)]:
            print(-1)
            sys.exit()
    
for j, i in enumerate(b1):
    if i in b0:
        if s[b0.index(i)] != t[b1.index(i)]:
            print(-1)
            sys.exit()
print(a)


Fixed submission:
import math
import sys
n, m = map(int, input().split())
s = list(input())
t = list(input())
a = (n*m)//math.gcd(n, m)
b0 = [i*(a//n)+1 for i in range(n)]
b1 = [i*(a//m)+1 for i in range(m)]
b2 = set(b0)
b3 = set(b1)

for j, i in enumerate(b0):
    if i in b3:
        if s[j] != t[b1.index(i)]:
            print(-1)
            sys.exit()
            
for j, i in enumerate(b1):
    if i in b2:
        if s[b0.index(i)] != t[j]:
            print(-1)
            sys.exit()
print(a)
Buggy python submission (verdict: Time Limit Exceeded):
s = input()
t = input()
num = set()

for i in range(len(s)):
    a, b = s[i], t[i]
    if a != b:
        s = s.replace(a, '-').replace(b, a).replace('-', b)
        
if s == t:
    print('Yes')
else:
    print('No')


Fixed submission:
import sys
s = list(input())
t = list(input())
a = [-1 for _ in range(26)]
b = [-1 for _ in range(26)]

for i in range(len(s)):
    num0, num1 = a[ord(s[i])-97], b[ord(t[i])-97]
    if num0 >= 0:
        if chr(num0+97) != t[i]:
            print('No')
            sys.exit()
    else:
        a[ord(s[i])-97] = ord(t[i])-97
        
    if num1 >= 0:
        if chr(num1+97) != s[i]:
            print('No')
            sys.exit()
    else:
        b[ord(t[i])-97] = ord(s[i])-97
            
print('Yes')

Buggy python submission (verdict: Time Limit Exceeded):
s = input()
t = input()
num = set()

for i in range(len(s)):
    if s[i] != t[i]:
        if not i in num:
            a, b = s[i], t[i]
            s = s.replace(a, '-')
            s = s.replace(b, a)
            s = s.replace('-', b)
            num.add(a)
            num.add(b)
    num.add(s[i])
    num.add(t[i])
            
if s == t:
    print('Yes')
else:
    print('No')


Fixed submission:
import sys
s = list(input())
t = list(input())
a = [-1 for _ in range(26)]
b = [-1 for _ in range(26)]

for i in range(len(s)):
    num0, num1 = a[ord(s[i])-97], b[ord(t[i])-97]
    if num0 >= 0:
        if chr(num0+97) != t[i]:
            print('No')
            sys.exit()
    else:
        a[ord(s[i])-97] = ord(t[i])-97
        
    if num1 >= 0:
        if chr(num1+97) != s[i]:
            print('No')
            sys.exit()
    else:
        b[ord(t[i])-97] = ord(s[i])-97
            
print('Yes')

Buggy python submission (verdict: Time Limit Exceeded):
s = input()
t = input()
num = set()

for i in range(len(s)):
    if s[i] != t[i]:
        if not i in num:
            a, b = s[i], t[i]
            s = s.replace(a, '-')
            s = s.replace(b, a)
            s = s.replace('-', b)
            num.add(s[i])

if s == t:
    print('Yes')
else:
    print('No')


Fixed submission:
import sys
s = list(input())
t = list(input())
a = [-1 for _ in range(26)]
b = [-1 for _ in range(26)]

for i in range(len(s)):
    num0, num1 = a[ord(s[i])-97], b[ord(t[i])-97]
    if num0 >= 0:
        if chr(num0+97) != t[i]:
            print('No')
            sys.exit()
    else:
        a[ord(s[i])-97] = ord(t[i])-97
        
    if num1 >= 0:
        if chr(num1+97) != s[i]:
            print('No')
            sys.exit()
    else:
        b[ord(t[i])-97] = ord(s[i])-97
            
print('Yes')
Buggy python submission (verdict: Runtime Error):
n, k = map(int, input().split())
x0 = list(map(int, input().split()))

x1 = [i for i in x0 if i >= 0]
x2 = list(reversed([i for i in x0 if i < 0]))
count0 = len(x1)
count1 = len(x2)
ans = float('inf')

if count1 == 0:
    print(x1[k-1])
elif count0 == 0:
    print(x2[k-1])
else:
    for i in range(min(count0, k)):
        ans = min(ans, x1[i]*2-x2[k-i-2])
    for i in range(min(count1, k)):
        ans = min(ans, -x2[i]*2+x1[k-i-2])
    print(ans)


Fixed submission:
n, k = map(int, input().split())
x0 = list(map(int, input().split()))

x1 = [i for i in x0 if i >= 0]
x2 = list(reversed([i for i in x0 if i < 0]))
count0 = len(x1)
count1 = len(x2)
ans = float('inf')

if count1 == 0:
    print(x1[k-1])
elif count0 == 0:
    print(-x2[k-1])
else:
    if 0 in x1:
        x1.remove(0)
        k -= 1
        count0 -= 1
    for i in range(min(count0, k)):
        if count1 >= k-i-1:
            ans = min(ans, x1[i]*2-x2[k-i-2])
    for i in range(min(count1, k)):
        if count0 >= k-i-1:
            ans = min(ans, -x2[i]*2+x1[k-i-2])
    print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
from itertools import permutations

def cal():
    d, g = map(int, input().split())
    a = list(permutations([[i+1]+list(map(int, input().split())) for i in range(d)]))
    ans = float('inf')

    for i in a:
        val, count = g, 0
        for a, b, c in i:
            num = 100*a*b
            if num+c >= val:
                if num >= val:
                    count += val//(100*a)
                else:
                    count += b
                break
            else:
                count += b
                val -= num+c
            if count >= ans:
                break
        ans = min(ans, count)
    print(ans)

cal()


Fixed submission:
def cal():
    d, g = map(int, input().split())
    a = [list(map(int, input().split())) for i in range(d)]
    ans = float('inf')

    for i in range(2**d):
        b = ['-']*d
        for j in range(d):
            if (i>>j)&1:
                b[j] = '+'
        count0, count1 = 0, 0
        for j in range(d):
            if b[j] == '+':
                count0 += 100*(j+1)*a[j][0]+a[j][1]
                count1 += a[j][0]
        if count0 < g:
            for j in range(d-1, -1, -1):
                if b[j] == '-':
                    for k in range(a[j][0]):
                        if count0 >= g:
                            break
                        count0 += 100*(j+1)
                        count1 += 1
        ans = min(ans, count1)
    print(ans)
cal()
Buggy python submission (verdict: Runtime Error):
def IslandsWar():
    n, m = map(int, input().split())
    a = [0 for _ in range(n-1)]
    INF = float('INF')
    for _ in range(m):
        x, y = map(int, input().split())
        for i in range(x-1, y-1):
            if abs(x-y) == 1:
                a[i] = INF
            else:
                a[i] += 1

    ans =  0
    count = 0
    status = False
    for i in range(n-2):
        if a[i] == INF:
            ans += 1
        elif a[i] < a[i+1]:
            status = True
        elif a[i] > a[i+1] and status:
            ans += 1
            count = 0
            status = False
        elif a[i] == a[i+1]:
            count += 1
        else:
            if count != 0:
                ans += 1
            count = 0
    if count != 0:
        ans += 1
    if a[-2] < a[-1]:
        ans += 1
    if a[-1] == INF:
        ans += 1
    print(ans)
            
if __name__ == "__main__":
    IslandsWar()


Fixed submission:
def IslandsWar():
    n, m = map(int, input().split())
    a = [list(map(int,input().split())) for _ in range(m)]
    a.sort(key=lambda x: x[1])
    removes = [a[0][1]]

    for i, j in a[1:]:
        check = False
        for k in removes:
            if i < k:
                check = True
                break
        if not check:
            removes.append(j)
    print(len(removes))
            
if __name__ == "__main__":
    IslandsWar()
Buggy python submission (verdict: Runtime Error):
n = int(input())
ans, index6, index9, num6, num9 = 0, 0, 0, 0, 0
INF = float('inf')

def a10_to_n(x, n):
    if x//n:
        return a10_to_n(x//n, n)+str(x%n)
    else:
        return str(x)

def index(n, num6, index6, num9, index9):
    val = str(a10_to_n(n, 6))
    for i, j in enumerate(val):
        if int(j) != 0:
            num6 = int(j)
            index6 = len(val)-i-1
            break
    val = str(a10_to_n(n, 9))
    for i, j in enumerate(val):
        if int(j) != 0:
            num9 = int(j)
            index9 = len(val)-i-1
            break
    return num6, index6, num9, index9

num6, index6, num9, index9 = index(n, num6, index6, num9, index9)
while index6 > 1 or index9 > 1:
    if 6**index6 <= 9**index9:
        n -= num9*9**index9
        ans += num9
    else:
        n -= num6*6**index6
        ans += num6
    num6, index6, num9, index9 = index(n, num6, index6, num9, index9)
    
a = [INF for _ in range(n+1)]
a[n] = 0
for i in range(n, -1, -1):
    if i-9 >= 0:
        a[i-9] = min(a[i]+1, a[i-9])
    if i-6 >= 0:
        a[i-6] = min(a[i]+1, a[i-6])
    if i-1 >= 0:
        a[i-1] = min(a[i]+1, a[i-1])
print(ans+a[0])


Fixed submission:
n = int(input())
index6, index9, num6, num9 = 0, 0, 0, 0
INF = float('inf')    
a = [INF for _ in range(n+1)]
a[n] = 0

for i in range(n, -1, -1):
    if a[i] == INF:
        continue
    j = 1
    while i-9**j >= 0:
        a[i-9**j] = min(a[i]+1, a[i-9**j])
        j += 1
    j = 1
    while i-6**j >= 0:
        a[i-6**j] = min(a[i]+1, a[i-6**j])
        j += 1
    if i-1 >= 0:
        a[i-1] = min(a[i]+1, a[i-1])
print(a[0])