Problem p02775 (python)

Fixed submission:
S = input()[::-1]
l = len(S)
dp = [[0]*2 for _ in range(l+1)]
for i in range(l):
  dp[i+1][0]=min(dp[i][0]+int(S[i]),dp[i][1]+int(S[i])+1)
  if i!=0:
    dp[i+1][1]=min(dp[i][0]+10-int(S[i]),dp[i][1]+10-(int(S[i])+1))
  else:
    dp[i+1][1]=dp[i][0]+10-int(S[i])
print(min(dp[-1][0],dp[-1][1]+1))

Buggy submission (verdict: Runtime Error):
N,K = map(int,input().split())
A = list(map(int,input().split()))
A = sorted(A)
mi = 0
zero = 0
for a in A:
  if a<0:
    mi+=1
  elif a==0:
    zero+=1
  else:
    break

plu = N-mi-zero
A_mi = A[0:mi:]
A_mi_rev = A_mi[::-1]
A_plu = A[mi+zero::]
A_plu_rev = A_plu[::-1]
num_mi = plu*mi
num_zero = (plu+mi)*zero+(zero*(zero-1))//2
r = max(A[-1]**2+10,A[0]**2+10)
l = A[0]*A[-1]-10
while l+1!=r:
  m = (l+r)//2
  num = 0
  if m>0:
    num+=num_mi+num_zero
    if num>=K:
      r=m
      continue
    cou=0
    for i in range(0,plu):
      while True:
        if cou>=plu-i-1:
          num+=plu-i-1
          break
        x = A_plu_rev[i]*A_plu[cou]
        if x<m:
          cou+=1
        else:
          num+=cou
          break
          
      if num>=K:
        break
    if num>=K:
      r=m
      continue
    cou = 0
    for i in range(0,mi):
      while True:
        if cou>=mi-i-1:
          num+=mi-i-1
          break
        x = A_mi[i]*A_mi_rev[cou]
        if x<m:
          cou+=1
        else:
          num+=cou
          break
          
      if num>=K:
        break

    if num>=K:
      r=m
    else:
      l=m
    
  elif m==0:
    num+=num_mi
    if num>=K:
      r=m
    else:
      l=m
  else:
    cou = 0
    for a in A_mi_rev:
      while True:
        if cou==plu:
          num+=cou
          break
        x = a*A_plu_rev[cou]
        if x<m:
          cou+=1
        else:
          num+=cou
          break
          
      if num>=K:
        break
    if num>=K:
      r = m
    else:
      l = m
      
print(l)

Buggy submission (verdict: Runtime Error):
S = input()
l = len(S)
dp = [[0]*2 for _ in range(l+1)]
for i in range(l):
  dp[i+1][0]=min(dp[i][1]+10-int(S[i]),dp[i][0]+int(S[i]))
  dp[i+1][1]=min(dp[i][1]+10-int(S[i])-1,dp[i][0]+int(S[i])+1)
print(min(dp[-1][0]+1,dp[-1][1])

Buggy submission (verdict: Runtime Error):
S = input()[::-1]
l = len(S)
dp = [[0]*2 for _ in range(l+1)]
for i in range(l):
  dp[i+1][0]=min(dp[i][0]+int(s[i]),dp[i][1]+int(s[i])+1)
  if i!=0:
    dp[i+1][1]=min(dp[i][0]+10-int(s[i]),dp[i][1]+10-(int(s[i])+1))
  else:
    dp[i+1][1]=dp[i][0]+10-int(s[i])
print(min(dp[-1][0],dp[-1][1]+1))

Buggy submission (verdict: Runtime Error):
N = int(input())
S = list(map(int,list(str(N))))
if len(S)==1:
  if N<=5:
    print(N)
    exit()
  else:
    print(10-N+1)
    exit()
ans = [0]*(len(S)+1)
for i in range(len(S)-1):
  s_1 = int(S[i])
  s_2 = int(S[i+1])
  if (s_1>=6)or((s_1==5)and(s_2>=6)):
    if s_2<=4:
      ans[i]=1
      ans[i+2]=s_2
    else:
      ans[i]=1
  else:
    if s_1==5:
      ans[i+1]=5
      ans[i+2]=s_2
    else:
      if s_2<=5:
        ans[i+1]=s_1
        ans[i+2]=s_2
      else:
        ans[i+1]=s_1+1

a = sum(ans)
ans = int(''.join(map(str,ans)))
ans = ans-N
a += sum(map(int,list(str(ans))))
print(a)

Buggy submission (verdict: Runtime Error):
N = int(input())
S = list(map(int,list(str(N))))
if len(S)==1:
  if N<=5:
    print(N)
    exit()
  else:
    print(10-N+1)
    exit()
ans = [0]*(len(S)+1)
for i in range(len(S)-1):
  s_1 = int(S[i])
  s_2 = int(S[i+1])
  if (s_1>=6)or((s_1==5)and(s_2>=6)):
    if s_2<=4:
      ans[i]=1
      ans[i+2]=s_2
    else:
      ans[i]=1
  else:
    if s_1==5:
      ans[i+1]=5
      ans[i+2]=s_2
    else:
      if s_2<=5:
        ans[i+1]=s_1
        ans[i+2]=s_2
      else:
        ans[i+1]=s_1+1

a = sum(ans)
ans = int(''.join(map(str,ans)))
ans = ans-N
a += sum(map(int,list(str(ans))))
print(a)