Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
K = int(input())

sevens = 0
count = 0
repeat = True

if {K%10} & {0,2,4,5,6,8}:
    count = -1
    repeat = False

while repeat:
    sevens = sevens * 10 + 7
    count += 1
    if sevens % K == 0:
        repeat = False

print(count)

Fixed submission:
K = int(input())

sevens = 0
count = 0
repeat = True

if {K%10} & {0,2,4,5,6,8}:
    count = -1
    repeat = False

while repeat:
    sevens = sevens % K * 10 + 7
    count += 1
    if sevens % K == 0:
        repeat = False

print(count)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = sorted(list(map(int, input().split())),reverse=True)
score = [A[0]]
s = 0

for a in A[1:N]:
    s += score[0]
    score = score[1:] + [a,a]

print(s)

Fixed submission:
import math
N = int(input())
A = sorted(list(map(int, input().split())),reverse=True)
s = A[0]

for i in range(1,N-1):
    s += A[math.ceil(i/2)]

print(s)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = sorted(list(map(int, input().split())),reverse=True)
score = [A[0]]
s = 0

for i in range(1,N):
    s += score[0]
    score = score[1:] + [A[i],A[i]]

print(s)

Fixed submission:
import math
N = int(input())
A = sorted(list(map(int, input().split())),reverse=True)
s = A[0]

for i in range(1,N-1):
    s += A[math.ceil(i/2)]

print(s)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = sorted(list(map(int, input().split())),reverse=True)
ring = [A[0]]
score = [A[0]] #ringのi-1とiの間に割り込んだ時に入るフレンドリーさ
s = 0

for i in range(1,N):
    s += max(score)
    j = score.index(max(score))
    ring.insert(j,A[i])
    score.insert(j,A[i])
    score[j+1] = A[i]
    # print("ring")
    # print(ring)
    # print(score)
    # print(s)

print(s)

Fixed submission:
import math
N = int(input())
A = sorted(list(map(int, input().split())),reverse=True)
s = A[0]

for i in range(1,N-1):
    s += A[math.ceil(i/2)]

print(s)

Buggy python submission (verdict: Runtime Error):
N = int(input())
A = sorted(list(map(int, input().split())),reverse=True)
ring = [A[0]]
score = [A[0]]
s = 0

for i in A[1:N]:
    s += max(score)
    j = score.index(max(score))
    ring.insert(j,A[i])
    score.insert(j,A[i])
    score[j-1] = A[i]

print(s)

Fixed submission:
import math
N = int(input())
A = sorted(list(map(int, input().split())),reverse=True)
s = A[0]

for i in range(1,N-1):
    s += A[math.ceil(i/2)]

print(s)
Buggy python submission (verdict: Time Limit Exceeded):
import math

N = int(input())
s = 0

def devisor(x):
    count = 0
    for i in range(1,math.floor(math.sqrt(x))+1):
        if (x % i) == 0:
            count += 2
            if i**2 == x:
                count -= 1
    return count

for i in range(1, N+1):
    s += i * devisor(i)

print(s)

Fixed submission:
import math

N = int(input())
s = 0

# def devisor(x):
#     count = 0
#     for i in range(1,math.floor(math.sqrt(x))+1):
#         if (x % i) == 0:
#             count += 2
#             if i**2 == x:
#                 count -= 1
#     return count

# for i in range(1, N+1):
#     s += i * devisor(i)

for i in range(1,N+1):
    y = math.floor(N/i)
    s += y*(y+1)*i//2

print(s)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
bucket = [0]*100001

for i in range(100000):
    bucket[i] = A.count(i)

sum = sum(A)

for i in range(Q):
    b, c = list(map(int, input().split()))
    sum += (c - b) * bucket[b]
    bucket[c] += bucket[b]
    bucket[b] = 0
    print(sum)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())

B, C = [0]*Q, [0]*Q
for i in range(Q):
    B[i], C[i] = list(map(int, input().split()))

bucket = [0]*100001
for i in A:
    bucket[i] += 1

sum = sum(A)

for i in range(Q):
    sum += (C[i] - B[i]) * bucket[B[i]]
    bucket[C[i]] += bucket[B[i]]
    bucket[B[i]] = 0
    print(sum)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
B = [0]*(N+1)
S = 0

for i in range(1, N+1):
    B[i] = A.count(i)
    S += B[i]*(B[i]-1)//2

for k in range(N):
    print(S-B[A[k]]+1)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
B = [0]*(N+1)
S = 0

for i in range(N):
    B[A[i]] += 1

for i in range(1, N+1):
    S += B[i]*(B[i]-1)//2

for k in range(N):
    print(S-B[A[k]]+1)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
B = [0]*(N+1)
S = 0

for i in range(1, N+1):
    B[i] = A.count(i)
    S += int(B[i]*(B[i]-1)/2)

for i in range(N):
    print(S-B[A[i]]+1)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
B = [0]*(N+1)
S = 0

for i in range(N):
    B[A[i]] += 1

for i in range(1, N+1):
    S += B[i]*(B[i]-1)//2

for k in range(N):
    print(S-B[A[k]]+1)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
B = [0]*(N+1)
S = 0

for i in range(1, N+1):
    B[i] = A.count(i)
    S += int(B[i]*(B[i]-1)/2)

for i in range(N):
    if B[A[i]] < 2:
        print(S)
    else:
        print(S-B[A[i]]+1)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
B = [0]*(N+1)
S = 0

for i in range(N):
    B[A[i]] += 1

for i in range(1, N+1):
    S += B[i]*(B[i]-1)//2

for k in range(N):
    print(S-B[A[k]]+1)

Buggy python submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split()))
B = []
S = 0

for i in range(1, N+1):
    B[i] = A.count(i)
    S += B[i]*(B[i]-1)/2

for i in range(N):
    if B[A[i]] < 2:
        print(S)
    else:
        print(S-B[A[i]]+1)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
B = [0]*(N+1)
S = 0

for i in range(N):
    B[A[i]] += 1

for i in range(1, N+1):
    S += B[i]*(B[i]-1)//2

for k in range(N):
    print(S-B[A[k]]+1)

Buggy python submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split()))
B = []
S = 0

for i in range(1, N+1):
	B[i] = A.count(i)
	S += B[i]*(B[i]-1)/2

for i in range(N):
	if B[A[i]] < 2:
		print(S)
	else:
		print(S-B[A[i]]+1)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
B = [0]*(N+1)
S = 0

for i in range(N):
    B[A[i]] += 1

for i in range(1, N+1):
    S += B[i]*(B[i]-1)//2

for k in range(N):
    print(S-B[A[k]]+1)
Buggy python submission (verdict: Time Limit Exceeded):
N, M, K = list(map(int, input().split()))
p = [0] * N # p[][0] 友達リスト　p[][1] ブロックリスト p[][2] 所属グループ(-1はなし)
for i in range(N):
    p[i] = [0]*3
    p[i][0] = []
    p[i][1] = []
    p[i][2] = -1
g = [] # グループのサイズ
stack = []
groupNum = 0

for i in range(M):
    a, b = list(map(int, input().split()))
    p[a-1][0].append(b-1)
    p[b-1][0].append(a-1)

for i in range(K):
    c, d = list(map(int, input().split()))
    p[c-1][1].append(d-1)
    p[d-1][1].append(c-1)

def groupmake(person,group):
    groupSize = 0
    stack.append(person)
    p[person][2] = group
    while True:
        num = stack.pop()
        groupSize += 1
        for i in p[num][0]:
            if p[i][2] == -1:
                stack.append(i)
                p[i][2] = group
        if len(stack) == 0:
            return groupSize

for i in range(len(p)):
    if p[i][2] == -1:
        g.append(groupmake(i, groupNum))
        groupNum += 1

answer = ""

for person in p:
    fc = g[person[2]] - len(person[0]) -1
    for block in person[1]:
        if p[block][2] == person[2]:
            fc -= 1
    answer = answer + str(fc) + " "

print(answer.strip())

Fixed submission:
N, M, K = list(map(int, input().split()))
p = [0] * N # p[][0] 友達リスト　p[][1] ブロックリスト p[][2] 所属グループ(-1はなし)
for i in range(N):
    p[i] = [0]*3
    p[i][0] = []
    p[i][1] = []
    p[i][2] = -1
g = [] # グループのサイズ
stack = []
groupNum = 0
fcs = []

for i in range(M):
    a, b = list(map(int, input().split()))
    p[a-1][0].append(b-1)
    p[b-1][0].append(a-1)

def groupmake(person,group):
    groupSize = 0
    stack.append(person)
    p[person][2] = group
    while True:
        num = stack.pop()
        groupSize += 1
        for i in p[num][0]:
            if p[i][2] == -1:
                stack.append(i)
                p[i][2] = group
        if len(stack) == 0:
            return groupSize

for i in range(len(p)):
    if p[i][2] == -1:
        g.append(groupmake(i, groupNum))
        groupNum += 1

for person in p:
    fcs.append(g[person[2]] - len(person[0]) -1)

for i in range(K):
    c, d = list(map(int, input().split()))
    if p[c-1][2] == p[d-1][2]:
        fcs[c-1] -= 1
        fcs[d-1] -= 1

print(" ".join(map(str, fcs)))
Buggy python submission (verdict: Time Limit Exceeded):
N, M = list(map(int, input().split()))

p = [0]*M
s = [0]*M
b = [0]*N
pena = 0
corr = 0

for i in range(N):
    b[i] = []

for i in range(M):
    p[i], s[i] = input().split()
    p[i] = int(p[i])
    if not (set(b[p[i]-1]) & {"AC"}):
        if s[i] == "AC":
            pena += len(b[p[i]-1])
            corr += 1
    b[p[i]-1].append(s[i])

print(corr, pena)


Fixed submission:
N, M = list(map(int, input().split()))

p = [0]*M
s = [0]*M
b = [0]*N
pena = 0
corr = 0

for i in range(M):
    p[i], s[i] = input().split()
    p[i] = int(p[i])
    if b[p[i]-1] != -1:
        if s[i] == "AC":
            pena += b[p[i]-1]
            corr += 1
            b[p[i]-1] = -1
        else:
            b[p[i]-1] += 1

print(corr, pena)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
L = list(map(int, input().split()))
count = 0

for i in range(N-2):
    for j in range(i+1,N-1):
        for k in range(j+1,N):
            if L[i] < L[j]+L[k] and L[j] < L[k]+L[i] and L[k] < L[i]+L[j]:
                count += 1

print(count)

Fixed submission:
import bisect
N = int(input())
L = list(map(int, input().split()))
count = 0

L.sort()

for i in range(N-2):
    for j in range(i+1,N-1):
        k = bisect.bisect_left(L,L[i]+L[j])
        count += k-j-1

print(count)
Buggy python submission (verdict: Time Limit Exceeded):
import math

A, B = list(map(int, input().split()))
count = 0

def divisor(x):
    divs = []
    for i in range(1, math.floor(math.sqrt(x))+1):
        if x%i == 0:
            divs.append(i)
            if i**2 != x:
                divs.append(x//i)
    return divs

codiv = set(divisor(A)) & set(divisor(B))

for i in codiv:
    if len(divisor(i)) <= 2:
        count += 1

print(count)


Fixed submission:
import math

A, B = list(map(int, input().split()))
count = 0

def divisor(x):
    divs = []
    for i in range(1, math.floor(math.sqrt(x))+1):
        if x%i == 0:
            divs.append(i)
            if i**2 != x:
                divs.append(x//i)
    return divs

def evPrimal(x):
    if x < 4:
        return True
    divs = []
    for i in range(2, math.floor(math.sqrt(x))+1):
        if x%i == 0:
            return False
    return True

codiv = set(divisor(A)) & set(divisor(B))

for i in codiv:
    if evPrimal(i):
        count += 1

print(count)
Buggy python submission (verdict: Time Limit Exceeded):
N, K, Q = list(map(int, input().split()))
A = []
for i in range(Q):
    A.append(int(input()))

for i in range(N):
    if K + A.count(i+1) - Q > 0:
        print('Yes')
    else:
        print('No')

Fixed submission:
N, K, Q = list(map(int, input().split()))

scores = [K-Q] * N
for i in range(Q):
    scores[int(input()) - 1] += 1

for score in scores:
    if score > 0:
        print('Yes')
    else:
        print('No')
Buggy python submission (verdict: Time Limit Exceeded):
N, M = list(map(int,input().split()))
A = sorted(list(map(int,input().split())), reverse=True)
B = []

B.append(A.pop(0)//2)
for i in range(1,M):
    if A == []:
        B[0] = B[0]//2
        B = sorted(B, reverse=True)
    else:
        if A[0] > B[0]:
            B.append(A.pop(0)//2)
        else:
            B[0] = B[0]//2
            B = sorted(B, reverse=True)

print(sum(A)+sum(B))

Fixed submission:
N, M = list(map(int,input().split()))
A = sorted(list(map(int,input().split())), reverse=True)
b = []

b.append(A.pop(0)//2)
for i in range(1,M):
    if A == []:
        b.append(b.pop(0)//2)
    else:
        if A[0] > b[0]:
            b.append(A.pop(0)//2)
        else:
            b.append(b.pop(0)//2)

print(sum(A)+sum(b))
Buggy python submission (verdict: Time Limit Exceeded):
X, Y, Z, K = list(map(int, input().split()))
A = sorted(list(map(int, input().split())), reverse=True)
B = sorted(list(map(int, input().split())), reverse=True)
C = sorted(list(map(int, input().split())), reverse=True)
s = []

for i in A:
    for j in B:
        for k in C:
            s.append(i+j+k)

s = sorted(s, reverse=True)

for i in range(K):
    print(s[i])

Fixed submission:
X, Y, Z, K = list(map(int, input().split()))
A = sorted(list(map(int, input().split())), reverse=True)
B = sorted(list(map(int, input().split())), reverse=True)
C = sorted(list(map(int, input().split())), reverse=True)
e = []
s = []

for i in A:
    for j in B:
        e.append(i+j)
e = sorted(e, reverse=True)

if K<len(e):
    e = e[0:K]

for i in e:
    for j in C:
        s.append(i+j)

s = sorted(s, reverse=True)

for i in range(K):
    print(s[i])

Buggy python submission (verdict: Runtime Error):
X, Y, Z, K = list(map(int, input().split()))
A = sorted(list(map(int, input().split())), reverse=True)
B = sorted(list(map(int, input().split())), reverse=True)
C = sorted(list(map(int, input().split())), reverse=True)
s = []

for i in A:
    for j in B:
        for k in C:
            s.append(i+j+k)

s = sorted(s, reverse=True)

for i in range(K):
    print s[i]

Fixed submission:
X, Y, Z, K = list(map(int, input().split()))
A = sorted(list(map(int, input().split())), reverse=True)
B = sorted(list(map(int, input().split())), reverse=True)
C = sorted(list(map(int, input().split())), reverse=True)
e = []
s = []

for i in A:
    for j in B:
        e.append(i+j)
e = sorted(e, reverse=True)

if K<len(e):
    e = e[0:K]

for i in e:
    for j in C:
        s.append(i+j)

s = sorted(s, reverse=True)

for i in range(K):
    print(s[i])

Buggy python submission (verdict: Runtime Error):
X, Y, Z, K = list(map(int, input().split()))
A = sorted(list(map(int, input().split())), reverse=True)
B = sorted(list(map(int, input().split())), reverse=True)
C = sorted(list(map(int, input().split())), reverse=True)
s = []

for i in A:
	for j in B:
		for k in C:
			s.append(i+j+k)

s = sorted(s, reverse=True)

for i in range(K):
	print s[i]

Fixed submission:
X, Y, Z, K = list(map(int, input().split()))
A = sorted(list(map(int, input().split())), reverse=True)
B = sorted(list(map(int, input().split())), reverse=True)
C = sorted(list(map(int, input().split())), reverse=True)
e = []
s = []

for i in A:
    for j in B:
        e.append(i+j)
e = sorted(e, reverse=True)

if K<len(e):
    e = e[0:K]

for i in e:
    for j in C:
        s.append(i+j)

s = sorted(s, reverse=True)

for i in range(K):
    print(s[i])
Buggy python submission (verdict: Time Limit Exceeded):

def gcd(a, b):
    while a>0 and b >0:
        if a > b:
            a %= b
        else:
            b %= a
    return a + b

N = int(input())
A = list(map(int, input().split()))

ans = 0
for i in range(N):
    a = A[1] if i == 0 else A[0]
    for j in range(N):
        if i == j:
            continue
        a = gcd(a, A[j])
    if a > ans:
        ans = a

print(ans)


Fixed submission:
def gcd(a, b):
    while a>0 and b >0:
        if a > b:
            a %= b
        else:
            b %= a
    return a + b

N = int(input())
A = list(map(int, input().split()))


forwards = [A[0]]
backwards = [A[-1]]
for i in range(1, N):
    forwards.append(gcd(forwards[-1], A[i]))
    backwards.append(gcd(backwards[-1], A[-(i+1)]))
backwards = list(reversed(backwards))

ans = 0
for i in range(1, N-1):
    a = gcd(forwards[i-1], backwards[i+1])
    if a > ans:
        ans = a

if N > 2:
    ans = max([ans, forwards[-2], backwards[1]])
elif N == 2:
    ans = max(A)
else:
    ans = A[0]

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n,x,y = map(int,input().split())

import numpy as np
from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson
from scipy.sparse import csr_matrix

l = np.zeros((n,n),dtype=int)

for i in range(n-1):
    l[i][i+1] = 1
    l[i+1][i] = 1

l[x-1][y-1] = 1
l[y-1][x-1] = 1

a = shortest_path(l)
# print(a)
b = np.triu(a)
# print(b)
c = b.reshape(1,n**2)
# print(c[0])
d = list(map(int,c[0]))

e = [ i for i in d if i!=0 ]
print(n)
for i in range(2,n):
    print(e.count(i))

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

l = [0]*n

x -= 1
y -= 1

for i in range(n):
    for j in range(i+1,n):
        a = j - i
        if i<=x:
            b1 = x - i
        else:
            b1 = i - x
        if j<=y:
            b2 = y - j
        else:
            b2 = j - y
        b = b1 + b2 + 1
        l[min(a,b)] += 1

for i in range(1,n):
    print(l[i])

Buggy python submission (verdict: Time Limit Exceeded):
n,x,y = map(int,input().split())

import numpy as np
from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson
from scipy.sparse import csr_matrix

l = np.zeros((n,n),dtype=int)
l = np.array(l)

for i in range(n-1):
    l[i][i+1] = 1
    l[i+1][i] = 1

l[x-1][y-1] = 1
l[y-1][x-1] = 1

a = shortest_path(l)

b = np.triu(a)

c = b.reshape(1,n**2)

d = list(map(int,c[0]))

e = [ i for i in d if i!=0 ]

for i in range(1,n):
    print(e.count(i))

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

l = [0]*n

x -= 1
y -= 1

for i in range(n):
    for j in range(i+1,n):
        a = j - i
        if i<=x:
            b1 = x - i
        else:
            b1 = i - x
        if j<=y:
            b2 = y - j
        else:
            b2 = j - y
        b = b1 + b2 + 1
        l[min(a,b)] += 1

for i in range(1,n):
    print(l[i])
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int,input().split()))

import collections

for k in range(n):
    b = a[:k]
    c = a[k+1:]
    d = b + c
    e = collections.Counter(d)
    count = 0
    for i in e.keys():
        m = e[i]
        count += m*(m-1)//2
    print(count)

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

import collections

e = collections.Counter(a)

count = 0
for i in e.keys():
    m = e[i]
    count += m*(m-1)//2

for k in range(n):
    b = a[k]
    c = e[b] - 1
    ans = count - c
    print(ans)

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

import collections

for k in range(n):
    b = a[:k]
    c = a[k+1:]
    d = b + c
    e = collections.Counter(d)
    count = 0
    for i in e.keys():
        m = e[i]
        count += m*(m-1)//2
    print(count)

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

import collections

e = collections.Counter(a)

count = 0
for i in e.keys():
    m = e[i]
    count += m*(m-1)//2

for k in range(n):
    b = a[k]
    c = e[b] - 1
    ans = count - c
    print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
S = [ input() for _ in range(n) ]

import collections

c = collections.Counter(S)

ls_ans = []
ls_ans.append( c.most_common()[0][0] )

a = c.most_common()[0][1]
for i in range(1,len(c)):
    if c.most_common()[i][1]==a:
        ls_ans.append(c.most_common()[i][0])
    else:
        break
ans = sorted(ls_ans)
print("\n".join(ans))
exit()

Fixed submission:
n = int(input())
S = [ input() for _ in range(n) ]

import collections

c = collections.Counter(S)
b = c.most_common()

ls_ans = []
ls_ans.append( b[0][0] )

a = b[0][1]

for i in range(1,len(b)):
    if b[i][1]==a:
        ls_ans.append(b[i][0])
    else:
        break
ans = sorted(ls_ans)
print("\n".join(ans))
exit()

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
S = [ input() for _ in range(n) ]

import collections

c = collections.Counter(S)

ls_ans = []
ls_ans.append( c.most_common()[0][0] )

a = c.most_common()[0][1]
for i in range(1,len(c)):
    if c.most_common()[i][1]==a:
        ls_ans.append(c.most_common()[i][0])
    else:
        break
ans = sorted(ls_ans)
print("\n".join(ans))
exit()

Fixed submission:
n = int(input())
S = [ input() for _ in range(n) ]

import collections

c = collections.Counter(S)
b = c.most_common()

ls_ans = []
ls_ans.append( b[0][0] )

a = b[0][1]

for i in range(1,len(b)):
    if b[i][1]==a:
        ls_ans.append(b[i][0])
    else:
        break
ans = sorted(ls_ans)
print("\n".join(ans))
exit()

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
S = [ input() for _ in range(n) ]

# print(S)

import collections

c = collections.Counter(S)

# print(c)
# print(c.most_common())
# print(c.most_common()[0][0])

ls_ans = []
ls_ans.append( c.most_common()[0][0] )

a = c.most_common()[0][1]
for i in range(1,len(c)):
    if c.most_common()[i][1]==a:
        ls_ans.append(c.most_common()[i][0])

ans = sorted(ls_ans)

print("\n".join(ans))

Fixed submission:
n = int(input())
S = [ input() for _ in range(n) ]

import collections

c = collections.Counter(S)
b = c.most_common()

ls_ans = []
ls_ans.append( b[0][0] )

a = b[0][1]

for i in range(1,len(b)):
    if b[i][1]==a:
        ls_ans.append(b[i][0])
    else:
        break
ans = sorted(ls_ans)
print("\n".join(ans))
exit()
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
Nr = list(reversed(list(map(int,str(n)))))
Nr.append(0)

ans = 0 
for i in range(len(Nr)):
    if Nr[i] > 5:
        Nr[i+1] += 1
        ans += (10 - Nr[i])
    elif Nr[i]==5:
        if Nr[i+1]>=5:
            Nr[i+1] += 1
            ans += (10 - Nr[i])
        else:
            ans += Nr[i]
    else:
        ans += Nr[i]


print(ans)

Fixed submission:
n = input()
Nr = list(map(int,list(reversed(n))))
Nr.append(0)

ans = 0 
for i in range(len(Nr)):
    if Nr[i] > 5:
        Nr[i+1] += 1
        ans += (10 - Nr[i])
    elif Nr[i]==5:
        if Nr[i+1]>=5:
            Nr[i+1] += 1
            ans += (10 - Nr[i])
        else:
            ans += Nr[i]
    else:
        ans += Nr[i]

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
Nr = list(reversed(list(map(int,str(n)))))
Nr.append(0)

ans = 0 
for i in range(len(Nr)):
    if i<(10**100):
        if Nr[i] > 5:
            Nr[i+1] += 1
            ans += (10 - Nr[i])
        elif Nr[i]==5:
            if Nr[i+1]>=5:
                Nr[i+1] += 1
                ans += (10 - Nr[i])
            else:
                ans += Nr[i]
        else:
            ans += Nr[i]
    elif i==(10**100):
        A = Nr[i:]
        B = list(map(str,A))
        C = "".join(B)
        D = int(C)
        ans += D
    else:
        break

print(ans)

Fixed submission:
n = input()
Nr = list(map(int,list(reversed(n))))
Nr.append(0)

ans = 0 
for i in range(len(Nr)):
    if Nr[i] > 5:
        Nr[i+1] += 1
        ans += (10 - Nr[i])
    elif Nr[i]==5:
        if Nr[i+1]>=5:
            Nr[i+1] += 1
            ans += (10 - Nr[i])
        else:
            ans += Nr[i]
    else:
        ans += Nr[i]

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())

N = list(map(int,str(n)))

# print(N)

Nr = list(reversed(N))
Nr.append(0)
# print(Nr)

ans = 0 
for i in range(len(Nr)):
    if i<(10**100):
        if Nr[i] > 5:
            Nr[i+1] += 1
            ans += (10 - Nr[i])
        elif Nr[i]==5:
            if Nr[i+1]>=5:
                Nr[i+1] += 1
                ans += (10 - Nr[i])
            else:
                ans += Nr[i]
        else:
            ans += Nr[i]
    elif i==(10**100):
        A = Nr[i:]
        B = list(map(str,A))
        C = "".join(B)
        D = int(C)
        ans += D
    else:
        break
print(ans)

Fixed submission:
n = input()
Nr = list(map(int,list(reversed(n))))
Nr.append(0)

ans = 0 
for i in range(len(Nr)):
    if Nr[i] > 5:
        Nr[i+1] += 1
        ans += (10 - Nr[i])
    elif Nr[i]==5:
        if Nr[i+1]>=5:
            Nr[i+1] += 1
            ans += (10 - Nr[i])
        else:
            ans += Nr[i]
    else:
        ans += Nr[i]

print(ans)