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: Wrong Answer

Solution code:
s=input()
ans=[]
if int(s[0]) <= 5:
  ans.append([int(s[0]),0])
else:
  ans.append([0,11-int(s[0])])
for i in range(1,len(s)):
  if ans[i-1][1] > 0:
    if int(s[i]) <= 5:
      ans.append([ans[i-1][0] + ans[i-1][1] + int(s[i]),0])
    else:
      ans.append([ans[i-1][0],ans[i-1][1] + 10 - int(s[i])])
  else:
    if int(s[i]) <= 5:
      ans.append([ans[i-1][0]+int(s[i]),0])
    else:
      ans.append([ans[i-1][0],ans[i-1][1] + 10 - int(s[i])])
if ans[-1][1] > 0:
  ans.append([ans[-1][0]+ans[-1][1]+1,0])
print(ans[-1][0])

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 6)
from collections import defaultdict
from collections import Counter
import fractions
import math
from collections import deque
from bisect import bisect_left
from bisect import insort_left
import itertools
from heapq import heapify
from heapq import heappop
from heapq import heappush
import heapq
import numpy as np
INF = float("inf")
#d = defaultdict(int)
#d = defaultdict(list)
#N = int(input())
#A = list(map(int,input().split()))
#S = list(input())
#S.remove("\n")
#N,M = map(int,input().split())
#S,T = map(str,input().split())
#A = [int(input()) for _ in range(N)]
#S = [input() for _ in range(N)]
#A = [list(map(int,input().split())) for _ in range(N)]
N = list(input())[:-1]
n = len(N)
ans = 0
flag = 0
for i in range(n):
    s = N[n-1-i]
    num = int(s)
    if flag != 0:
        if num <= 4:
            ans += num-flag
            flag = 0
        else:
            ans += 11-num
            flag += 1
    else:
        if num <= 5:
            ans += num
        else:
            ans += 11 - num
            flag = 1
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
ans = 0
while N != 0:
    k = N%10
    N = N // 10
    if k <= 5:
        ans += k
    else:
        ans += 11 - k
print(ans)

Submission status: Wrong Answer

Solution code:
s = map(int,list(raw_input()))
dp = [[0,0] for _ in range(len(s))]
for i in range(len(dp)):
	a = (dp[i - 1][0] if i - 1 >=0 else 0) + s[i]
	b = (dp[i - 1][1] if i - 1 else 1) + 10 - s[i]
	dp[i][0] = min(a,b)

	a = (dp[i - 1][0] if i - 1 >=0 else 0) + s[i] + 1
	b = (dp[i - 1][1] if i - 1 else 1) + 10 - s[i] - 1
	dp[i][1] = min(a,b)
print dp[-1][0]

Submission status: Wrong Answer

Solution code:
s = [0] + map(int,list(raw_input()))
dp = [0] * len(s)

for j in range(len(s) - 1, -1, -1):
	a = s[j]
	if j+ 1 < len(dp):
		a += dp[j + 1] 
	b = s[j] + 1
	b += 10 # dude potentially gives me back 10 
	if j + 1 < len(dp):
		
		b -= s[j + 1]
	if j + 2 < len(dp):
		b += dp[j + 2]


	dp[j] = min(a,b) 

print dp[0]

Submission status: Wrong Answer

Solution code:
import sys
from functools import lru_cache
sys.setrecursionlimit(10**7)
N = int(input())
@lru_cache(None)
def f(n):
  if n<10:
    if n<5:
      return n
    else:
      return 10-n+1
  p,q = divmod(n,10)
  
  return min(f(p)+q, f(p+1)+10-q)
print(f(N))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.readline
read = sys.stdin.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import combinations
#import bisect# lower_bound etc
#import numpy as np
def run():
    N = list(map(int, list(input())))
    N = N[::-1]
    count = 0
    seq = 0
    for idx, n in enumerate(N):
        if seq == 1 and n >= 5:
            count+= (10 - n) - seq
        elif seq == 0 and n >= 6:
            count += 10 - n
            seq = 1
        else:
            count += n + seq
            seq = 0
    count += seq
    print(count)

if __name__ == "__main__":
    run()

Submission status: Wrong Answer

Solution code:
N = input()
L=len(N)

DP=[[N]*2 for _ in range(L+1)]
DP[0][0]=0
DP[0][1]=0

for i,n in enumerate(N,1):
    DP[i][0]= min(DP[i-1][0]+int(n),DP[i-1][1]+10-int(n))
    DP[i][1] = min(DP[i-1][0]+int(n)+1,DP[i-1][1]+9-int(n))


print(DP[L][0])

Submission status: Wrong Answer

Solution code:
from math import ceil
N = input()
L=len(N)

DP=[[N]*2 for _ in range(L+1)]
DP[0][0]=0
DP[0][1]=0

for i,n in enumerate(N,1):
    DP[i][0]= min(DP[i-1][0]+int(n),DP[i-1][1]+10-int(n))
    DP[i][1] = min(DP[i-1][1]+int(n)+1,DP[i-1][1]+9-int(n))


print(DP[L][0])

Submission status: Wrong Answer

Solution code:
from math import ceil
N = input()
L=len(N)
DP=[[N]*(L+1) for _ in range(2)] #DP[i][j]: i=0ならぴったり、 jは上から何桁を見たか(1 origin)
DP[0][0]=0
DP[1][0]=0

for i,n in enumerate(N,1):
    DP[0][i] = min(DP[0][i-1]+int(n), DP[1][i-1]+10 -int(n))
    DP[1][i] = min(DP[0][i-1]+int(n)+1, DP[1][i-1]+9 -int(n))

print(min(DP[0][L],DP[0][L]))

Submission status: Wrong Answer

Solution code:
from math import ceil
N = input()+"0"
L=len(N)
DP=[[N]*(L+1) for _ in range(2)] #DP[i][j]: i=0ならぴったり、 jは上から何桁を見たか(1 origin)
DP[0][0]=0
DP[1][0]=0

for i,n in enumerate(N,1):
    DP[0][i] = min(DP[0][i-1]+int(n), DP[1][i-1]+10 -int(n))
    DP[1][i] = min(DP[0][i-1]+int(n)+1, DP[1][i-1]+9 -int(n))

print(min(DP[0][L],DP[0][L]))

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    import math
    #from math import gcd

    #inf = 10**17
    #mod = 10**9 + 7

    n = list(input().rstrip())
    n = [0] + n
    n = n[::-1]
    ln = len(n)
    res = 0
    a = 0
    for i in range(ln):
        s = int(n[i])
        s += a
        if 0<=s<=4:
            res += s
            a = 0
        else:
            res += 10-s
            a = 1
    print(res)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
s=input()
we=0
sum=0
tmp=0
for i in range(len(s)-1,0,-1):
  if tmp==1:
    qwe=int(s[i])+1
  else:
    qwe=int(s[i])
  if qwe==1 or qwe==9:
    we=1
  elif qwe==2 or qwe==8:
    we=2
  elif qwe==3 or qwe==7:
    we=3
  elif qwe==4 or qwe==6:
    we=4
  elif qwe==5 :
    we=5
  else:
    we=0
  
  sum+=we
  if qwe>5:
    tmp=1
  else:
    tmp=0
  #print("qwe",qwe)

if tmp==1:
    qwe=int(s[0])+1
else:
    qwe=int(s[0])

if qwe==2 or qwe==9:
    we=2
elif qwe==3 or qwe==8:
    we=3
elif qwe==4 or qwe==7:
    we=4
elif qwe==5 or qwe==6:
    we=5
elif qwe==1 or qwe==10 :
    we=1
else:
    we=0
sum+=we
print(sum)

Submission status: Wrong Answer

Solution code:
S = str(input())

flag = False

ans = 0

for s in S[::-1]:
    i = int(s)
    if flag:
        i += 1
    
    if flag != None:
        if i <= 4:
            ans += i
            flag = False
        elif i >= 6:
            ans += 10 - i
            flag = True
        else:
            ans += 5
            flag = None
    else:
        if i <= 4:
            ans += i
            flag = False
        elif i >= 6:
            ans += 10 - (i + 1)
            flag = True
        else:
            ans += 5
            flag = None

if flag:
    ans += 1

# print (ans)
count = 0
for s in S[::-1]:
    if s == '5':
        count += 1
    else:
        if int(s) > 5:
            ans -= max(0, count - 1)
        else:
            ans -= max(0, count - 2)
        count = 0

ans -= max(0, count - 1)
print (ans)

Submission status: Wrong Answer

Solution code:
S = str(input())

flag = False

ans = 0

for s in S[::-1]:
    i = int(s)
    if flag:
        i += 1
    
    if flag != None:
        if i <= 4:
            ans += i
            flag = False
        elif i >= 6:
            ans += 10 - i
            flag = True
        else:
            ans += 5
            flag = None
    else:
        if i <= 4:
            ans += i
            flag = False
        elif i >= 6:
            ans += 10 - (i + 1)
            flag = True
        else:
            ans += 5
            flag = None

if flag:
    ans += 1

# print (ans)
count = 0
for s in S[::-1]:
    if s == '5':
        count += 1
    else:
        if int(s) > 5:
            ans -= max(0, count - 1)
        else:
            ans -= max(0, count - 2)
        count = 0

ans -= max(0, count - 2)
print (ans)

Submission status: Wrong Answer

Solution code:
S = str(input())

flag = False

ans = 0

for s in S[::-1]:
    i = int(s)
    if flag:
        i += 1
    
    if flag != None:
        if i <= 4:
            ans += i
            flag = False
        elif i >= 6:
            ans += 10 - i
            flag = True
        else:
            ans += 5
            flag = None
    else:
        if i <= 4:
            ans += 3
            flag = False
        elif i >= 5:
            ans += 10 - (i + 1)
            flag = True

if flag:
    ans += 1

print (ans)

Submission status: Wrong Answer

Solution code:
n = input()
a = [0,1,2,3,4,5,5,4,3,2]
b = [0,1,2,3,4,5,4,3,2,1]
c = [0,0,1,2,3,4,5,4,3,2]

ans = 0
kuriagari = 0
for i in range(len(n)):
    x = int(n[-i-1])
    if kuriagari:
        if i == len(n)-1:
            ans += c[x]
        else:
            if x>=5:
                kuriagari = 1
            ans += b[x]
    else:
        if x>=6:
            kuriagari = 1
        ans += a[x]

print(ans)

Submission status: Wrong Answer

Solution code:
n = list(map(int, list(input())))
ln = len(n)
ans = 0
for i in range(-1, -ln, -1):
    if n[i] == 10:
        continue
    elif n[i] < 5:
        ans += n[i]
    elif n[i] > 5:
        ans += 10 - n[i]
        n[i-1] += 1
    else:
        if n[i-1] < 5:
            ans += 5
        else:
            ans += 5
            n[i-1] += 1
if n[0] == 10:
    ans += 1
elif n[0] <= 5:
    ans += n[0]
else:
    ans += 11 - n[0]

print(ans)

Submission status: Wrong Answer

Solution code:
from sys import setrecursionlimit
n = "0"+input()
N = len(n)
point = 0
for i in range(1,N):
    if int(n[i]) >= 6:
        point += 10-int(n[i])
    else:
        point += int(n[i])
print(point)

Submission status: Wrong Answer

Solution code:
from sys import setrecursionlimit
n = "0"+input()
N = len(n)
point = 0
for i in range(1,N):
    one_push = 1+point+10-int(n[i])
    no_push = point + int(n[i])
    point = min(one_push,no_push)
print(point)

Submission status: Wrong Answer

Solution code:
from sys import setrecursionlimit
setrecursionlimit(10 **7)
n = "0"+input()
N = len(n)
#num:それまでに何枚使ったか
#keta:何桁目まで確認したか（上の桁から数えて)
def oturi(num,keta):
    one_push = num + 10-int(n[keta])+1#前の桁で一つ多い分
    no_push = num + int(n[keta])
    if keta == N-1:
        return min(one_push,no_push)
    num = min(one_push,no_push)
    return oturi(num,keta+1)
print(oturi(0,0))

Submission status: Wrong Answer

Solution code:
n = int(input())
ans = 0
dp = [[0,0] for _ in range(10**6)]
i = 1
dp[0][0] = n%10
dp[0][1] = 10 - n%10
n = n//10
ans = 0
while n > 0:
  dp[i][0] = min(dp[i-1][0] + n%10, dp[i-1][1] + n%10 + 1)
  dp[i][1] = min(dp[i-1][0] + 10 - n%10, dp[i-1][1] + 9 - n%10)
  ans = min(dp[i][0], dp[i][1] + 1)
  n = n//10
  i += 1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
l = list(str(n))

for j in range(len(l)):
  l[j] = int(l[j])

def up(idx):
  if idx-1 != 0:
    if l[idx-1] == 9 :
      l[idx-1] = 0
      up(idx-1)
    else:
      l[idx-1] = l[idx-1] + 1
  else:
    if l[idx-1] == 9 :
      l[idx-1] = 0
      l.appendleft(1)
    else:
      l[idx-1] = l[idx-1] + 1
      
st = ''
for i in range(len(l)-1,-1,-1):
  if l[i] >= 6 and i > 0:
    l[i] = 0
    up(i)
  st = str(l[i]) + st 

m = int(st) - n
mm = list(str(m))

for k in range(len(mm)):
  mm[k] = int(mm[k])

print(sum(mm) + sum(l))

Submission status: Wrong Answer

Solution code:
n = input()
a = 0 # 繰り下がりが発生しない場合の枚数
b = 1 # 繰り下がり発生させる場合の枚数
for s in n:
    x = int(s)
    # 繰り下がってこない場合、来る場合
    a = min(a + x, b + 10 - x)
    b = min(a + x + 1, b + 9 - x)
    
print(a)

Submission status: Wrong Answer

Solution code:
N = list(map(int, input()))
N = N[::-1] + [1]
out = 0
for i, n in enumerate(N):
  n = int(n)
  if n < 6:
    out += n
  else:
    out += 10-n
    N[i] += 1
print(out)

Submission status: Wrong Answer

Solution code:
n = input()
ans = 0
 
if len(n) == 1:
  print(n)
  exit()
#chk=[] 
if int(n[0]) >= 5:
  ans += 1
for i in range(len(n)-1):
  if int(n[i]) < 5 and int(n[i+1]) > 5:
    ans += int(n[i])+1
    #chk.append(int(n[i])+1)
  elif int(n[i]) < 5 and int(n[i+1]) == 5:
    if i == len(n)-2:
      ans += int(n[i])
      #chk.append(int(n[i]))
    else:
      if int(n[i+2]) >= 5:
        ans += int(n[i])+1
        #chk.append(int(n[i])+1)
      else:
        ans += int(n[i])
        #chk.append(int(n[i]))
  elif int(n[i]) >= 5 and int(n[i+1]) >= 5:
    ans += 9
    ans -= int(n[i])
    #chk.append(9-int(n[i]))
  elif int(n[i]) >= 5 and int(n[i+1]) == 4:
    if i == len(n)-2:
      ans += 10
      ans -= int(n[i])
      #chk.append(10-int(n[i]))
    else:
      if int(n[i+2]) >= 5:
        ans += 9
        ans -= int(n[i])
        #chk.append(9-int(n[i]))
      else:
        ans += 10
        ans -= int(n[i])
        #chk.append(10-int(n[i])) 
  elif int(n[i]) >= 5 and int(n[i+1]) < 4:
    ans += 10
    ans -= int(n[i])
    #chk.append(10-int(n[i]))
  elif int(n[i]) < 5 and int(n[i+1]) < 5:
    ans += int(n[i])
    #chk.append(int(n[i]))
    
if int(n[-1]) >= 5:
  ans += 10
  ans -= int(n[-1])
  #chk.append(10-int(n[-1]))
elif int(n[-1]) < 5:
  ans += int(n[-1])
  #chk.append(int(n[-1]))

print(ans)

Submission status: Wrong Answer

Solution code:
n = input()
ans = 0

if len(n) == 1:
  print(n)
  exit()

if int(n[0]) >= 5:
  ans += 1
for i in range(len(n)-1):
  if int(n[i]) < 5 and int(n[i+1]) >= 5:
    ans += int(n[i])+1
  elif int(n[i]) >= 5 and int(n[i+1]) >= 5:
    ans += 9
    ans -= int(n[i])
  elif int(n[i]) >= 5 and int(n[i+1]) < 5:
    ans += 10
    ans -= int(n[i])
  elif int(n[i]) < 5 and int(n[i+1]) < 5:
    ans += int(n[i])
    
if int(n[-1]) >= 5:
  ans += 10
  ans -= int(n[-1])
elif int(n[-1]) < 5:
  ans += int(n[-1])

print(ans)

Submission status: Wrong Answer

Solution code:
n = input()
ans = 0

if len(n) == 1:
  print(n)
  exit()

if int(n[0]) > 5:
  ans += 1
for i in range(len(n)-1):
  if int(n[i]) <= 5 and int(n[i+1]) > 5:
    ans += int(n[i])+1
  elif int(n[i]) > 5 and int(n[i+1]) > 5:
    ans += 9
    ans -= int(n[i])
  elif int(n[i]) > 5 and int(n[i+1]) <= 5:
    ans += 10
    ans -= int(n[i])
  elif int(n[i]) <= 5 and int(n[i+1]) <= 5:
    ans += int(n[i])
    
if int(n[-1]) >5:
  ans += 10
  ans -= int(n[-1])
elif int(n[-1]) <= 5:
  ans += int(n[-1])

print(ans)

Submission status: Wrong Answer

Solution code:
n = input()
ans = 0

if len(n) == 1:
  print(n)
  exit()
    
for i in range(len(n)-1):
  if int(n[i]) <= 5 and int(n[i+1]) > 5:
    ans += int(n[i])+1
  elif int(n[i]) > 5 and int(n[i+1]) > 5:
    ans += 9
    ans -= int(n[i])
  elif int(n[i]) > 5 and int(n[i+1]) <= 5:
    ans += 10
    ans -= int(n[i])
  elif int(n[i]) <= 5 and int(n[i+1]) <= 5:
    ans += int(n[i])
    
if int(n[-1]) >5:
  ans += 10
  ans -= int(n[-1])
elif int(n[-1]) <= 5:
  ans += int(n[-1])

print(ans)

Submission status: Wrong Answer

Solution code:
N = input()
length=len(N)
R = list(reversed(list(N)))
#print(R)

pay=[0]*(length+1)
charge=[0]*(length+1)

for i in range(length):
    k=int(R[i])
    if k==0:
        pass
    elif 1<=k<=5:
        pay[i]+=k
    else:
        pay[i+1]+=1
        charge[i]+=10-k
#print(pay)
#print(charge)

ans=0
lst=[0]*(length+1)
ideal_pay=[0]*(length+1)
ideal_charge=[0]*(length+1)
for i in range(length+1):
    if pay[i] and charge[i]:
        ans+=abs(pay[i]-charge[i])
        ideal_pay[i]=max(pay[i]-charge[i], 0)
        ideal_charge[i]=max(charge[i]-pay[i], 0)
        lst[i]=abs(pay[i]-charge[i])
    else:
        ans+=pay[i]+charge[i]
        ideal_pay[i]=pay[i]
        ideal_charge[i]=charge[i]
        lst[i]=pay[i]+charge[i]
#print(ans)
#print(ideal_pay)
#print(ideal_charge)
print(sum(ideal_pay)+sum(ideal_charge))

Submission status: Wrong Answer

Solution code:
N = input()

money=[0,1,2,3,4,5,5,4,3,2]
cnt=0
for num in N:
    cnt+=money[int(num)]
print(cnt)

Submission status: Wrong Answer

Solution code:
n=input()
k=0
s=0
for i in range(int(len(n))-1,-1,-1):
    if k==0:
        if int(n[i])<=5:
            s+=int(n[i])
            k=0
        elif int(n[i])>=6:
            s+=11-int(n[i])
            k=1
    elif k==1:
        if int(n[i])<=4:
            s+=int(n[i])
            k=0
        elif int(n[i])>=5:
            s+=9-int(n[i])
            k=1
print(s)

Submission status: Wrong Answer

Solution code:
import sys

stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

n="0"+ns()
ans=0
kuri=0
for j in range(len(n)-1,-1,-1):
    i= int (n[j])
    up=i+kuri
    coin=0
    down=10-i-kuri
    if up>down:
        coin=down
        kuri=1
    else:
        coin=up
        kuri=0
    print(coin)
    ans+=coin

print(ans)

Submission status: Wrong Answer

Solution code:



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

Submission status: Wrong Answer

Solution code:
def main():
    n=list(map(int,list(input())))
    nl=len(n)
    dp=[[0]*2 for _ in range(nl+1)]
    for i in range(1,nl+1):
        if dp[i-1][1]==0:
            if 10-n[i-1]<n[i-1]:
                dp[i-1][0]+=1
                dp[i][1]=10-n[i-1]
            else:
                dp[i][0]=n[i-1]
        else:
            if 10-n[i-1]<n[i-1]:
                dp[i-1][1]-=1
                dp[i][1]=10-n[i-1]
            else:
                dp[i][0]=n[i-1]
    ans=0
    for i in range(nl+1):
        ans+=dp[i][0]+dp[i][1]
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n=[int(str) for str in list(input())]
#print(n)
#calculate how many times you have to modify
a=len(n)



for i in range(1,a):
    if n[-i]==5 & n[-i-1]>=5:
        n[-i]=5.0000001
    if 5<n[-i] :
        n[-i]=10-n[-i]
        n[-i-1]+=1


if 6<=n[0]:
    n[0]=10-n[0]
    n.insert(0,1)



print(round(sum(n)))

Submission status: Wrong Answer

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 i < L-1:
            if array[i+1] >=4:
                array[i+1] +=1
                
print( ans)

Submission status: Wrong Answer

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 i < L-1:
            if array[i+1] >=4:
                array[i+1] +=1
                
print( ans)

Submission status: Wrong Answer

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 i < L-1:
            if array[i+1] >=5:
                array[i+1] +=1
                
print( ans)

Submission status: Wrong Answer

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
                
print( ans)

Submission status: Wrong Answer

Solution code:
S=input()
paynum=0
payout=0
change=0
changenum=0
num=0 #繰り上げフラグ
#支払う枚数
array = list(map(int, S))
array.reverse()
for i in array:  
    if i - num<= 5 :
        paynum = paynum + i
        num=0
    else:
        paynum = paynum + 1
        changenum = changenum + (10-i)
        num=1
print( changenum + paynum)

Submission status: Wrong Answer

Solution code:
S=input()
paynum=0
payout=0
change=0
changenum=0
num=0 #繰り上げフラグ
#支払う枚数
array = list(map(int, S))
array.reverse()
for i in array:  
    if i +num<= 5 :
        paynum = paynum + i
        num=0
    else:
        paynum = paynum + 1
        changenum = changenum + (10-i)
        num=1
print( changenum + paynum)

Submission status: Wrong Answer

Solution code:
S=input()
paynum=0
payout=0
change=0
changenum=0
num=0
#支払う枚数
array = list(map(int, S))
array.reverse()
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 - int(S)
tmp= str(change)
array2 = list(map(int, tmp))
changenum= sum(array2)
print( changenum + paynum)

Submission status: Wrong Answer

Solution code:
S=input()
N=int(S)
paynum=0
payout=0
change=0
changenum=0
tmp1=N
#支払う枚数
for i in range(0,len(S))[::-1]:
    if tmp1 >= 10**i :
        if tmp1//(10**i)>5 :
            paynum = paynum + 1
            payout = payout  + 10**(i+1)
            tmp1 = tmp1-(tmp1//(10**i))*(10**i)
      
        elif len(S)-1==i:
            paynum = paynum + tmp1//(10**i)+1
            payout = payout + (tmp1//(10**i) +1)*(10**i)
            break
        else :
            paynum = paynum + tmp1//(10**i)
            payout = payout + tmp1//(10**i) *(10**i)      
#お釣り
change = payout - N
tmp= str(change)
array = list(map(int, tmp))
changenum= sum(array)
print( changenum + paynum)

Submission status: Wrong Answer

Solution code:
S=input()
N=int(S)
paynum=0
payout=0
change=0
changenum=0
tmp1=N
#支払う枚数
for i in range(0,len(S))[::-1]:
    if tmp1 >= 10**i :
        if tmp1//(10**i)>5 :
            paynum = paynum + 1
            payout = payout  + 10**(i+1)
            tmp1 = tmp1-(tmp1//(10**i))*(10**i)
        elif i==0 :
            paynum = paynum + tmp1//(10**i)
            payout = payout + tmp1//(10**i) *(10**i)            
        else:
            paynum = paynum + tmp1//(10**i)+1
            payout = payout + (tmp1//(10**i) +1)*(10**i)
            break
#お釣り
change = payout - N
tmp= str(change)
array = list(map(int, tmp))
changenum= sum(array)
print( changenum + paynum)

Submission status: Wrong Answer

Solution code:
S=input()
N=int(S)
paynum=0
payout=0
change=0
changenum=0
tmp1=N
#支払う枚数
for i in range(0,len(S))[::-1]:
    if tmp1 >= 10**i :
        if tmp1//(10**i)>5 :
            paynum = paynum + 1
            payout = payout  + 10**(i+1)
            tmp1 = tmp1-(tmp1//(10**i))*(10**i)
        elif i==0 :
            paynum = paynum + tmp1//(10**i)
            payout = payout + tmp1//(10**i) *(10**i)            
        else:
            paynum = paynum + tmp1//(10**i)+1
            payout = payout + (tmp1//(10**i) +1)*(10**i)
            break
change = payout - N
tmp= change
#お釣り
for i in range(0,len(S)+1)[::-1]:
    if tmp >= 10**i :
        changenum = changenum + tmp//(10**i)
        tmp = tmp - (tmp//(10**i))*(10**i)
print(paynum  + changenum)

Submission status: Wrong Answer

Solution code:
S=input()
N=int(S)
paynum=0
payout=0
change=0
changenum=0
tmp1=N
#支払う枚数
for i in range(0,len(S))[::-1]:
    if tmp1 >= 10**i :
        if tmp1//(10**i)>5 :
            paynum = paynum + 1
            payout = payout  + 10**(i+1)
            tmp1 = tmp1-(tmp1//(10**i))*(10**i)
        elif tmp1//(10**i)==1 :
            paynum = paynum + tmp1//(10**i)
            payout = payout + tmp1//(10**i) *(10**i)            
        else:
            paynum = paynum + tmp1//(10**i)+1
            payout = payout + (tmp1//(10**i) +1)*(10**i)
            break
change = payout - N
tmp= change
#お釣り
for i in range(0,len(S)+1)[::-1]:
    if tmp >= 10**i :
        changenum = changenum + tmp//(10**i)
        tmp = tmp - (tmp//(10**i))*(10**i)
    else:
        continue
print(paynum  + changenum)

Submission status: Wrong Answer

Solution code:
N=int(input())
paynum=0
payout=0
change=0
changenum=0
#支払う枚数
for i in range(0,1001)[::-1]:
    if N >= 10**i :
        paynum = N//10**i+1
        payout = paynum *(10**i)
        break
    else:
        continue


change = payout - N
tmp= change
print(tmp)
#お釣り
for i in range(0,1000)[::-1]:
    if tmp >= 10**i :
        changenum = changenum + tmp//(10**i)
    else:
        continue

print(paynum + changenum)

Submission status: Wrong Answer

Solution code:
#i桁めをそのまま払うかi+1桁めの額の紙幣で支払うかでDP
N = input()
digit = len(N)
l_N = [0] * digit
for i in range(1, digit + 1):
  l_N[i - 1] = int(N[-i])
dp = 0
for i in range(digit):
  dp += min(l_N[i], 10 - l_N[i] + 1)
  #print(dp)
print(dp)

Submission status: Wrong Answer

Solution code:
#i桁めをそのまま払うかi+1桁めの額の紙幣で支払うかでDP
N = input()
digit = len(N)
l_N = [0] * digit
for i in range(1, digit + 1):
  l_N[i - 1] = int(N[-i])
dp = 0
for i in range(digit):
  dp += min(l_N[i], 10 - l_N[i] + 1)
  print(dp)
print(dp)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-


def input_multiple_number():
    return map(int, input().split())
def input_multiple_number_as_list():
    return list(map(int, input().split()))

import numpy as np

INF = 99999999
dp= np.zeros((1000005,2))


givenN= input()
givenN= "0" + givenN
nn = len(givenN)
for i in range(nn):
    dp[i][0] = INF
    dp[i][1] = INF

dp[0][0] = 0
for i in range(nn):
    for j in range(2):
        xdigit = int(givenN[nn-1-i])
        for k in range(10):
            bq = k-xdigit-j
            carrie = 0
            if bq < 0:
                bq += 10
                carrie = 1
            dp[i+1][carrie] = min(dp[i+1][carrie],dp[i][j]+bq+k)

print(int(dp[nn-1][0]))

Submission status: Wrong Answer

Solution code:
num = [0,1,2,3,4,5,5,4,3,2]
x = list(map(int,list(input())))
n = len(x)
x.append(0)
if x[0] > 5:
    count = 1
else:
    count = 0
for i in range(n):
    if x[i+1] > 5:
        if x[i] > 5:
            count += 9-x[i]
        else:
            count += x[i]+1
    if x[i+1] < 5:
        if x[i] > 5:
            count += 10-x[i]
        else:
            count += x[i]
    if x[i+1] == 5:
        if x[i] > 5:
            count += 9-x[i]
        else:
            count += x[i]
print(count)

Submission status: Wrong Answer

Solution code:
# E - Payment

N = input().strip()

s = 0
carry = 0
fives = 0
for c in reversed(N):
    x = int(c) + carry
    if x > 5:
        if fives > 0:
            x += 1
            s -= fives - 1
        s += 10 - x
        carry = 1
        fives = 0
    elif x == 5:
        s += x
        carry = 0
        fives += 1
    else:
        s += x
        carry = 0
        fives = 0
s += carry

print(s)

Submission status: Wrong Answer

Solution code:
N = (list(input()))
N =N[::-1]
N_int = [int(i) for i in N]
N_int.append(0)
maisu = 0
keta = False
for i in range(len(N_int)-1):
    if keta ==True:
        N_int[i] +=1
    if N_int[i]<5:
        maisu +=N_int[i]
        keta =False
    elif N_int[i]==5 :
        if N_int[i+1]>4:
            keta =True
            maisu +=(10-N_int[i])
        else:
            keta =False
            maisu += N_int[i]
    else:
        keta =True
        maisu +=(10-N_int[i])       
print(maisu)

Submission status: Wrong Answer

Solution code:
N = int(input())
bills = {N:0}
supperbills = {}
results = []

def calc(bills):
    for key,val in bills.items():
        left1, right1 = key//10, key%10

        if left1 in supperbills.keys():
            supperbills[left1] = val + right1 if supperbills[left1] > val + right1 else supperbills[left1]
        else:
            supperbills[left1] = val + right1


        if right1%5!=0:
            left2 = left1+1
            right2 = 10 - right1

            if left2 in supperbills.keys():
                supperbills[left2] = val + right2 if supperbills[left2] > val + right2 else supperbills[left2]
            else:
                supperbills[left2] = val + right2

    return supperbills



for i in range(len(str(N-1))):
    supperbills = {}
    bills = calc(bills)


#     print(bills)

for key,val in bills.items():
    results.append(val+key)

print(min(results))

Submission status: Wrong Answer

Solution code:
import numpy as np

Ns =str(input())
sub = np.ones(len(Ns))

for j in range(len(Ns)):
    i = len(Ns)-j-1
    intt = int(Ns[i])
    if j == 0:
        sub[i] = intt
    else:
        if sub[i+1]>4:
            sub[i]= intt+1
        else:
            sub[i] = intt


count = 0
for i in sub:
    i = int(i)
    if 10>i>5:
        count+=(11-i)
    elif i<6:
        count+=i
    elif i==10:
        pass
count = int(count)

print(count)

Submission status: Wrong Answer

Solution code:
N = input()
s = str(N)
l = len(s)
A = 0
k = 0
for i in range(-1, -l-1,-1):
  j = int(s[i])
  if j < 5:
    A = A + j
    k = 0
  else:
    A = A + 11 - j - k
    k = 2
print(A)

Submission status: Wrong Answer

Solution code:
n = input()
a = 0
b = 0
for s in n:
  v = int(s)
  a_ = min(a + v, b + v)
  b_ = min(a + 10 - v + 1, b + 10 - v + 1 - 2)
  a = a_
  b = b_
  
print(min(a, b))

Submission status: Wrong Answer

Solution code:
import math

#(min, carrydowm)
def minMoney(n):
  digit = len(str(n))
  if digit == 1:
    if 10 - n + 1 <= n:
      return (10 - n + 1, True)
    else:
      return (n, False)
    #return min(n, 10 - n + 1) #10yen + 1yen vs 9 * 1yen
  top = int(str(n)[0])
  rest = n - top * (10 ** (digit - 1))
  minrest, cd = minMoney(rest)
  n1 = top + minrest
  n2 = 10 - top + 1 + minrest
  if cd:
    n2 -= 2
  if n2 <= n1:
    return (n2, True)
  else:
    return (n1, False)
  #return min(top + minrest, 10 - top + 1 + minrest - 2)

def main():
  n, f = minMoney(int(input()))
  print(n)
  
main()