Harryxun/GPT-100-sft-dataset huggingface dataset:
Problem p02936 (python)

Fixed submission:
from collections import deque

n, q = map(int, input().split())
ans = [0 for x in range(n)]
g = [list() for x in range(n)]
for i in range (n - 1) :
    v, u = map(int, input().split())
    v -= 1
    u -= 1
    g[v].append(u)
    g[u].append(v)
for i in range (q):
    p, x = map(int, input().split())
    p -= 1;
    ans[p] += x
dq = deque()
dq.append((0, -1))
while (len(dq)) :
    v, p = dq.pop()
    for u in g[v]:
        if (u != p) :
            ans[u] += ans[v]
            dq.append((u, v))
print(*ans)

Buggy submission (verdict: Runtime Error):
n, q = map(int, input().split())
ans = [0 for x in range(n)]
g = [list() for x in range(n)]
for i in range (n - 1) :
    v, u = map(int, input().split())
    v -= 1
    u -= 1
    g[v].append(u)
    g[u].append(v)
for i in range (q):
    p, x = map(int, input().split())
    p -= 1;
    ans[p] += x
def dfs(v, p):
    for u in g[v]:
        if (u != p) :
            ans[u] += ans[v]
            dfs(u, v)
dfs(0, -1)
print(*ans)

Buggy submission (verdict: Runtime Error):
n, q = map(int, input().split())
ans = [0 for x in range(n)]
g = [list() for x in range(n)]
for i in range (n - 1) :
    v, u = map(int, input().split())
    v -= 1
    u -= 1
    g[v].append(u)
    g[u].append(v)
for i in range (q):
    p, x = map(int, input().split())
    p -= 1;
    ans[p] += x
def dfs(v, p):
    for u in g[v]:
        if (u != p) :
            ans[u] += ans[v]
            dfs(u, v)
dfs(0, -1)
for x in ans:
    print(x, end = ' ')
Problem p03071 (python)

Fixed submission:
a,b = map(int, input().split())
ans = max(a + b, 2*a-1, 2*b-1)
print(ans)

Buggy submission (verdict: Runtime Error):
a,b = map(int, input(),split())
ans = max(a + b, 2*a-1, 2*b-1)
print(ans)

Buggy submission (verdict: Runtime Error):
a,b = map(int, input())
ans = max(a + b, 2*a-1, 2*b-1)
print(ans)
Problem p03136 (python)

Fixed submission:
n = input()
L = list(map(int, input().split()))
print('Yes' if sum(L) > 2 * max(L) else 'No')

Buggy submission (verdict: Runtime Error):
n = int(input())
l = [int.input() for i in range(n)]
s = sorted(l)
sum_of_s = sum(s)
if sum_of_s > 2 * s[-1]:
  print('Yes')
else:
  print('No')
Problem p03145 (python)

Fixed submission:
a,b,c=map(int,input().split())
print(a*b//2)

Buggy submission (verdict: Runtime Error):
c,a,b = map(int,input().split())
area = c*a//2
print(ans)

Buggy submission (verdict: Runtime Error):
c,a,b = map(int,input().split())
area = c * a // 2
print(ans)
Problem p03161 (python)

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

def frog(n, k, h):
    import math
    d = [float('inf')] * n
    d[0] = 0
    
    for i in range(1, n):
        
        d[i] = min(d[j] + abs(h[i] - h[j]) for j in range(max(i-k, 0), i))
    return d[n-1]
    
print(frog(n, k, h))

Buggy submission (verdict: Runtime Error):
import math
n, k = map(int, input().split())
h = list(map(int, input().split()))

def frog(n, k, h):
    d = [math.inf] * n
    d[0] = 0
    
    for i in range(1, n):
        
        d[i] = min(d[j] + abs(h[i] - h[j]) for j in range(max(i-k, 0), i))
    return d[n-1]
    
print(frog(n, k, h))
Problem p03210 (python)

Fixed submission:
x = input()
 
print('YES' if x in '753' else 'NO')

Buggy submission (verdict: Runtime Error):
x = input()

print('YES" if x in '753' else 'NO')
Problem p03337 (python)

Fixed submission:
a,b=map(int,input().split())
print(max(a+b,a-b,a*b))

Buggy submission (verdict: Runtime Error):
a,b,c = map(int, input().split())
ans1 = max(a+b, a-b)
ans = max(ans1, a * b)
print(ans)

Buggy submission (verdict: Runtime Error):
a,b,c = map(int, input())
ans1 = max(a+b, a-b)
ans = max(ans1, a * b)
print(ans)
Problem p03493 (python)

Fixed submission:
print(input().count("1"))

Buggy submission (verdict: Runtime Error):
print(input().count('1))
Problem p03502 (python)

Fixed submission:
x = input()
sum_of_digits = sum(map(int, x))
check = int(x) % sum_of_digits
print('No' if check else 'Yes')

Buggy submission (verdict: Runtime Error):
s = input()
sum_of_digits = sum(map(int, s))
if int(s) % sum_of_digts == 0:
  print('Yes')
else:
  print('No')

Buggy submission (verdict: Runtime Error):
s = input()
sum_of_digits = sum(map(int, s))
if (int)s % sum_of_digits == 0:
  print('Yes')
else:
  print('No')

Buggy submission (verdict: Runtime Error):
s = input()
sum_of_digits = sum(map(int, s))
if (int)s % sum_of_digits:
  print('Yes')
else:
  print('No')

Buggy submission (verdict: Runtime Error):
x = input()
sum_of_digits = sum(map(int, x))
check = (int)x % sum_of_digits
print('No' if check else 'Yes')

Buggy submission (verdict: Runtime Error):
x = input()
n = (int)x
sum_of_digits = sum(map(int, x))
check = n % sum_of_digits
print('No' if check else 'Yes')
Problem p03635 (python)

Fixed submission:
n,m=map(int,input().split())
print((n-1)*(m-1))

Buggy submission (verdict: Runtime Error):
a, b = int(input().split())
print((a-1) * (b - 1))
Problem p03671 (python)

Fixed submission:
a,b,c = sorted(map(int, input().split()))
ans = a + b
print(ans)

Buggy submission (verdict: Runtime Error):
l = [int(input()) for _ in range(3)]
s = sorted(l)[::-1]
ans = s[0] + s[1]
print(ans)
Problem p03697 (python)

Fixed submission:
a, b = map(int, input().split())
temp_ans = a + b
if temp_ans >= 10:
  print('error')
else:
  print(temp_ans)

Buggy submission (verdict: Runtime Error):
a, b = map(int, input().split())
sum = a + b
if sum >= 10:
  print('error')
else:
  print(ans)
Problem p03826 (python)

Fixed submission:
a,b,c,d = map(int, input().split())
rectangle1 = a * b
rectangle2 = c * d
ans = max(rectangle1, rectangle2)
print(ans)

Buggy submission (verdict: Runtime Error):
a,b,c,d = map(int, input().split())
rectangle1 = a * b
rectangel2 = c * d
ans = max(rectangle1, rectangle2)
print(ans)
Problem p04044 (python)

Fixed submission:
n, l = map(int , input().split())
s = sorted([input() for i in range(n)])
print(*s, sep = "")

# 1次元リストをアンパックを使用して出力する場合、print(*リスト名)で出力する。

Buggy submission (verdict: Runtime Error):
n, l = map(int(input().split()))
l = [for i in range(n)]
s = sorted(l)
print(*s,sep = "")

Buggy submission (verdict: Runtime Error):
n, l = map(int(input()))
l = [for i in range(n)]
s = sorted(l)
print(*s,sep = "")
Problem p03194 (python)

Fixed submission:
n, p = map(int, input().split())

if n==1:
    print(p)
else:
    out=1
    tm = int(pow(p, 1/n))
    for i in range(2,tm+1):
        while p%(i**n)==0:
            p = p//(i**n)
            out *= i
        if p < i**n:
            break
    print(out)

Buggy submission (verdict: Runtime Error):
n, p = map(int, input().split())
out=1
for i in range(2,p+1):
    while p%(i**n)==0:
        p = p//(i**n)
        out *= i
        
    if p < i**n:
        break
print(out)

Buggy submission (verdict: Runtime Error):
n, p = map(int, input().split())
dic = {}

for i in [2] + list(range(3,p+1,2)):
    while p%i==0:
        if i not in dic.keys():
            dic[i] = 0
        p = p// i
        dic[i] += 1
        
    if p==1:
        break

out=1
for i in dic.keys():
    if dic[i] >=n:
        out *= i**(dic[i]//n)

print(out)
Problem p03231 (python)

Fixed submission:
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a

n,m = map(int, input().split())
s = input()
t = input()

gcd_ = gcd(n,m)
lcm = n*m//gcd_

a = lcm//n
b = lcm//m

flag=0
le = min(n,m)//(min(n,m)//gcd_)

for i in range(le):
    if s[i*b]!=t[i*a]:
        flag=1

if flag==1:
    print(-1)
else:
    print(lcm)

Buggy submission (verdict: Runtime Error):
import math

n,m = map(int, input().split())
s = input()
t = input()

gcd = math.gcd(n,m)
lcm = n*m//gcd

a = lcm//n
b = lcm//m

flag=0
le = min(n,m)//(min(n,m)//gcd)

for i in range(le):
    # print(i*b, i*a)
    if s[i*b]!=t[i*a]:
        flag=1

if flag==1:
    print(-1)
else:
    print(lcm)
Problem p03241 (python)

Fixed submission:
n,m = map(int, input().split())
 
ma = m//n
 
flag=0
 
while(flag==0):
    if m%ma==0:
        print(ma)
        flag = 1
    ma -=1

Buggy submission (verdict: Runtime Error):
n,m = map(int, input().split())

ma = m//n
re = m%n

flag=0

if re==0:
    while(flag==0):
        if m%ma==0:
            print(ma)
            flag = 1
        ma -=1
else:
    flag2=0
    while(flag==0):
        if m%ma==0:
            print(ma)
            flag = 1
            
        if flag2==0:
            ma -=1
            if ma%re==0:
                flag2=1
        
        else:
            ma -= re
Problem p03282 (python)

Fixed submission:
s = input()
k = int(input())

i=0
while(s[i]=="1"):
    if i==(len(s)-1):
        break
    i +=1
    
if k<(i+1):
    print(1)
else:
    print(s[i])

Buggy submission (verdict: Runtime Error):
s = input()
k = int(input())

i=0
while(s[i]=="1"):
    if i==99:
        break
    i +=1
    
if k<i:
    print(1)
else:
    print(s[i])

Buggy submission (verdict: Runtime Error):
s = input()
k = int(input())

i=0
while(s[i]=="1"):
    i +=1
    
if k<i:
    print(1)
else:
    print(s[i])

Buggy submission (verdict: Runtime Error):
s = input()
k = int(input())

i=0
while(s[i]=="1"):
    i +=1
print(s[i])
Problem p03283 (python)

Fixed submission:
n,m,Q = list(map(int, input().split()))

l=[]
r=[]
for j in range(m):
    l_tmp, r_tmp = list(map(int, input().split()))
    l.append(l_tmp)
    r.append(r_tmp)

p=[]
q=[]
for k in range(Q):
    p_tmp, q_tmp = list(map(int, input().split()))
    p.append(p_tmp)
    q.append(q_tmp)
    
su = [[0 for _ in range(n+1)] for __ in range(n+1)]

def sumTable(L, R):
    for i in range(m):
        su[L[i]][R[i]] += 1
    
    for i in range(1,n+1):
        for j in range(1, n+1):
            su[i][j] += su[i-1][j]
            su[i][j] += su[i][j-1]
            su[i][j] -= su[i-1][j-1]

def getSum(ll,rr):
    ans = su[rr][rr] - su[rr][ll-1] -su[ll-1][rr] + su[ll-1][ll-1]
    return ans
  
sumTable(l,r)

import copy
ss = copy.copy(su)

for qi in range(Q):
    su = copy.copy(ss)
    print(getSum(p[qi], q[qi]))

Buggy submission (verdict: Runtime Error):
for qi in range(Q):
    out = 0
    for i in range(m):
        if(l[i]>=p[qi] and r[i]<=q[qi]):
            out += 1
    print(out)
Problem p03361 (python)

Fixed submission:
h, w = list(map(int, input().split()))
s = [list(input()) for i in range(h)]
ss = [["." for i in range(w+2)] for j in range(h+2)]

for i in range(h):
    for j in range(w):
        ss[i+1][j+1]=s[i][j]

flag = 1

for i in range(h):
    for j in range(w):
        if ss[i+1][j+1]=="#":
            if ss[i][j+1]=="." and ss[i+2][j+1]=="." and ss[i+1][j]=="." and ss[i+1][j+2]==".":
                flag = 0
                break
                
if flag==1:
    print("Yes")
else:
    print("No")

Buggy submission (verdict: Runtime Error):
h, w = list(map(int, input().split()))
s = [list(input()) for i in range(h)]
ss = [["." for i in range(w+2)] for j in range(h+2)]

for i in range(w):
    for j in range(h):
        ss[i+1][j+1]=s[i][j]

flag = 1

for i in range(h):
    for j in range(w):
        if ss[i+1][j+1]=="#":
            if ss[i][j+1]=="." and ss[i+2][j+1]=="." and ss[i+1][j]=="." and ss[i+1][j+2]==".":
                flag = 0
                break
                
if flag==1:
    print("Yes")
else:
    print("No")
Problem p03371 (python)

Fixed submission:
a,b,c,x,y = list(map(int,input().split()))
for i in range(100001):
    price_new = i*2*c + max(0, x-i)*a + max(0, y-i)*b
    if i==0:
        price = price_new
    else:
        price = min(price, price_new)

print(price)

Buggy submission (verdict: Runtime Error):
for i in range(100000):
    price_new = i*2*c + max(0, x-i)*a + max(0, y-i)*b
    if i==0:
        price = price_new
    else:
        price = min(price, price_new)

print(price)
Problem p02642 (python)

Fixed submission:
import sys
import numpy as np
from collections import Counter


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(20000000)

MOD = 10 ** 9 + 7
INF = float("inf")


def main():
    N = int(input())
    A = list(map(int, input().split()))
    A.sort()
    max = A[-1]
    count = Counter(A)
    S = list(count.keys())
    C = []
    for s in S:
        if count[s] > 1:
            continue
        else:
            C.append(s)

    is_prime = np.ones(max + 1, np.bool)
    for a in A:
        if is_prime[a]:
            is_prime[a * 2 :: a] = 0
        else:
            continue
    answer = 0
    for c in C:
        if is_prime[c]:
            answer += 1
        else:
            continue
    print(answer)


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):

import sys
import numpy as np
from collections import Counter


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(20000000)

MOD = 10 ** 9 + 7
INF = float("inf")


def main():
    N = int(input())
    A = list(map(int, input().split()))
    A.sort()
    max = A[-1]
    count = Counter(A)
    S = list(count.keys())
    C = []
    for s in S:
        if count[s] > 1:
            continue
        else:
            C.append(s)

    is_prime = np.ones(max + 1, np.bool)
    for a in A:
        if is_prime[a]:
            is_prime[a * 2 :: a] = 0
        else:
            continue
    answer = 0
    for c in C:
        if is_prime[c]:
            answer += 1
        else:
            continue
    print(answer)


if __name__ == "__main__":
    main()
Problem p02684 (python)

Fixed submission:
import sys


def input():
    return sys.stdin.readline().strip()


def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    went = [0] * N
    went[0] = 1
    Flag = True
    check = [0]
    cnt = 1
    now = 0
    while Flag and (cnt <= K + 1):
        now = A[now] - 1
        if went[now] == 1:
            ind = check.index(now)
            if ind == 0:
                cnt = len(check)
            else:
                cnt = len(check) - ind

            Flag = False
        else:
            check.append(now)
            went[now] = 1
            cnt += 1
            continue

    if len(check) > K:
        print(check[K] + 1)
    else:
        if ind == 0:
            K = K % cnt
            print(check[K] + 1)
        else:
            ind -= 1
            K = (K - ind - 1) % cnt
            print(check[ind + K + 1] + 1)


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys


def input():
    return sys.stdin.readline().strip()


def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    went = [0] * N
    went[0] = 1
    Flag = True
    check = [0]
    cnt = 1
    now = 0
    while Flag and (cnt <= K):
        now = A[now] - 1
        if went[now] == 1:
            ind = check.index(now) - 1
            cnt = cnt - ind - 1
            Flag = False
        else:
            check.append(now)
            went[now] = 1
            cnt += 1
            continue
    if len(check) >= K:
        print(check[K] + 1)
    else:
        K = (K - ind) % cnt
        print(check[ind + K] + 1)


if __name__ == "__main__":
    main()
Problem p02702 (python)

Fixed submission:
import sys


def input():
    return sys.stdin.readline().strip()


def main():
    S = list(input())
    N = len(S)
    check = [0] * 2019
    ten = 1
    mod = 1
    check[mod] += 1
    for i in range(N):
        mod = (mod + int(S[N - i - 1]) * ten) % 2019
        ten = (ten * 10) % 2019
        check[mod] += 1
    answer = 0
    for i in range(2019):
        answer += ((check[i] - 1) * check[i]) // 2
    print(answer)


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys
from itertools import accumulate


def input():
    return sys.stdin.readline().strip()


MOD = 10 ** 9 + 7


def main():
    S = input()
    answer = 0
    for i in range(len(S)):
        func = lambda x, y: (int(x) * 10 + int(y)) % 2019
        A = list(accumulate(S[i:], func, initial=0))
        answer += A.count(0) - 1
    print(answer)


if __name__ == "__main__":
    main()
Problem p02714 (python)

Fixed submission:
import sys
from collections import Counter


def input():
    return sys.stdin.readline().strip()


def main():
    N = int(input())
    S = input()
    C = Counter(S)
    R = C.get("R", 0)
    G = C.get("G", 0)
    B = C.get("B", 0)
    ans = R * G * B
    for left in range(N):
        for mid in range(left + 1, N):
            right = mid + mid - left
            if right < N:
                if S[right] != S[mid] and S[mid] != S[left] and S[right] != S[left]:
                    ans -= 1
    print(ans)


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys
from collections import Counter


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(20000000)

MOD = 10 ** 9 + 7
INF = float("inf")


def main():
    N = int(input())
    S = input()
    C = Counter(S)
    r = C.get("R")
    g = C.get("G")
    b = C.get("B")
    answer = r * g * b
    for left in range(N):
        for mid in range(left + 1, N):
            right = mid * 2 - left
            if right < N:
                if S[right] != S[mid] and S[mid] != S[left] and S[right] != S[left]:
                    answer -= 1
    print(answer)


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys
from collections import Counter


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(20000000)

MOD = 10 ** 9 + 7
INF = float("inf")


def main():
    N = int(input())
    S = input()
    C = Counter(S)
    r = C.get("R")
    g = C.get("G")
    b = C.get("B")
    answer = r * g * b
    for left in range(N):
        for mid in range(left + 1, N):
            right = mid * 2 - left
            if right < N:
                if S[right] != S[mid] and S[mid] != S[left] and S[right] != S[left]:
                    answer = -1
    print(answer)


if __name__ == "__main__":
    main()
Problem p02743 (python)

Fixed submission:
from decimal import Decimal

a, b, c = map(int, input().split())
k = Decimal(0.5)

A = Decimal(a) ** k + Decimal(b) ** k
C = Decimal(c) ** k
if A < C:
    print("Yes")
else:
    print("No")

Buggy submission (verdict: Runtime Error):
from decimal import Decimal

a, b, c = map(int, input().split())


A = Decimal(a) ** (1 / 2) + Decimal(b) ** (1 / 2)
C = Decimal(c) ** (1 / 2)
if A < C:
    print("Yes")
else:
    print("No")
Problem p02744 (python)

Fixed submission:
N = int(input())
D = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
Ans = [["a"]]
for i in range(1, 10):
    before = Ans[i - 1]
    # print(before)
    next = []
    for j in before:
        L = set(list(j))
        # print(L)
        for k in range(len(L) + 1):
            # print(j)
            next.append(j + D[k])
    Ans.append(next)
# print(Ans)
print(*Ans[N - 1], sep="\n")

Buggy submission (verdict: Runtime Error):
N = int(input())
A = {
    1: ["a"],
    2: ["aa", "ab"],
    3: ["aaa", "aab", "aba", "abb", "abc"],
    4: [
        "aaaa",
        "aaab",
        "aabb",
        "aabc",
        "abaa",
        "abab",
        "abac",
        "abba",
        "abbb",
        "abbc",
        "abca",
        "abcb",
        "abcc",
        "abcd",
    ],
    5: [
        "aaaaa",
        "aaaab",
        "aaaba",
        "aaabb",
        "aaabc",
        "aabba",
        "aabbb",
        "aabbc",
        "aabca",
        "aabcb",
        "aabcc",
        "aabcd",
        "abaaa",
        "abaab",
        "abaac",
        "ababa",
        "ababb",
        "ababc",
        "abaca",
        "abacb",
        "abacc",
        "abacd",
        "abbaa",
        "abbab",
        "abbac",
        "abbba",
        "abbbb",
        "abbbc",
        "abbca",
        "abbcb",
        "abbcc",
        "abbcd",
        "abcaa",
        "abcab",
        "abcac",
        "abcad",
        "abcba",
        "abcbb",
        "abcbc",
        "abcbd",
        "abcca",
        "abccb",
        "abccc",
        "abccd",
        "abcda",
        "abcdb",
        "abcdc",
        "abcdd",
        "abcde",
    ],
}
Ans = A[N]
for i in range(len(Ans)):
    print(Ans[i])
Problem p02767 (python)

Fixed submission:
import numpy as np

N = int(input())
X = np.array(list(map(int, input().split())))
costs = []
for i in range(101):
    costs.append(np.sum((X - i) ** 2))
print(min(costs))

Buggy submission (verdict: Runtime Error):
N = int(input())
X = sorted(list(map(int, input().split())))
if N == 1:
    print(0)
else:
    r = X[-1] - X[0]
    ans = float("inf")
    if r == 0:
        print(0)
    else:
        for i in range(r):
            R = X[0] + i
            hp = 0
            for j in range(N):
                hp = hp + (R - X[j]) ** 2
 
            ans = min(hp, ans)
 
    print(ans)
N = int(input())
X = sorted(list(map(int, input().split())))
if N == 1:
    print(0)
else:
    r = X[-1] - X[0]
    ans = float("inf")
    for i in range(r+1):
      R = X[0] + i
      hp = 0
      for j in range(N):
        hp = hp + (R - X[j]) ** 2

        ans = min(hp, ans)

    print(ans)
提出情報

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

N = int(input())
X = np.array(list(map(int, input().split())))
cost = float("inf")
for i in range(101):
    cost = min(np.sum((X - i) ** 2))
print(cost)
Problem p02775 (python)

Fixed submission:
n = str(input())[::-1]
n = n + "0"
l = len(n)
ans = 0
flg = 0
check = 0

for i in range(l):
    check = int(n[i])
    check += flg
    if check == 5:
        if int(n[i + 1]) >= 5:
            ans += 5
            flg = 1
        else:
            ans += 5
            flg = 0
    elif check > 5:
        ans += 10 - check
        flg = 1
    else:
        ans += check
        flg = 0
print(ans + flg)

Buggy submission (verdict: Runtime Error):
n = str(input())[::-1]
n = "0" + n
l = len(n)
ans = 0
flg = 0
check = 0

for i in range(l):
    check = int(n[i])
    check += flg
    if check == 5:
        if int(n[i + 1]) >= 5:
            ans += 5
            flg = 1
        else:
            ans += 5
            flg = 0
    elif check > 5:
        ans += 10 - check
        flg = 1
    else:
        ans += check
        flg = 0
print(ans + flg)

Buggy submission (verdict: Runtime Error):
n = input()
l = len(n)
n = int(n)
ans = 0


def c(l, n, ans):

    a = n // (10 ** (l - 1))
    if l == 1:
        if a <= 5:
            return ans + a
        else:
            return ans + 1 + (10 - a)
    elif a == 5 and l > 1:
        if (n // (10 ** (l - 2))) % 10 <= 4:
            return c(l - 1, n % (10 ** (l - 1)), ans + a)
        else:
            return c(l, 10 ** l - n, ans + 1)
    elif a <= 4 and l > 1:
        return c(l - 1, n % (10 ** (l - 1)), ans + a)
    else:
        return c(l, 10 ** l - n, ans + 1)


print(c(l, n, ans))

Buggy submission (verdict: Runtime Error):
n = input()
l = len(n)
n = int(n)
ans = 0


def c(l, n, ans):
    print(ans)
    a = n // (10 ** (l - 1))
    if l == 1:
        if a <= 5:
            return ans + a
        else:
            return ans + 1 + (10 - a)
    elif a == 5 and l > 1:
        if (n // (10 ** (l - 2))) % 10 <= 4:
            return c(l - 1, n % (10 ** (l - 1)), ans + a)
        else:
            return c(l, 10 ** l - n, ans + 1)
    elif a <= 4 and l > 1:
        return c(l - 1, n % (10 ** (l - 1)), ans + a)
    else:
        return c(l, 10 ** l - n, ans + 1)


print(c(l, n, ans))