Problem:


Score: 500 points

Problem Statement

In the Kingdom of AtCoder, only banknotes are used as currency. There are 10^{100}+1 kinds of banknotes, with the values of 1, 10, 10^2, 10^3, \dots, 10^{(10^{100})}. You have come shopping at a mall and are now buying a takoyaki machine with a value of N. (Takoyaki is the name of a Japanese snack.)

To make the payment, you will choose some amount of money which is at least N and give it to the clerk. Then, the clerk gives you back the change, which is the amount of money you give minus N.

What will be the minimum possible number of total banknotes used by you and the clerk, when both choose the combination of banknotes to minimize this count?

Assume that you have sufficient numbers of banknotes, and so does the clerk.

Constraints

N is an integer between 1 and 10^{1,000,000} (inclusive).

Input

Input is given from Standard Input in the following format:

N

Output

Print the minimum possible number of total banknotes used by you and the clerk.

Sample Input 1

36

Sample Output 1

8

If you give four banknotes of value 10 each, and the clerk gives you back four banknotes of value 1 each, a total of eight banknotes are used.

The payment cannot be made with less than eight banknotes in total, so the answer is 8.

Sample Input 2

91

Sample Output 2

3

If you give two banknotes of value 100, 1, and the clerk gives you back one banknote of value 10, a total of three banknotes are used.

Sample Input 3

314159265358979323846264338327950288419716939937551058209749445923078164062862089986280348253421170

Sample Output 3

243



Sample input:
36


Sample output:
8

Submission status: Runtime Error

Solution code:
import sys
import numpy as np



def main(N, dp):
    dp=np.zeros((2,len(N)),dtype=np.int64)
    dp[0, 0] = min(N[0], 11 - N[0])
    dp[0, 1] = min(N[0] + 1, 10 - N[0])
    for i in range(1, len(N)):
        dp[i, 0] = min(dp[i - 1, 0] + N[i], dp[i - 1, 1] + 10 - N[i])
        dp[i, 1] = min(dp[i - 1, 0] + N[i] + 1, dp[i - 1, 1] + 9 - N[i])
    print(dp[-1][0])


if sys.argv[-1] == 'ONLINE_JUDGE':
    import numba
    from numba.pycc import CC
    cc = CC('my_module')
    cc.export('main', '(i8,i8[:],)')(main)
    # b1: bool, i4: int32, i8: int64, double: f8, [:], [:, :]
    cc.compile()
else:
    #from my_module import main
    N = np.array([int(_) for _ in input()])
    main(N)

Submission status: Runtime Error

Solution code:
n=int(input())
def f(x):
    if x==1:
        return 1
    else:
        if x%10==0:
            return f(x//10)
        elif x%10==1:
            return f(x//10)+1
        elif x%10==2:
            return f(x//10)+2
        elif x%10==3:
            return f(x//10)+3
        elif x%10==4:
            return f(x//10)+4
        elif x%10==5:
            return f(x//10)+5
        elif x%10==6:
            return f(x//10+1)+4
        elif x%10==7:
            return f(x//10+1)+3
        elif x%10==8:
            return f(x//10+1)+2
        elif x%10==9:
            return f(x//10+1)+1
print(f(n))

Submission status: Runtime Error

Solution code:
s = input()[::-1]
size = len(s)
s += "9"

ans = 0
bef = 0

for i in range(size):
    v1 = int(s[i])
    v2 = int(s[i+1])
    if v1+bef>=6 or (v1+bef>=5 and v2>=5):
        ans += 10-(v1+bef)
        bef = 1
    else:
        ans += (v1+bef)
        bef = 0

ans += (v+bef)//10
print(ans)

Submission status: Runtime Error

Solution code:
def main():
    import sys
    input = sys.stdin.readline

    N = list(map(int, list(input())))
    N = N[::-1] + [0]

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

    print(ans + N[len(N)-1])

main()

Submission status: Runtime Error

Solution code:
def main():
    import sys
    input = sys.stdin.readline

    N = list(map(int, list(input()[::-1]))) + [0]

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

    print(ans + N[len(N)-1])

main()

Submission status: Runtime Error

Solution code:
# ABC155E

s = input()
d0 = [0,1,2,3,4,5,6,7,8,9]
d1 = [0,1,2,3,4,5,5,4,3,2] # 通常時

a0 = d1[int(s[0])]
a1 = d1[int(s[0]) + 1]

for c in s[1:]:
    c = int(c)
    b0 = min(a0 + c, a1 + (10 - c))
    b1 = min(a0 + c + 1, a1 + (9 - c)) if c <=8 else a1
    a0, a1 = b0, b1
    
print(a0)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
N,Q=map(int,input().split())
ans=[0]*N
ki=[[] for _ in range(N)]
for i in range(N-1):
 a,b=map(int,input().split())
 ki[a-1].append(b-1)
 ki[b-1].append(a-1)
for i in range(Q):
 p,q=map(int,input().split())
 ans[p-1]+=q
# dfs
from collections import deque
stack =deque([0])
visited = ["False"] * N
while stack:
 ne = stack.popleft()
 if visited[ne] == "False":
  visited[ne] = "True"
  for j in ki[ne]:
   if visited[j] == "False":
    ans[j]+=ans[ne]
    stack.append(j)
print(*ans)

Submission status: Runtime Error

Solution code:
def digit(i):
 if i > 0:
  return digit(i//10) + [i%10]
 else:
  return []
N=int(input())
lst=digit(N)
L=len(lst)
ans=[[0 for e in range(2)] for f in range(L)]
ans[0][0]=lst[L-1]
ans[0][1]=10-lst[L-1]
for i in range(1,L):
 ans[i][0]=min(ans[i-1][0]+lst[L-1-i],ans[i-1][1]+lst[L-1-i]+1)
 ans[i][1]=min(ans[i-1][0]+10-lst[L-1-i],ans[i-1][1]+10-lst[L-1-i]-1)
print(min(ans[L-1][0],ans[L-1][1]+1))

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
l = len(s)

INF = float('inf')
dp = [[INF]*2 for _ in range(l+1)]
dp[0][0]=0
dp[0][1]=1
for i in range(l):
    num = int(s[i])
    dp0 = dp[i][0]
    dp1 = dp[i][1]
    dp[i+1][0] = min(dp0+num, dp1+min(10-num,num+1))
    dp[i+1][1] = min(dp0+num+1,dp1+10-num-1)

print(dp[-1][0])

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
s = input()
l = len(s)

INF = float('inf')
dp = [[INF]*2 for _ in range(l+1)]
dp[0][0]=0
dp[0][1]=1
for i in range(l):
    num = int(s[i])
    dp0 = dp[i][0]
    dp1 = dp[i][1]
    dp[i+1][0] = min(dp0+num, dp1+min(10-num,num+1))
    dp[i+1][1] = min(dp0+num+1,dp1+10-num-1)

print(dp[-1][0])

Submission status: Runtime Error

Solution code:
'''
ika tako
X個中のK番目を求めたいが、Xが巨大すぎて全部を実際に計算してられない.
問題は「答えがaだとして、積がa以下となるペアがK個以上できるか？」を
チェックする二分探索で求められることがある。
まず問題を仮にAiが全て正として単純化し「積がM以下となるペアが
K個以上できるか」を判定する.
'''
from bisect import bisect
 
def check_neg(m, k, pos, neg, len_pos):
    less = 0  # m 未満になるペア数
    j = 0
    for a in neg:
        d = m // a
        while j < len_pos and pos[j] <= d:
            j += 1
        if len_pos == j:
            break
        less += len_pos - j
    return less < k
 
 
def check_pos(m, k, pos, neg, len_pos, len_neg):
    less_eq = 0  # m 以下になるペア数
    j = 0
    for i in range(len_pos - 1, -1, -1):
        a = pos[i]
        d = m // a
        while j < len_pos and pos[j] <= d:
            j += 1
        if j == len_pos:
            less_eq += len_pos * (i + 1)
            break
        less_eq += j
 
    j = 0
    for i in range(len_neg - 1, -1, -1):
        a = neg[i]
        d = m // a
        while j < len_neg and neg[j] <= d:
            j += 1
        if j == len_neg:
            less_eq += len_neg * (i + 1)
            break
        less_eq += j
 
    m_sqrt = int(m ** 0.5)
    less_eq -= bisect(pos, m_sqrt)
    less_eq -= bisect(neg, m_sqrt)
 
    less_eq //= 2
 
    return less_eq < k
 
 
def solve(n, k, aaa):
    pos = [a for a in aaa if a > 0]
    neg = [a for a in aaa if a < 0]
    len_pos = len(pos)
    len_neg = len(neg)
    neg_mul = len_pos * len_neg
    pos_mul = len_pos * (len_pos - 1) // 2 + len_neg * (len_neg - 1) // 2
    if neg_mul >= k:
        l = -(10 ** 18)
        r = 0
        while l + 1 < r:
            m = (l + r) // 2
            if check_neg(m, k, pos, neg, len_pos):
                l = m
            else:
                r = m
        return l
    elif n * (n - 1) // 2 - pos_mul >= k:
        return 0
    else:
        k -= n * (n - 1) // 2 - pos_mul
        l = 0
        r = 10 ** 18
        neg = [-a for a in neg]
        neg.reverse()
        while l + 1 < r:
            m = (l + r) // 2
            if check_pos(m, k, pos, neg, len_pos, len_neg):
                l = m
            else:
                r = m
        return r
 
 
n, k = map(int, input().split())
aaa = list(map(int, input().split()))
aaa.sort()
print(solve(n, k, aaa))

Submission status: Runtime Error

Solution code:
N = list(map(int, input()))
N = N[::-1] + [0]

count = 0

def moveUp(i):
  if N[i] == 9:
    N[i] = 0
    moveUp(i+1)
  else:
    N[i] += 1

for i, n in enumerate(N):
  if n < 5:
    count += n
  elif n > 5:
    count += 10 - n
    moveUp(i+1)
  elif n == 5:
    for j in range(i+1, len(N)):
      if N[j] < 5:
        break
      if N[j] >= 5:
        moveUp(i+1)
        break
      
    count += 5
print(count)

Submission status: Runtime Error

Solution code:
N = list(map(int, input()))
N = N[::-1] + [0]

count = 0

def moveUp(i):
  if N[i] == 9:
    N[i] = 0
    moveUp(i+1)
  else:
    N[i] += 1

for i, n in enumerate(N):
  if n < 5:
    count += n
  elif n > 5:
    count += 10 - n
    moveUp(i+1)
  elif n == 5:
    for j in range(i+1, len(N)):
      if N[j] < 5:
        break
      if N[j] > 5:
        moveUp(i+1)
        break
      if N[j] == 5:
        continue
      
    count += 5
print(count)

Submission status: Runtime Error

Solution code:
n = "0"+input()
N = len(n)
mi = 10**100
#num:それまでに何枚使ったか
#keta:何桁目まで確認したか（上の桁から数えて)
#mae:前の桁で一つ多めに支払ったか
def oturi(num,keta,mae):
    global mi
    #最後の桁までたどり着いた場合
    if keta == N-1:
        #前の桁で一つ多めに支払ってた場合
        if mae == True:
            num += 10-int(n[keta])
            mi = min(mi,num)
            return
        #前の桁でちょっきり支払ってた場合
        else:
            num += int(n[keta])
            mi = min(mi,num)
            return
    #最後の桁ではない場合
    #前の桁で一つ多めに支払ってた場合
    if mae == True:
        num += 10-int(n[keta])
        return oturi(num+1,keta+1,True),oturi(num,keta+1,False)
    #前の桁でちょっきり支払ってた場合
    else:
        num += int(n[keta])
        return oturi(num+1,keta+1,True),oturi(num,keta+1,False)
oturi(0,0,True);oturi(0,0,False)
print(mi)

Submission status: Runtime Error

Solution code:
n = int(input())
ans = 0
p = [0,0]
now = [0,0]
p[0] = n%10
p[1] = 10 - n%10
n = n//10
ans = min(p[0], p[1] + 1)
while n > 0:
  now[0] = min(p[0] + n%10, p[1] + n%10 + 1)
  now[1] = min(p[0] + 10 - n%10, p[1] + 9 - n%10)
  ans = min(now[0], now[1] + 1)
  n = n//10
  p = now.copy()
print(ans)

Submission status: Runtime Error

Solution code:
S = input().strip()
dp = [[INFTY for _ in range(2)] for _ in range(len(S))]
for k in range(10):
    n = int(S[-1])
    if k>= n:
        dp[0][0] = min(dp[0][0],2*k-n)
    else:
        dp[0][1] = min(dp[0][1],2*k-n+10)
for i in range(1,len(S)):
    n = int(S[-(1+i)])
    for k in range(10):
        if k>n:
            dp[i][0] = min(dp[i][0],2*k-n+dp[i-1][0],k+(k-1)-n+dp[i-1][1])
        elif k==n:
            dp[i][0] = min(dp[i][0],2*k-n+dp[i-1][0])
            if n==0:
                dp[i][1] = min(dp[i][1],k+(k-1)%10-n+dp[i-1][1])
            else:
                dp[i][1] = min(dp[i][1],k+10-n+k-1+dp[i-1][1])
        elif k<n:
            dp[i][1] = min(dp[i][1],k+10-n+k+dp[i-1][0],k+10-n+k-1+dp[i-1][1])
print(min(dp[len(S)-1][0],dp[len(S)-1][1]+1))

Submission status: Runtime Error

Solution code:
a=input()
i=0
count=0
pay=0
while(i<len(a)-1) :
  
  if i==0 :
    if int(a[0])>=6 :
      count+=1
      i+=2
      pay+=10**(len(a))
    else :
      if int(a[1])>=6 :
        pay+=(int(a[0])+1)*10**(len(a)-1)
        print(10^(len(a)-1))
        count+=int(a[0])+1
        
        i+=2
      else :
        count+=int(a[0])
        pay+=int(a[0])*10**(len(a)-1)
        i+=1
  else :
      if int(a[i+1])>=6 :
        count+=int(a[i])+1
        pay+=(int(a[i])+1)*10**(len(a)-i-1)
        i+=2
      else :
        count+=int(a[i])
        pay+=int(a[i])*10**(len(a)-i-1)
        i+=1

if int(a[-1])<6 :
  pay+=int(a[-1])
  count+=int(a[-1])
k=str(pay-int(a))
for item in k :
  count+=int(item)
print(count)

Submission status: Runtime Error

Solution code:
N = list(map(int, input()))
N = N[::-1] + [0]
out = 0

def moveUp(i, N):
  if N[i] != 9:
      N[i] += 1
  else:
    N[i] = 0
    moveUp(i+1, N)

for i, n in enumerate(N):
  n = int(n)
  if n < 6:
    out += n
  else:
    out += 10-n
    moveUp(i+1, N)
print(out)

Submission status: Runtime Error

Solution code:
from math import log10
s = list(input())
n = len(s)
N = [s[i] for i in range(n-1, -1, -1)] + [0]
dp = [[float('inf'),float('inf')] for _ in range(n+1)]
dp[0][0] = 0

for i in range(n):
    for j in [0,1]:
        x = N[i]
        x += j
        for a in range(10):
            Ni = i + 1; Nj = 0
            b = a - x
            if b < 0:
                Nj = 1
                b += 10
            dp[Ni][Nj] = min(dp[Ni][Nj], dp[i][j]+a+b)

    print(dp[n][0])

Submission status: Runtime Error

Solution code:
S=input()[::-1]
ans=0
#支払う枚数
array = list(map(int, S))
L=len(array)
for i,n in enumerate(array):
    if n<= 5 :
        ans+=n
    elif n > 5:
        ans+= 10-n
        if i <L-1:
            array[i+1] +=1
        else:
            ans +=1
    else:
        ans+=5
        if < L-1:
            if array[i+1] >=5:
                array[i+1] +=1
                
print( ans)

Submission status: Runtime Error

Solution code:
S=input()
paynum=0
payout=0
change=0
changenum=0
num=0
#支払う枚数
array = list(map(int, S))
array.reverse()
print(array)
for i in array:  
    if i <= 5 :
        paynum = paynum + i
        payout = payout + i *( 10 ** num)
    else:
        if num+1 <len(S):
            array[num+1] = array[num+1]+1
        else:
            paynum = paynum + 1
            payout = payout + 1*(10**(num+1))
    num= num+1

#お釣り
change = payout - N
tmp= str(change)
array2 = list(map(int, tmp))
changenum= sum(array2)
print( changenum + paynum)

Submission status: Runtime Error

Solution code:
n = list(map(int,input()))[::-1]
mon = []
pay = []
ret = []
d = 0
for i in range(len(n)):
    c = d + n[i]
    e = c%10
    mon.append(c)
    if e < 5:
        pay.append(e)
        ret.append(0)
        d = c//10
    elif e == 5:
        if i == len(n):
            pay.append(e)
            ret.append(0)
            d = c//10
        else:
            if n[i+1] < 4:
                pay.append(e)
                ret.append(0)
                d = c//10
            else:
                pay.append(0)
                f = 10-e
                ret.append(f)
                d = c//10 + 1
    else:
        pay.append(0)
        f = 10-e
        ret.append(f)
        d = c//10 + 1
if d == 1:
    pay.append(1)
    

ans = 0
for i in range(len(pay)):
    ans += pay[i]
for j in range(len(ret)):
    ans += ret[j]
    

print(ans)

Submission status: Runtime Error

Solution code:
def solve():
    from sys import stdin
    #f_i = stdin
    f_i = open('test_in_3')#
    
    N = f_i.readline().rstrip()
    N = N[::-1] + '0'
    
    INF = 10 ** 32
    
    import numpy as np
    dp = np.full((len(N), 2), INF)
    dp[0][0] = 0
    
    for i, x in enumerate(int(n) for n in N[:-1]):
        ni = i + 1
        dp[ni][0] = min(dp[i] + x)
        dp[ni][1] = min(dp[i][0] + 11 - x, dp[i][1] + 9 - x)
    
    ans = min(dp[-1])
    
    print(ans)

solve()

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline

def main():
  N = input()
  N = '0'+N
  dp= [[float('inf') for _ in range(2)] for _ in range(len(N)+1)]
  dp[0][0] = 0
  for i in range(len(N)):
    n = int(N[len(N)-1-i])
    for j in range(2):
      # jが1のときは繰り下がりあり。
      # 引かれる数から1を引く代わりに、引く数xに1を足す。
      x = n+j
      # xが10のとき、必ず繰り下がりが起こる。
      if x < 10:
        dp[i+1][0] = min(dp[i+1][0],dp[i][j]+x)
      # xが0のとき、繰り下がりは起こせない。
      if x > 0:
        dp[i+1][1] = min(dp[i+1][1],dp[i][j]+10-x)
  print(dp[len(N)][0])  
  # print(dp)
  
if __name__ == '__main__':
  main()

Submission status: Runtime Error

Solution code:
n = input()

def kuriage(x):
    if x == "":
        return "1"
    elif x[-1] != "9":
        return x[:-1] + str(int(x[-1]) + 1)
    else:
        return kuriage(x[:-1])


ans = 0

while n != "":
    m = int(n[-1])
    if m <= 5:
        ans += m
        n = n[:-1]
    else:
        ans += 10 - m
        n = kuriage(n[:-1])

print(ans)

Submission status: Runtime Error

Solution code:
N=int(input())
import math
d=0
k=N
while k>=1:
    z=k-math.floor(k/10)*10
    if z<=4:
        k=math.floor(k/10)
        d=d+z
    elif z>=5:
        k=math.floor(k/10)+1
        d=d+10-z

print(d)

Submission status: Runtime Error

Solution code:
#それぞれの桁を考えた時にその桁を一つ多めに払うのかちょうどで払うのかで場合分けする

import math
n=int(input())
k=math.floor(math.log10(n))+1#桁数
n=list(map(int,str(n)))#それぞれの桁にアクセスするためにイテラブルにする

#それぞれの桁を見ていくが、そのままと一つ多い状態から引く場合と二つあることに注意
dp=[[-1,-1] for _ in range(k)]
dp[0]=[min(1+(10-n[0]),n[0]),min(1+(10-n[0]-1),n[0]+1)]
for i in range(1,k):
    dp[i]=[min(dp[i-1][1]+(10-n[i]),dp[i-1][0]+n[i]),min(dp[i-1][1]+(10-n[i]-1),dp[i-1][0]+n[i]+1)]
print(dp[k-1][0])

Submission status: Runtime Error

Solution code:
#それぞれの桁を考えた時にその桁を一つ多めに払うのかちょうどで払うのかで場合分けする

import math
n=int(input())
k=math.floor(math.log10(n))+1#桁数
n=list(map(int,str(n)))#それぞれの桁にアクセスするためにイテラブルにする

#それぞれの桁を見ていくが、そのままと一つ多い状態から引く場合と二つあることに注意
dp=[[-1,-1] for _ in range(k)]
dp[0]=[min(1+(10-n[i]),n[i]),min(1+(10-n[i]-1),n[i]+1)]
for i in range(1,k):
    dp[i]=[min(dp[i-1][1]+(10-n[i]),dp[i-1][0]+n[i]),min(dp[i-1][1]+(10-n[i]-1),dp[i-1][0]+n[i]+1)]
print(dp[k-1][0])

Submission status: Runtime Error

Solution code:
a = input()

cnt = 0
sakki5 = False
for s in a:
    if int(s) < 6:
        cnt += int(s)
        sakki6 = False
        if int(s) == 5:
            sakki5 = True
    else:
        cnt += 11 - int(s)
        if sakki6:
            cnt -= 2
        if sakki5:
            cnt -= 1
        sakki6 = True
        sakki5 = False
print(cnt)

Submission status: Runtime Error

Solution code:
s = input()
cnt = 0
sakki5 = False
for s in a:
    if int(s) < 6:
        cnt += int(s)
        sakki6 = False
        if int(s) == 5:
            sakki5 = True
    else:
        cnt += 11 - int(s)
        if sakki6:
            cnt -= 2
        if sakki5:
            cnt -= 1
        sakki6 = True
        sakki5 = False
cnt

Submission status: Runtime Error

Solution code:
#payment=桁DPの復習方法
n=input()
#0の方がちょうど、1の方が余裕持って払う
dp0=[0 for i in range(1000001)]
dp1=[0 for i in range(1000001)]
nums=int(n[-1])
dp0[1]=nums
dp1[1]=11-nums
#dp0、dp1でそれぞれぴったり払う時と余裕を持たせて払うときのiけためまでを一つの数としてみたときの最小の値を表すものとする
for i in range(1,len(n)):
    s=int(n[-(i+1)])
    dp0[i+1]=s+min(dp0[i],dp1[i])
    dp1[i+1]=min(9-s+dp1[i],11-s+dp0[i])
print(min(dp0[len(n)],dp1[len(n)]))

Submission status: Runtime Error

Solution code:
import sys
def main():
    input=sys.stdin.readline
    S=input()
    dp=[[0,0] for i in range(len(S)+1)]
    dp[0][1]=1
    for i in range(1,len(S)+1):
        for j in (0,1):
            if j==0:
                dp[i][0]=min(dp[i-1][0]+int(S[i-1]),dp[i-1][1]+10-int(S[i-1]))
            elif j==1:
                dp[i][1]=min(dp[i-1][0]+int(S[i-1])+1,dp[i-1][1]+10-int(S[i-1])-1)

    print(dp[len(S)][0])

if __name__=="__main__":
    main()

Submission status: Runtime Error

Solution code:
# https://atcoder.jp/contests/abc155/submissions/10175015

def main():
    n = input()[::-1]
    s = 0
    cnt = 0
    for i, ni in enumerate(n):
        k = int(ni) + s
        if k < 5 or k == 5 and int(n[i + 1]) < 5:
            cnt += k
            s = 0
        else:
            cnt += 10 - k
            s = 1
    cnt += s
    print(cnt)
 
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
# https://atcoder.jp/contests/abc155/submissions/10175015

n = input()[::-1]
s = 0
res = 0
for i, ni in enumerate(n):
    k = int(ni) + s
    if k < 5 or k == 5 and int(n[i + 1]) < 5:
        res += k
        s = 0
    else:
        res += 10 - k
        s = 1
res += s
print(res)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define REP(i,n) for(int i=0,_n=(int)(n);i<_n;++i)
#define ALL(v) (v).begin(),(v).end()
#define CLR(t,v) memset(t,(v),sizeof(t))
template<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>&a){return os<<"("<<a.first<<","<<a.second<< ")";}
template<class T>void pv(T a,T b){for(T i=a;i!=b;++i)cout<<(*i)<<" ";cout<<endl;}
template<class T>void chmin(T&a,const T&b){if(a>b)a=b;}
template<class T>void chmax(T&a,const T&b){if(a<b)a=b;}


int nextInt() { int x; scanf("%d", &x); return x;}
ll nextLong() { ll x; scanf("%lld", &x); return x;}

const int MAX_N = 1123456;
int x[MAX_N];

int dp[MAX_N][2];

int main2() {
  string S; cin >> S;
  S = "0" + S;
  const int N = S.size();
  REP(i, N) x[i] = S[i]-'0';

  const int INF = 1001001001;
  REP(i, MAX_N) REP(j, 2) dp[i][j] = INF;
  dp[0][0] = 0;
  for (int i = 1; i <= N; i++) {
    int e = x[i-1];
    chmin(dp[i][0], dp[i-1][0] + e);
    chmin(dp[i][1], dp[i-1][0] + e + 1);

    chmin(dp[i][1], dp[i-1][1] + 9 - e); // 何もしない
    chmin(dp[i][0], dp[i-1][1] + e + 1); // 今の桁ぴったり出す。
    chmin(dp[i][1], dp[i-1][1] + e + 1 + 1); // 今の桁+1出す。どこの桁で超えたかがリセットされる
  }
  int ans = min(dp[N][0], dp[N][1] + 1);
  cout << ans << endl;
  return 0;
}

int main() {

#ifdef LOCAL
  for (;!cin.eof();cin>>ws)
#endif
    main2();
  return 0;
}

Submission status: Runtime Error

Solution code:
import sys

n=sys.stdin.readline().rstrip()[::-1]
to=0
num=len(n)
m=0

for i in range(num):
    k=int(n[i])
    k+=m
    if k==5:
        if int(n[i+1])>=5:
            to+=10-k
            m=1
        else:
            to+=k
            m=0
    if k<5:
        to+=k
        m=0
    elif k>5:
        to+=10-k
        m=1
        
print(to+m)

Submission status: Runtime Error

Solution code:
import sys

n=sys.stdin.readline().rstrip()[::-1]
to=0
num=len(n)
m=0

for i in range(num):
    k=int(n[i])
    k+=m
    if k==5:
        if int(n[i+1])>=5:
            to+=10-k
            m=1
        else:
            to+=k
            m=0
    if k<5:
        to+=k
        m=0
    elif k>5:
        to+=10-k
        m=1
        if i==num-1:
            to+=1
        
print(to)

Submission status: Runtime Error

Solution code:
import sys

n=sys.stdin.readline().rstrip()[::-1]
to=0
num=len(n)
m=0
print(n)

for i in range(num):
    k=int(n[i])
    k+=m
    if k==5:
        if int(n[i+1])>=5:
            to+=10-k
            m=1
        else:
            to+=k
            m=0
    if k<5:
        to+=k
        m=0
    elif k>5:
        to+=10-k
        m=1
        if i==num-1:
            to+=1
        
print(to)

Submission status: Runtime Error

Solution code:
import sys
n = list(map(int, sys.stdin.readline().strip()))
s = sum(i if i <= 5 else 10 - i for i in n)
if (n[0] > 5 and n[1] < 6) or (n[0] <= 5 and n[1] >= 6):
    s += 1
print(s)

Submission status: Runtime Error

Solution code:
import sys
n = list(map(int, sys.stdin.readline()))
s = sum(i if i <= 5 else 10 - i for i in n)

Submission status: Runtime Error

Solution code:
import sys
n = list(map(int, sys.stdin.readline()))
s = sum(i if i <= 5 else 10 - i for i in n)

print(s)

Submission status: Runtime Error

Solution code:
import sys
n = list(map(int, sys.stdin.readline()))
s = sum(i if i <= 5 else 10 - i for i in n)
if (n[0] > 5 and n[1] < 6) or (n[0] <= 5 and n[1] >= 6):
  s += 1
print(s)

Submission status: Runtime Error

Solution code:
n = int(input())
n_l = str(n)

harai = 0
keta = False
keisu = 1

for i in range(len(n_l)-1, -1, -1):
    num_i = int(n_l[i])
    if (num_i + keta) == 5:
        for j in range(i-1, -1, -1):
            num_j = int(n_l[j])
            if num_j == 5:
                continue
            elif num_j == 5 and j == 0:
                keta = True
                break
            elif 6 <= num_j <= 9:
                keta = True
                break
            else:
                # 該当ケースは 0 <= num_j <= 4:
                harai += 5 * keisu
                keta = False
                break
    elif 1 <= num_i + keta <= 4:
        harai += (num_i + keta) * keisu
        keta = False
    elif 6 <= num_i + keta <= 10:
        keta = True
    else:
        # 該当するケースはnum_i + keta == 0:
        keta = False
    keisu *= 10

if keta:
    harai += keisu

harai_maisu = sum(tuple(map(int,str(harai))))
otsuri = harai - n
otsuri_maisu = sum(tuple(map(int,str(otsuri))))
print(harai_maisu + otsuri_maisu)

Submission status: Runtime Error

Solution code:
n = int(input())
n_l = str(n)

harai = 0
keta = False
keisu = 0

for i in range(len(n_l)-1, -1, -1):
    num_i = int(n_l[i])
    if (num_i + keta) == 5:
        for j in range(i-1, -2, -1):
            num_j = int(n_l[j])
            if num_j == 5:
                continue
            elif num_j == 5 and j == 0:
                keta = True
                break
            elif 6 <= num_j <= 9:
                keta = True
                break
            elif 0 < num_j <= 4:
                harai += 5 * 10**keisu
                keta = False
                break
    elif 1 <= num_i + keta <= 4:
        harai += (num_i + keta) * 10**keisu
        keta = False
    elif 6 <= num_i + keta <= 10:
        keta = True
    elif num_i + keta == 0:
        keta = False
    keisu += 1
if keta:
    harai += 10**keisu

harai_maisu = sum(list(map(int,str(harai))))
otsuri = harai - n
otsuri_maisu = sum(list(map(int,str(otsuri))))
print(harai_maisu + otsuri_maisu)

Submission status: Runtime Error

Solution code:
n = int(input())
n_l = list(map(int, str(n)))

harai = []
keta = False

for i in range(len(n_l)-1, -1, -1):
    if n_l[i]+keta == 5:
        for j in range(i-1, -2, -1):
            if n_l[j] == 5:
                continue
            elif n_l[j] == 5 and j == 0:
                harai.append(0)
                keta = True
                break
            elif 6 <= n_l[j] <= 9:
                harai.append(0)
                keta = True
                break
            elif 0 <= n_l[j] <= 4:
                harai.append(5)
                keta = False
                break

    elif 1 <= n_l[i]+keta <= 4:
        harai.append(n_l[i]+keta)
        keta = False
    elif 6 <= n_l[i]+keta <= 10:
        harai.append(0)
        keta = True
    elif n_l[i]+keta == 0:
        harai.append(0)
        keta = False

if keta:
    harai.append(1)

harai_maisu = sum(harai)
harai = reversed(harai)
harai_s = int("".join(map(str,harai)))
otsuri = harai_s - n
otsuri_maisu = sum(list(map(int,str(otsuri))))
print(harai_maisu + otsuri_maisu)

Submission status: Runtime Error

Solution code:
n = int(input())
n_l = list(map(int, str(n)))

harai = []
keta = False

for i in range(len(n_l)-1, -1, -1):
    if n_l[i]+keta == 5:
        for j in range(i-1, -2, -1):
            if n_l[j] == 5:
                continue
            elif n_l[j] == 5 and j == 0:
                harai.append(0)
                keta = True
                break
            elif 6 <= n_l[j] <= 9:
                harai.append(0)
                keta = True
                break
            elif 1 <= n_l[j] <= 4:
                harai.append(5)
                keta = False
                break

    elif 1 <= n_l[i]+keta <= 4:
        harai.append(n_l[i]+keta)
        keta = False
    elif 6 <= n_l[i]+keta <= 10:
        harai.append(0)
        keta = True
    elif n_l[i]+keta == 0:
        harai.append(0)
        keta = False

if keta:
    harai.append(1)

harai_maisu = sum(harai)
harai = reversed(harai)
harai_s = int("".join(map(str,harai)))
otsuri = harai_s - n
otsuri_maisu = sum(list(map(int,str(otsuri))))
print(harai_maisu + otsuri_maisu)

Submission status: Runtime Error

Solution code:
n = int(input())
n_l = list(map(int, str(n)))

harai = []
keta = False

for i in range(len(n_l)-1, -1, -1):
    if n_l[i]+keta == 5:
        for j in range(i-1, -2, -1):
            if n_l[j] == 5:
                continue
            elif n_l[j] == 5 and j == 0:
                harai.append(0)
                keta = True
                break
            elif 6 <= n_l[j] <= 9:
                harai.append(0)
                keta = True
                break
            elif 1 <= n_l[j] <= 4:
                harai.append(5)
                keta = False
                break

    elif 1 <= n_l[i]+keta <= 4:
        harai.append(n_l[i]+keta)
        keta = False
    elif 6 <= n_l[i]+keta <= 10:
        harai.append(0)
        keta = True
    elif n_l[i]+keta == 0:
        harai.append(0)
        keta = False

if keta:
    harai.append(1)

harai_maisu = sum(harai)
harai = reversed(harai)
harai_s = int("".join(map(str,harai)))
otsuri = harai_s - n
otsuri_maisu = sum(list(map(int,str(otsuri))))
print(harai_maisu + otsuri_maisu)

Submission status: Runtime Error

Solution code:
n = list(map(int,input()))[::-1]
n.append(0)
for i in range(len(n)):
	if n[i] >= 6 or (n[i] == 5 and n[i+1] >= 5):
    	n[i] = 10 - n[i]
        n[i+1] += 1
print(sum(n))

Submission status: Runtime Error

Solution code:
s=input()
a=len(s)
b=0
ans=0
i=0
c=0
d=0
e=0
f=0
while i<=a-1:
  if int(s[i])<=4:
    if e==1:
      ans=ans+int(s[i])+c
    elif e+d>=1:
      ans=ans+int(s[i])+c-d+2
    else:
      ans=ans+int(s[i])
    c=0
    d=0
    e=0
    f=0
    i=i+1
  elif d==0 and s[i]=="5":
    while i<=a-1 and s[i]=="5":
      if s[i]=="5":
        c=c+5
        d=d+1
        e=e+1
        i=i+1
      
  else:
    c=c+10-int(s[i])
    d=d+1
    i=i+1
    e=0
if d==0 :
  ans=ans
elif e==1:
  ans=ans+5
elif d==1:
  ans=ans+10-int(s[-1])+1
else:
  ans=ans+c-d+2
g=0
h=0
l=0
for i in range(a):
  if a!=0 and a!=a-1 and s[i]=="4":
    if int(s[i-1])>=6:
      h=1
    elif int(s[i-1])<=4:
      h=0
    else:
      if i==1:
        h=0
      elif int(s[i-2])>=5:
        h=1
      else:
        h=0
    if int(s[i+1])>=6:
      l=1
    elif int(s[i+1])<=4:
      l=0
    else:
      if i==a-2:
        l=0
      elif int(s[i+2])>=5:
        l=1
      else:
        l=0
    if h*l==1:
      g=g+1
print(ans-g)

Submission status: Runtime Error

Solution code:
N = input()

for i in range(len(N) - 1, -1, -1):
    t = int(N[i])
    a, b = min(a + t, b + (t + 1)), min(a + (10 - t), b + (10 - (t + 1)))
print(min(a, b + 1))

Submission status: Runtime Error

Solution code:
N = input()

a = 0  # not carried
b = 0  # carried
t = int(N[-1])
a, b = a + t, a + (10 - t)

for i in range(1, len(N))
    t = int(N[-i])
    a, b = min(a + t, b + (t + 1)), min(a + (10 - t), b + (10 - (t + 1)))
print(min(a, b + 1))

Submission status: Runtime Error

Solution code:
N = input()

a = 0  # not carried
b = 0  # carried
t = int(N[:-1]) % 10
a, b = a + t, a + (10 - t)
N = N[:-1]
while N != 0:
    t = int(N[:-1]) % 10
    a, b = min(a + t, b + (t + 1)), min(a + (10 - t), b + (10 - (t + 1)))
    N = N[:-1]
print(min(a, b + 1))

Submission status: Runtime Error

Solution code:
def main():
    from builtin import min
    N = int(input())

    a = 0  # not carried
    b = 0  # carried
    t = N % 10
    a, b = a + t, a + (10 - t)
    N //= 10
    while N != 0:
        t = N % 10
        a, b = min(a + t, b + (t + 1)), min(a + (10 - t), b + (10 - (t + 1)))
        N //= 10
    print(min(a, b + 1))


main()

Submission status: Runtime Error

Solution code:
def main():
    from builtin import min
    N = int(input())

    a = 0  # not carried
    b = 0  # carried
    while N != 0:
        t = N % 10
        a, b = min(a + t, b + (t + 1)), min(a + (10 - t), b + (10 - (t + 1)))
        N //= 10
    print(min(a, b + 1))


main()