qyliang/CodeNanoFix huggingface dataset:
Buggy code:
import numpy as np
N, K = map(int, input().split())
L = np.array(list(map(int, input().split())))

L = np.sort(L)
pos = L[0<L]
neg = L[0>L]
zero = len(L[L==0])

def f(x):
  tmp = 0
  if x>=0:
    tmp += zero*N
  tmp += np.searchsorted(L, x//pos, side='right').sum()
  tmp += (N - np.searchsorted(L, x//neg, side='left')).sum()
  tmp -= np.count_nonzero(L*L<=x)
  return tmp//2

l, r = -10**18, 10**18
while l<r-1:
  m = (l+r)//2
  count = f(m)
  if count >= K:
    r = m
  else:
    l = m

print(r)

Fixed code:
import numpy as np
N, K = map(int, input().split())
L = np.array(list(map(int, input().split())))

L = np.sort(L)
pos = L[0<L]
neg = L[0>L]
zero = len(L[L==0])

def f(x):
  tmp = 0
  if x>=0:
    tmp += zero*N
  tmp += np.searchsorted(L, x//pos, side='right').sum()
  tmp += (N - np.searchsorted(L, -(-x//neg), side='left')).sum()
  tmp -= np.count_nonzero(L*L<=x)
  return tmp//2

l, r = -10**18, 10**18
while l<r-1:
  m = (l+r)//2
  count = f(m)
  if count >= K:
    r = m
  else:
    l = m

print(r)
Buggy code:
import numpy as np
import sys

n,k=map(int,sys.stdin.readline().split())
a=np.array(sys.stdin.readline().split(),dtype=int)

a.sort()
neg=a[a<0]
zero=a[a==0]
pos=a[a>0]

def count(x):
    cnt=0
    if x>=0:
        cnt+=len(zero)
    cnt+=np.searchsorted(a,x//pos,side='right').sum()
    cnt+=(n-np.searchsorted(a,(-x-1)//(-neg),side='right')).sum()
    cnt-=np.count_nonzero(a*a<=x)
    return cnt//2


left=-10**18
right=10**18

while left+1<right:
    x=(left+right)//2
    cnt=count(x)
    if cnt>=k:
        right=x
    if cnt<k:
        left=x

print(right)


Fixed code:
import numpy as np
import sys
 
n,k=map(int,sys.stdin.readline().split())
a=np.array(list(map(int, input().split())))
 
a.sort()
neg=a[a<0]
zero=a[a==0]
pos=a[a>0]
 
def count(x):
    cnt=0
    if x>=0:
        cnt+=len(zero)*n
    cnt+=np.searchsorted(a,x//pos,side='right').sum()
    cnt+=(n-np.searchsorted(a,(-x-1)//(-neg),side='right')).sum()
    cnt-=np.count_nonzero(a*a<=x)
    return cnt//2
 
 
left=-10**18
right=10**18
 
while left+1<right:
    x=(left+right)//2
    out=count(x)
    if out>=k:
        right=x
    if out<k:
        left=x
print(right)
Buggy code:
import numpy as np
import sys

n,k,*a=map(int,sys.stdin.readlines().split())
a=np.array(a,dtype=np.int64)

print(n,k,a)

a.sort()
neg=a[a<0]
zero=a[a==0]
pos=a[a>0]

print(np.searchsorted(a,0//pos,side='right').sum())

def count(x):
    cnt=0
    if x>=0:
        cnt+=len(zero)
    cnt+=np.searchsorted(a,x//pos,side='right').sum()
    cnt+=(n-np.searchsorted(a,(-x-1)//(-neg),side='right')).sum()
    cnt-=np.count_nonzero(a*a<=x)
    return cnt//2


left=-10**18
right=10**18

while left+1<right:
    x=(left+right)//2
    cnt=count(x)
    if cnt>=k:
        right=x
    if cnt<k:
        left=x

print(right)


Fixed code:
import numpy as np
import sys

n,k=map(int,sys.stdin.readline().split())
a=np.array(sys.stdin.readline().split(),dtype=np.int64)

a.sort()
neg=a[a<0]
zero=a[a==0]
pos=a[a>0]

def count(x):
    cnt=0
    if x>=0:
        cnt+=len(zero)*n
    cnt+=np.searchsorted(a,x//pos,side='right').sum()
    cnt+=(n-np.searchsorted(a,(-x-1)//(-neg),side='right')).sum()
    cnt-=np.count_nonzero(a*a<=x)
    return cnt//2


left=-10**18
right=10**18

while left+1<right:
    x=(left+right)//2
    out=count(x)
    if out>=k:
        right=x
    if out<k:
        left=x

print(right)
Buggy code:
import numpy as np

N,K = [int(i) for i in input().split()]
n = np.array([int(i) for i in input().split()])
n=np.sort(n)

mn = n[np.where(n<0)]
mz = n[np.where(n==0)]
mp = n[np.where(n>0)]

M = 10**18
m = -1*M-1
pos = 0

while M-m > 1:
    a = 0
    pos=(M+m)//2
    a+=np.searchsorted(n,pos//mp,side="right").sum()
    a+=cz*N
    a+=N*len(mn) - np.searchsorted(n,-(-pos//mn),side="left").sum()
    a-=np.count_nonzero(n*n <= pos)
    a=a//2
    if a >= K:
        M=pos
    else:
        m=pos
#print(M,m)
print(M)


Fixed code:
import numpy as np

N,K = [int(i) for i in input().split()]
n = np.array([int(i) for i in input().split()])
n=np.sort(n)

mn = n[np.where(n<0)]
mz = n[np.where(n==0)]
mp = n[np.where(n>0)]
M = 10**18
m = -1*M-1
pos = 0

while M-m > 1:
    a = 0
    pos=(M+m)//2
    a+=np.searchsorted(n,pos//mp,side="right").sum()
    if pos >= 0: a+=len(mz)*N
    a+=N*len(mn) - np.searchsorted(n,-(-pos//mn),side="left").sum()
    a-=np.count_nonzero(n*n <= pos)
    a=a//2
    if a >= K:
        M=pos
    else:
        m=pos
        
print(M)
Buggy code:
import numpy as np

N,K = [int(i) for i in input().split()]
n = np.array([int(i) for i in input().split()])
n=np.sort(n)

mn = n[np.where(n<0)]
mz = n[np.where(n==0)]
mp = n[np.where(n>0)]

#cn = len(mn)
#cp = len(mp)
#cz = len(mz)
#tcn = cn*cp

#if cn*cp < K <= cn*cp + (N-cz)*cz + cz*(cz-1)//2:
#    print(0)
#    exit(0)

M = 10**18
m = -1*M-1
pos = 0

while M-m > 1:
    a = 0
    pos=(M+m)//2
    a+=np.searchsorted(n,pos//mp,side="right").sum()
    a+=cz*N
    a+=N*len(mn) - np.searchsorted(n,-(-pos//mn),side="left").sum()
    a-=np.count_nonzero(n*n <= pos)
    a=a//2
    if a >= K:
        M=pos
    else:
        m=pos
#print(M,m)
print(M)


Fixed code:
import numpy as np

N,K = [int(i) for i in input().split()]
n = np.array([int(i) for i in input().split()])
n=np.sort(n)

mn = n[np.where(n<0)]
mz = n[np.where(n==0)]
mp = n[np.where(n>0)]

#cn = len(mn)
##cp = len(mp)
#cz = len(mz)
#tcn = cn*cp

#if cn*cp < K <= cn*cp + (N-cz)*cz + cz*(cz-1)//2:
#    print(0)
#    exit(0)

M = 10**18
m = -1*M-1
pos = 0

while M-m > 1:
    a = 0
    pos=(M+m)//2
    a+=np.searchsorted(n,pos//mp,side="right").sum()
    if pos >= 0: a+=len(mz)*N
    a+=N*len(mn) - np.searchsorted(n,-(-pos//mn),side="left").sum()
    a-=np.count_nonzero(n*n <= pos)
    a=a//2
    if a >= K:
        M=pos
    else:
        m=pos
#print(M,m)
print(M)
Buggy code:
def main():
    from bisect import bisect_left as bl

    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    a.sort()

    inf = 10**18+1

    ok = -inf
    ng = inf
    start_0 = bl(a, 0)
    start_1 = bl(a, 1)

    while abs(ok-ng) > 1:
        mid = (ok+ng)//2
        cnt = 0
        now = n
        for i in a[:start_0]:
            while now > 0:
                if i*a[now-1] < mid:
                    now -= 1
                else:
                    break
            cnt += n - now
        now = n
        for i in a[start_1:]:
            while now > 0:
                if i*a[now-1] >= mid:
                    now -= 1
                else:
                    break
            cnt += now
        if mid > 0:
            cnt += n * (start_1 - start_0)
        cnt -= len([None for i in a if i*i < mid])
        if cnt//2 < k:
            ok = mid
        else:
            ng = mid
    print(ok)


main()


Fixed code:
def main():
    from bisect import bisect_left as bl

    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    a.sort()

    inf = 10**18+1

    ok = -inf
    ng = inf
    start_0 = bl(a, 0)
    start_1 = bl(a, 1)

    while abs(ok-ng) > 1:
        mid = (ok+ng)//2
        cnt = 0
        if mid >= 0:
            now = 0
            if start_0 > 0:
                for i in a[start_0-1::-1]:
                    while now < n:
                        if i*a[now] >= mid:
                            now += 1
                        else:
                            break
                    cnt += n - now
            now = n
            for i in a[start_1:]:
                while now > 0:
                    if i*a[now-1] >= mid:
                        now -= 1
                    else:
                        break
                cnt += now
        else:
            now = 0
            for i in a[:start_0]:
                while now < n:
                    if i*a[now] >= mid:
                        now += 1
                    else:
                        break
                cnt += n - now
            now = n
            for i in a[n-1:start_1-1:-1]:
                while now > 0:
                    if i*a[now-1] >= mid:
                        now -= 1
                    else:
                        break
                cnt += now
        if mid > 0:
            cnt += n * (start_1 - start_0)
        cnt -= len([None for i in a if i*i < mid])
        if cnt//2 < k:
            ok = mid
        else:
            ng = mid
    print(ok)


main()
Buggy code:
import numpy as np

n, k = map(int, input().split())
A = np.array(input().split(), np.int64)

A.sort()

posA = A[A>0]
negA = A[A<0]
zeros = (A==0).sum()

const = 10**18
left = -(const+1)
right = const+1

while left+1 < right:
    num = (left+right)//2
    
    less_count = 0
    
    if num > 0:
        less_count += zeros * n
    
    limit_pair_for_pos = num / posA
    limit_idxs = np.searchsorted(A, limit_pair_for_pos, side='left')
    less_count += limit_idxs.sum()
    
    limit_pair_for_neg = num / negA
    limit_idxs = n - np.searchsorted(A, limit_pair_for_neg, side='right')
    less_count += limit_idxs.sum()
    
    duplicate_cases = (A**2 < num).sum()
    less_count -= duplicate_cases
    less_count //= 2
    
    if less_count < k:
        left = num
    else:
        right = num

print(left)

Fixed code:
import numpy as np

n, k = map(int, input().split())

A = np.array(input().split(), np.int64)

A.sort()

posA = A[A > 0]
negA = A[A < 0]
zeros = (A==0).sum()

left = -10**18
right = 10**18

while left+1 < right:
    num = (left+right)//2
    
    less_count = 0
    
    if num >= 0:
        less_count += zeros * n
    
    limit_pair_for_pos = num // posA
    limit_idxs = np.searchsorted(A, limit_pair_for_pos, side='right')
    less_count += limit_idxs.sum()

    limit_pair_for_neg = -(num // -negA)
    limit_idxs = n - np.searchsorted(A, limit_pair_for_neg, side='left')
    less_count += limit_idxs.sum()

    duplicate_cases = (A**2 <= num).sum()
    less_count -= duplicate_cases
    less_count //= 2
    
    if less_count >= k:
        right = num
    else:
        left = num

print(right)
Buggy code:
import numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(10000)


def cnt(x,A,An,Ap,N,Nz):
    res = 0
    if x >= 0:
        res += N * Nz
    res += np.searchsorted(A,x//Ap,side='right').sum()
    res += N * len(An) - np.searchsorted(A,-(-x//An),side='left').sum()
    res -= np.count_nonzero(A*A<=x)
    return res//2


def main():
    N,K = map(int,input().split())
    A = list(map(int,input().split()))

    print(len(A))

    A = np.array(A)
    A.sort()

    An = A[A<0]
    Az = A[A==0]
    Ap = A[A>0]

    Nz = np.count_nonzero(A==0)

    INF = 10 ** 18

    l = - INF - 1
    r = INF + 1

    while r-l > 1:
        m = (l+r)//2
        if cnt(m,A,An,Ap,N,Nz) >= K:
            r = m
        else:
            l = m

    print(r)





main()


Fixed code:
import numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(10000)


def cnt(x,A,An,Ap,N,Nz):
    res = 0
    if x >= 0:
        res += N * Nz
    res += np.searchsorted(A,x//Ap,side='right').sum()
    res += N * len(An) - np.searchsorted(A,-(-x//An),side='left').sum()
    res -= np.count_nonzero(A*A<=x)
    return res//2


def main():
    N,K = map(int,input().split())
    A = list(map(int,input().split()))

    A = np.array(A)
    A.sort()

    An = A[A<0]
    Az = A[A==0]
    Ap = A[A>0]

    Nz = np.count_nonzero(A==0)

    INF = 10 ** 18

    l = - INF - 1
    r = INF

    while r-l > 1:
        m = (l+r)//2
        if cnt(m,A,An,Ap,N,Nz) >= K:
            r = m
        else:
            l = m

    print(r)





main()
Buggy code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
mA, nA, zc = [], [], 0
dd = []
for a in A:
    if a<0:
        mA.append(a)
    elif a>0:
        nA.append(a)
    else:
        zc += 1
    dd.append(a*a)
rA = A[::-1]
#print(mA, nA)
lnA = len(nA)
lmA = len(mA)

def F(idx):
    j = lnA-1
    r = 0
    for a in nA:
        while j >= 0:
            if nA[j]*a <= idx:
                break
            j -= 1
        r += j+1
    if idx > 0:
        r += 2*(zc*N) - zc*zc
    j = 0
    for a in mA:
        while j < lmA:
            if mA[j]*a <= idx:
                break
            j += 1
        r += lmA-j
    j = 0
    for a in mA:
        while j < lnA:
            if nA[j]*a <= idx:
                break
            j += 1
        r += 2*(lnA-j)

    for a in dd:
        if idx < a:
            break
        r -= 1
    return r//2
#print(F(5))

mx, mn = 10**18+1, -10**18-1
idx = (mx + mn)//2
target = K
while mx - mn > 1:
    if F(idx) < target:
        idx, mn = (idx + mx)//2, idx
        continue
    idx, mx = (idx + mn)//2, idx
print(idx+1)


Fixed code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
mA, nA, zc = [], [], 0
dd = []
for a in A:
    if a<0:
        mA.append(a)
    elif a>0:
        nA.append(a)
    else:
        zc += 1
    dd.append(a*a)
#rA = A[::-1]
#print(mA, nA)
lnA = len(nA)
lmA = len(mA)

def F(idx):
    j = lnA-1
    r = 0
    for a in nA:
        while j >= 0:
            if nA[j]*a <= idx:
                break
            j -= 1
        r += j+1
    if idx >= 0:
#        print(2*(zc*N) - zc*zc)
        r += 2*(zc*N) - zc*zc
#    print(r, zc)
    j = 0
    for a in mA[::-1]:
        while j < lmA:
            if mA[j]*a <= idx:
                break
            j += 1
        r += lmA-j
    j = 0
    for a in mA:
        while j < lnA:
            if nA[j]*a <= idx:
                break
            j += 1
        r += 2*(lnA-j)

    for a in dd:
        if a <= idx:
            r-=1
    return r//2
#print(F(448283280358331064))

mx, mn = 2*10**18, -2*10**18
idx = (mx + mn)//2
target = K
while mx - mn > 1:
    if F(idx) < target:
        idx, mn = (idx + mx)//2, idx
        continue
    idx, mx = (idx + mn)//2, idx
print(idx+1)
Buggy code:
import numpy as np
from bisect import bisect_right as br
from bisect import bisect_left as bl
n,k=map(int,input().split())
a=np.sort(np.array(list((map(int,input().split())))))
q=len(a[a==0])
d=a[a<0]
e=a[a>0]
l,r=10**18,-10**18
while l-r>1:
    t=(l+r)//2
    p=0
    if t>=0:
        p+=q*n
    p+=np.searchsorted(a,t//e,side="right").sum()+(n-np.searchsorted(a,(-t-1)//(-d),side="right")).sum()
    p-=np.count_nonzero(a*a<=t)
    p//=2
    if p>=k:
        l=t
    else:
        r=t
print(r)

Fixed code:
import numpy as np
from bisect import bisect_right as br
from bisect import bisect_left as bl
n,k=map(int,input().split())
a=np.sort(np.array(list((map(int,input().split())))))
q=len(a[a==0])
d=a[a<0]
e=a[a>0]
l,r=10**18,-10**18
while l-r>1:
    t=(l+r)//2
    p=0
    if t>=0:
        p+=q*n
    p+=np.searchsorted(a,t//e,side="right").sum()+(n-np.searchsorted(a,(-t-1)//(-d),side="right")).sum()
    p-=np.count_nonzero(a*a<=t)
    p//=2
    if p>=k:
        l=t
    else:
        r=t
print(l)
Buggy code:
N=input()
a=[]
s=len(N)

for i in range(s-1,-1,-1):
    a.append(int(N[i]))
a.append(0)
#print(a)

p=sum(a)
t=0
f=0
c=0
for i in range(len(a)-1):
    if a[i] <= 5:
        t+=a[i]
        if a[i] == 5 and a[i+1]>=5:a[i+1]+=1
        f=0
        
    else:
        c+=10-a[i]
        a[i+1] += 1
        f=1
        
    print(t+f+c)

Fixed code:
N=input()
a=[]
s=len(N)

for i in range(s-1,-1,-1):
    a.append(int(N[i]))
a.append(0)
#print(a)

p=sum(a)
t=0
f=0
c=0
for i in range(len(a)-1):
    if a[i] <= 5:
        t+=a[i]
        if a[i] == 5 and a[i+1]>=5:a[i+1]+=1
        f=0
        
    else:
        c+=10-a[i]
        a[i+1] += 1
        f=1
        
print(t+f+c)
Buggy code:
N=input()
L=[int(N[i]) for i in range(len(N))]

P=0
OT=0

for i in range(len(L)-1):
    if 0 <= L[i] <= 4:
        P += L[i] + OT
        OT = 0

    elif L[i] == 5:
        if OT == 1:
            P += 4

        else:
            if L[i+1] >= 5:
                P += 1 + 4
                OT = 1
            else:
                P += 5

    else:
        if OT == 1:
            P += 9-L[i]
            OT = 1
        else:
            P += 10 - L[i]
            OT = 1

Pone = 0
if OT==0:
    Pone = min(L[-1],11-L[-1])
elif OT==1 and L[-1]<=4:
    Pone = 1+L[-1]
else:
    Pone = 10-L[-1]

print(P+Pone)

Fixed code:
N=input()
L=[int(N[i]) for i in range(len(N))]

P=0
OT=0

for i in range(len(L)-1):
    if 0 <= L[i] <= 3:
        P += L[i] + OT
        OT = 0

    elif L[i] == 4:
        if OT == 1 and L[i+1]>=5:
            P += 5
        else:
            P += OT+L[i]
            OT = 0

    elif L[i] == 5:
        if OT == 1:
            P += 4

        else:
            if L[i+1] >= 5:
                P += 1 + 4
                OT = 1
            else:
                P += 5

    else:
        if OT == 1:
            P += 9-L[i]
            OT = 1
        else:
            P += 10 - L[i]
            OT = 1

Pone = 0
if OT==0:
    Pone = min(L[-1],11-L[-1])
elif OT==1 and L[-1]<=4:
    Pone = 1+L[-1]
else:
    Pone = 10-L[-1]

print(P+Pone)
Buggy code:
s = input()
keta = len(s)
n = int(s)
infi = 10 ** 10
dp = [[infi] * (2) for _ in range(keta + 1)]
dp[0][0] = dp[0][1] = 0
s = s[::-1]
for i in range(keta):
    for j in range(2):
        num = int(s[i])
        if j == 1:
            num += 1
        ni = i + 1
        if num == 10:
            nj = 1
            num = 0
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + num)
        elif num < 5:
            nj = 0
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + num)
        elif num > 5:
            nj = 1
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + 10 - num)
        else:
            nj = 0
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + num)
            nj = 1
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + 10 - num)
print(min(dp[keta][0], dp[keta][1] + 1))


Fixed code:
s = input()
keta = len(s)
infi = 10 ** 10
dp = [[infi] * (2) for _ in range(keta + 1)]
dp[0][0] = 0
s = s[::-1]
for i in range(keta):
    for j in range(2):
        num = int(s[i])
        if j == 1:
            num += 1
        ni = i + 1
        if num == 10:
            nj = 1
            num = 0
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + num)
        elif num < 5:
            nj = 0
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + num)
        elif num > 5:
            nj = 1
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + 10 - num)
        else:
            nj = 0
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + num)
            nj = 1
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + 10 - num)
print(min(dp[keta][0], dp[keta][1] + 1))
Buggy code:
#!/usr/bin/env python3
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

L=list(map(int,input())); n=len(L)
L.reverse();L.append(0)
for i in range(n):

  if L[i] >= 6:
    count += 10-L[i]
    L[i] = 0; L[i+1] += 1

  elif L[i] == 5 and L[i+1] == 5:
    count += 10-L[i]
    L[i] = 0; L[i+1] += 1

print(count + sum(L))
# print(L)
    



Fixed code:
#!/usr/bin/env python3
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

L=list(map(int,input())); n=len(L)
L.reverse();L.append(0)
for i in range(n):

  if L[i] >= 6:
    count += 10-L[i]
    L[i] = 0; L[i+1] += 1

  elif L[i] == 5 and L[i+1] >= 5:
    count += 10-L[i]
    L[i] = 0; L[i+1] += 1

print(count + sum(L))
# print(L)
Buggy code:
def main():
  n=list(input())
  n=[int(i) for i in reversed(n)]
  l=len(n)
  n.append(0)
  print(n)
  
  ans=0
  for i in range(l):
    if n[i]<=5:
      a=n[i]
    else:
      n[i+1]+=1
      a=10-n[i]
    print(a)
    ans+=a
  ans+=n[l]
  print(ans)  
if __name__=='__main__':
    main()


Fixed code:
def main():
  n=list(input())
  n=[int(i) for i in reversed(n)]
  l=len(n)
  n.append(0)
  
  ans=0
  for i in range(l):
    if n[i]<=4:
      a=n[i]
    elif n[i]==5:
      if n[i+1]<=4:
        a=n[i]
      else:
        a=10-n[i]
        n[i+1]+=1
    else:
      n[i+1]+=1
      a=10-n[i]
    ans+=a
  ans+=n[l]
  print(ans)  
if __name__=='__main__':
    main()
Buggy code:
S = input()

ans=0
num = [int(i) for i in S]
num.reverse()

def judge(keta):
    if keta >= len(S):
        return true
    tmp = num[keta]
    if tmp < 5:
        return false
    elif tmp > 5:
        num[keta] = 10 - num[keta]
        num[keta+1] += 1
        return true
    else:
        if judge(keta):
            pass
        
for i in range(len(S)-1):
    tmp = num[i]
    if tmp < 5:
        pass
    elif tmp > 5:
        num[i] = 10-num[i]
        num[i+1] +=1
    else:
        if num[i+1] >= 5:
            num[i] = 10-num[i]
            num[i+1] +=1
        else:
            pass

print(sum(num))
            



Fixed code:
S = input()

ans=0
num = [int(i) for i in S]
num.reverse()

def judge(keta):
    if keta >= len(S):
        return true
    tmp = num[keta]
    if tmp < 5:
        return false
    elif tmp > 5:
        num[keta] = 10 - num[keta]
        num[keta+1] += 1
        return true
    else:
        if judge(keta):
            pass
        
for i in range(len(S)-1):
    tmp = num[i]
    if tmp < 5:
        pass
    elif tmp > 5:
        num[i] = 10-num[i]
        num[i+1] +=1
    else:
        if num[i+1] >= 5:
            num[i] = 10-num[i]
            num[i+1] +=1
        else:
            pass

last = len(S)-1
ans = 0
if num[last] > 5:
    num[last] = 10 - num[last]
    ans =1
print(sum(num)+ans)
Buggy code:
n = list(map(int, list(input())))
m = len(n)

DP = [[float("inf")]*2 for i in range(m+1)]
DP[0] = [0,1]

for i in range(m):
    # tight
    tmp = float("inf")
    for j in range(10):
        if j == 0:
            tmp = min(tmp, DP[i][1]+9-n[i])
        elif j <= n[i]:
            tmp = min(tmp, DP[i][1]+j+10+j-1-n[i])
        else:
            tmp = min(tmp, DP[i][0]+j+j-1-n[i])
    DP[i+1][1] = tmp

    # not tight
    tmp = float("inf")
    for j in range(10):
        if j < n[i]:
            tmp = min(tmp, DP[i][1]+j+(10+j)-n[i])
        else:
            tmp = min(tmp, DP[i][0]+j+j-n[i])
    DP[i+1][0] = tmp

print(min(DP[-1]))

Fixed code:
n = list(map(int, list(input())))
m = len(n)

DP = [[float("inf")]*2 for i in range(m+1)]
DP[0] = [0,1]

for i in range(m):
    # tight
    tmp = float("inf")
    for j in range(10):
        if j == 0:
            tmp = min(tmp, DP[i][1]+9-n[i])
        elif j <= n[i]:
            tmp = min(tmp, DP[i][1]+j+10+j-1-n[i])
        else:
            tmp = min(tmp, DP[i][0]+j+j-1-n[i])
    DP[i+1][1] = tmp

    # not tight
    tmp = float("inf")
    for j in range(10):
        if j < n[i]:
            tmp = min(tmp, DP[i][1]+j+(10+j)-n[i])
        else:
            tmp = min(tmp, DP[i][0]+j+j-n[i])
    DP[i+1][0] = tmp

print(DP[-1][0])
Buggy code:
import sys

def solve():
    N = str(input())
    dig = len(N)
    ans = 0
    up = False
    for i in reversed(range(dig)):
        n = int(N[i])
        if 0 <= n <= 4: 
            if up: ans += n + 1
            else: ans += n 
            up = False
        elif n == 5:
            if up: ans += 4
            else: 
                ans += 5
                if i > 0:
                    if int(N[i-1]) >= 5: up = True
        else: 
            if up: ans += 9 - n
            else: ans += 10 - n
            up = True
        #print(n, ans)
    if up: ans += 1
    print(ans)

    return 0

if __name__ == "__main__":
    solve()

Fixed code:
import sys

def solve():
    N = str(input())
    dig = len(N)
    ans = 0
    up = False
    for i in reversed(range(dig)):
        n = int(N[i])
        if 0 <= n <= 4: 
            if up: 
                ans += n + 1
                up = False
                if n == 4:
                    if i > 0:
                        if int(N[i-1]) >= 5: up = True
            else: 
                ans += n 
                up = False
        elif n == 5:
            if up: ans += 4
            else: 
                ans += 5
                if i > 0:
                    if int(N[i-1]) >= 5: up = True
        else: 
            if up: ans += 9 - n
            else: ans += 10 - n
            up = True
        #print(n, ans)
    if up: ans += 1
    print(ans)

    return 0

if __name__ == "__main__":
    solve()