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:
N = input().strip()
 

N = [int(i) for i in N]
#N.append(0)
N.insert(0, 0)
#print(N)

d = len(N)
culc = [0] * d

for i, digit in enumerate(N):
  #digit = int(digit)
  if digit < 5:
    culc[i] = digit
  elif digit == 5:
    if i != (len(N)-1):
      if N[i+1] > 4:
        culc[i-1] += 1
        culc[i] -= (10 - digit) 
      elif culc[i-1] < 0:
        culc[i-1] += 1
        culc[i] -= (10 - digit) 
      else:
        culc[i] = digit
    else:
      if culc[i-1] < 0:
        culc[i-1] += 1
        culc[i] -= (10 - digit) 
      else:
        culc[i] = digit
  else:
    culc[i-1] += 1
    culc[i] -= (10 - digit) 
 
ans = 0
for i in culc:
  ans += abs(i)
    
#print(culc)
print(ans)

Submission status: Wrong Answer

Solution code:
N = input().strip()
 

N = [int(i) for i in N]
#N.append(0)
N.insert(0, 0)
#print(N)

d = len(N)
culc = [0] * d

for i, digit in enumerate(N):
  #digit = int(digit)
  if digit < 5:
    culc[i] = digit
  elif digit == 5:
    if i != (len(N)-1):
      if N[i+1] > 4:
        culc[i-1] += 1
        culc[i] -= (10 - digit) 
      elif culc[i-1] < 0:
        culc[i-1] += 1
        culc[i] -= (10 - digit) 
    else:
      if culc[i-1] < 0:
        culc[i-1] += 1
        culc[i] -= (10 - digit) 
      else:
        culc[i] = digit
  else:
    culc[i-1] += 1
    culc[i] -= (10 - digit) 
 
ans = 0
for i in culc:
  ans += abs(i)
    
#print(culc)
print(ans)

Submission status: Wrong Answer

Solution code:
s=input()
keta=int(len(s))
n=int(s)
#dp[i][kuri]
dp=[[float('INF')]*2 for _ in range(keta+2)]
dp[0][0]=0
for i in range(keta):
    for kuri in range(2):
        nd=int(s[-(i+1)])
        if kuri:
            nd+=1
        for d in range(10):
            nkuri=0
            ni=i+1
            if d-nd>=0:
                a=d
                b=d-nd
            else:
                a=d
                b=10+d-nd
                nkuri=1
            dp[ni][nkuri]=min(dp[ni][nkuri], dp[i][kuri]+a+b)
print(dp[keta][0])

Submission status: Wrong Answer

Solution code:
N = str(input())
keta = len(N)

ans = 0
is_up = 0
for i in range(keta):
  kazu = int(N[keta-1-i]) + is_up
  if kazu <= 4 :
    is_up = 0
    ans += kazu
  elif kazu == 5:
    if i > 0:
      if int(N[keta-i-2]) <= 4:
        is_up = 0
        ans += kazu
      else:
        is_up = 1
        ans += kazu  
    else:
      is_up = 0
      ans += kazu
  elif kazu <= 9:
    is_up = 1
    ans += (10 - kazu)
  else:
    is_up = 1
    
print(ans + is_up)

Submission status: Wrong Answer

Solution code:
N = str(input())
keta = len(N)

ans = 0
is_up = 0
for i in range(keta):
  kazu = int(N[keta-1-i]) + is_up
  if kazu <= 4 :
    is_up = 0
    ans += kazu
  elif kazu == 5:
    if int(N[keta-i-2]) <= 4:
      is_up = 0
      ans += kazu
    else:
      is_up = 1
      ans += kazu     
  elif kazu <= 9:
    is_up = 1
    ans += (10 - kazu)
  else:
    is_up = 1
    
print(ans + is_up)

Submission status: Wrong Answer

Solution code:
def main():
  import numpy as np
  n=list(input())
  n=[int(i) for i in n]
  l=len(n)
  
  dp=np.zeros((l+1,2),dtype=np.int64)   
  for i in range(l):
      dp[i+1][:1]=np.minimum(dp[i][:1]+n[i],dp[i][:1]+10-n[i])
      dp[i+1][1:]=np.minimum(dp[i][1:]+n[i]+1,dp[i][1:]+9-n[i])
  #print(dp)
  print(dp[l,0])

if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = str(input())
keta = len(N)

ans = 0
is_up = False
for i in range(keta):
  kazu = int(N[keta-1-i])
  if is_up:
    kazu += 1
  if kazu <= 4 :
    is_up = False
    ans += kazu
  elif kazu == 5:
    if int(N[keta-i-2]) <= 5:
      is_up = False
      ans += kazu
    else:
      is_up = True
      ans += 10 - kazu      
  elif kazu <= 9:
    is_up = True
    ans += (10 - kazu)
  else:
    is_up = True
    
if is_up:
  ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input()) # n=1
def inpm(): return map(int,input().split()) # x=1,y=2
def inpl(): return list(map(int, input().split())) # a=[1,2,3,4,5,...,n]
def inpls(): return list(input().split())  # a=['1','2','3',...,'n']
def inplm(n): return list(int(input()) for _ in range(n)) # x=[] 複数行
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return sorted([list(map(int, input().split())) for _ in range(n)]) # [[1,1,1,1],[2,2,2,2],[3,3,3,3]] 

def main():
    n = list(input())
    l = len(n)
    ans = 0
    for i in range(l):
        n[i] = int(n[i])
    for i in range(l):
        if n[i] <= 4:
            ans += n[i]
        elif n[i] == 5 and (i == l-1 or n[i+1] <= 4):
            ans += n[i]
        else:
            ans += 10 - n[i]
            n[i] = 0
            if i != l-1:
                n[i+1] += 1
        if i == l-1 and n[i] == 0:
            ans += 1
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
s = input(); n = len(s)
dp = [ [0,1>>32] for i in range(n+1) ]
for i in range(n):
  d = ord(s[i])-ord('0')
  dp[i+1][0] = min(dp[i][0], dp[i][1])+d
  dp[i+1][1] = min(dp[i][0], dp[i][1]-2)+11-d
print(min(dp[n][0],dp[n][1]))

Submission status: Wrong Answer

Solution code:
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input()) # n=1
def inpm(): return map(int,input().split()) # x=1,y=2
def inpl(): return list(map(int, input().split())) # a=[1,2,3,4,5,...,n]
def inpls(): return list(input().split())  # a=['1','2','3',...,'n']
def inplm(n): return list(int(input()) for _ in range(n)) # x=[] 複数行
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return sorted([list(map(int, input().split())) for _ in range(n)]) # [[1,1,1,1],[2,2,2,2],[3,3,3,3]] 

def main():
    n = list(input())
    l = len(n)
    
    ans = 0
    for i in range(l):
        n[i] = int(n[i])
        if i != l-1:
            n[i+1] = int(n[i+1])
        if n[i] == 10:
            if i != l-1:
                n[i+1] += 1
            n[i] = 0
        if n[i] == 0:
            n[i] += 0
        elif n[i] <= 4:
            ans += n[i]
        elif n[i] == 5 and (i == l-1 or n[i+1] <= 4):
            ans += n[i]
        else:
            ans += 10 - n[i]
            n[i] = 0
            if i != l-1:
                n[i+1] += 1
        if i == l-1:
            if n[i] == 0:
                ans += 1
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution 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]<=5:
      a=n[i]
    else:
      n[i+1]+=1
      a=10-n[i]
    ans+=a
  ans+=n[l]
  print(ans)  
if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution 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()

Submission status: Wrong Answer

Solution code:
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input()) # n=1
def inpm(): return map(int,input().split()) # x=1,y=2
def inpl(): return list(map(int, input().split())) # a=[1,2,3,4,5,...,n]
def inpls(): return list(input().split())  # a=['1','2','3',...,'n']
def inplm(n): return list(int(input()) for _ in range(n)) # x=[] 複数行
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return sorted([list(map(int, input().split())) for _ in range(n)]) # [[1,1,1,1],[2,2,2,2],[3,3,3,3]] 

def main():
    N = inp()
    n = []
    while N > 0:
        n.append(N%10)
        N //= 10
    n.reverse()
    l = len(n)
    ans = 0
    for i in range(l-1,-1,-1):
        if n[i] == 10:
            n[i-1] += 1
            n[i] = 0
        if n[i] == 0:
            continue
        if n[i] <= 4:
            ans += n[i]
        elif n[i] == 5 and n[i-1] <= 4:
            ans += n[i]
        else:
            ans += 10 - n[i]
            n[i] = 0
            n[i-1] += 1
        if i == 0:
            if n[i] == 0:
                ans += 1
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
if __name__ == "__main__":
    n = '0' + input()

    ans = 0
    flag = 0

    for i in range(len(n)-1, 0, -1):
        if int(n[i]) + flag == 10:
            flag = 1
        elif int(n[i]) + flag > 5 or (int(n[i]) + flag == 5 and int(n[i-1]) > 4):
            ans += 10 - int(n[i]) - flag
            flag = 1
        else:
            ans += int(n[i]) + flag
            flag = 0
        print(str(n[i]) + ' ' + str(n[i-1]) + ' ' + str(ans))

    if flag == 1:
        ans += 1

    print(ans)

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline
N = int(input().strip())
ans, tmp = divmod(N, pow(10, 100))
N = list(map(int, str(tmp)[::1]))
before = 0
L = len(N)
for i, c in enumerate(N):
    n = c + before
    if 0 <= n < 5:
        before = 0
        ans += n
    elif n == 5:
        if i+1 == L:
            before = 0
            ans += n
            continue
        if N[i+1] < 5:
            before = 0
            ans += n
        else:
            ans += 10-n
            before = 1
    else:
        ans += 10-n
        before = 1
print(ans + before)

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline
N = int(input().strip())
ans = N//pow(10, 100)
N = list(map(int, str(N % pow(10, 100))[::-1]))
before = 0
L = len(N)
for i, c in enumerate(N):
    n = c + before
    if 0 <= n < 5:
        before = 0
        ans += n
    elif n == 5:
        if i+1 == L:
            before = 0
            ans += n
            continue
        if N[i+1] < 5:
            before = 0
            ans += n
        else:
            ans += 10-n
            before = 1
    else:
        ans += 10-n
        before = 1
print(ans + before)

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline
N = int(input().strip())
N = list(map(int, str(N)[::-1]))
before = 0
ans = sum(N[101:])
L = len(N)
for i, c in enumerate(N[:101]):
    n = c + before
    if 0 <= n < 5:
        before = 0
        ans += n
    elif n == 5:
        if i+1 == L:
            before = 0
            ans += n
            continue
        if N[i+1] < 5:
            before = 0
            ans += n
        else:
            ans += 10-n
            before = 1
    else:
        ans += 10-n
        before = 1
print(ans + before)

Submission status: Wrong Answer

Solution code:
N = list(map(int, list(input())))
ans = 0

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

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import numpy as np

N = list(input())
dp = np.zeros((2,len(N)+1))


for i in range(len(N)+1):
  if i < len(N):
    n = int(N[i-1])
  else:
    n = 0
  if i == 0:
    dp[0][i] = n
    dp[1][i] = 10-n
  else:
    dp[0][i] = min(dp[0][i-1]+n, dp[1][i-1]+n+1)
    dp[1][i] = min(dp[0][i-1]+10-n, dp[1][i-1]+9-n)
print(int(min(dp[0][-1],dp[0][-2])))

Submission status: Wrong Answer

Solution code:
s=input()
a=0
b=0
c=len(s)
s='0'+s
s=s[::-1]
d=0
for i in range(c):
  if 0<b:
    if 4<int(s[i])+1:
      if 5==int(s[i])+1 and int(s[i+1])<5:
        a+=5
        b=0
      else:
        a+=(9-int(s[i]))
    else:
      a+=(int(s[i])+1)
      b=0
  else:
    if 4<int(s[i]):
      if int(s[i])==5 and int(s[i+1])<5:
        a+=5
      else:
        b+=1
        a+=(10-int(s[i]))
    else:
      a+=int(s[i])
print(a)

Submission status: Wrong Answer

Solution code:
N=list(input())

N.insert(0,0)
ans=0
for i in range(len(N)-1,-1,-1):
  n=int(N[i])
  if n//10>=1:
    N[i-1]=str(int(N[i-1])+1)
    continue
    
  if n==5:
    if int(N[i-1])>=4:
      N[i-1]=str(int(N[i-1])+1)
    ans+=5
  elif n>=6:
    ans+=10-n
    N[i-1]=str(int(N[i-1])+1)
  else:
    ans+=n
    
print(ans)

Submission status: Wrong Answer

Solution code:
N=list(input())

N.insert(0,0)
ans=0
for i in range(len(N)-1,-1,-1):
  n=int(N[i])
  if n//10>=1:
    N[i-1]=str(int(N[i-1])+1)
    continue
    
  if n==5:
    if int(N[i-1])>=4:
      N[i-1]=str(int(N[i-1])+1)
    ans+=5
  elif n>=10-n:
    ans+=10-n
    N[i-1]=str(int(N[i-1])+1)
  else:
    ans+=n
    
print(ans)

Submission status: Wrong Answer

Solution code:
print("0")

Submission status: Wrong Answer

Solution code:
n = input()
n = n[::-1]
n += '0'
cnt = 0
tmp = 0
print(n)
for i in range(len(n)):
    a = int(n[i])
    a += tmp
    if a == 5:
        cnt += a
        if int(n[i + 1]) >= 5:
            tmp = 1
        else:
            tmp = 0
    elif a < 5:
        cnt += a
        tmp = 0
    else:
        cnt += 10 - a
        tmp = 1
print(cnt)

Submission status: Wrong Answer

Solution code:
S = input()
n = len(S)
dp = [[0] * 2 for _ in range(n+1)]

dp[0][0] = 0
dp[0][1] = 1
for i in range(n):
    dp[i+1][0] = min(int(S[i]) + dp[i][0], 10 - int(S[i]) + dp[i][1])
    dp[i+1][1] = min(int(S[i]) + 1 + dp[i][0], 10 - int(S[i]) - 1 + dp[i][1])    

print(min(dp[n][0], dp[n][1]))

Submission status: Wrong Answer

Solution code:
S = list(map(int,list(input())))
ans = 0
di = {1:1,2:2,3:3,4:4,5:5,6:4,7:3,8:2,9:1,0:0}
for i in range(len(S)-1,-1,-1):
    ans += di[S[i]]
'''
    if S[i] >= 6:
        if i == 0:
            ans += 1
        elif S[i-1] != 0:
            S[i-1] = (S[i-1]+1)%10
'''
if (S[0] >= 6 and S[min(1,len(S)-1)] <5) or (S[min(1,len(S)-1)] >= 6 and S[0]<5):
    ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

# Nの価値の物を購入する際に必要な最小紙幣数とおつりの合計枚数
# 入力はN
# 通貨は10のn乗の紙幣のみ

def gen(n):
    digit_value = []
    
    while(n != 0):
        digit_value.append(n % 10)
        n //= 10
    
    digit_value.append(0)
    digit_value.reverse()
    
    return digit_value

# 支払いに必要な最小の紙幣数
def num_payment(money):
    count = 0
    digit_value = gen(money) 
    num_digit = len(digit_value) - 1
  
    print(digit_value)
    
    for i in range(1, num_digit + 1):
        if(digit_value[-i] <= 5):
             count += digit_value[-i]
             print("digit: " + str(digit_value[-i]) + " count: " + str(i) + " sum: " +str(count)) 
        else:
            count += (10 - digit_value[-i])
            digit_value[-(i+1)] += 1
            print("digit: " + str(digit_value[-i]) + " count: " + str(i) + " sum: " +str(count)) 
    
    return count + digit_value[0]

if __name__ == '__main__':
    value = int(input())
    print(num_payment(value))

Submission status: Wrong Answer

Solution code:
from itertools import groupby

def fivefunction(num):
    if num>=5:
        return 1
    else:
        return 0

N=input()
A=[int(N[i]) for i in range(0,len(N))]
fiveonlyzone=0
for i in range(0,len(N)):
    if len(N)==1:
        if A[0]==5:
            fiveonlyzone+=1
    else:
        if i==0:
            if A[i]==5 and A[i+1]<5:
                fiveonlyzone+=1
        elif i==len(N)-1:
            if A[i-1]<5 and A[i]==5:
                fiveonlyzone+=1
        else:
            if A[i-1]<5 and A[i]==5 and A[i+1]<5:
                fiveonlyzone+=1
jointfour=0
for i in range(1,len(N)-1):
    if A[i-1]>=5 and A[i]==4 and A[i+1]>=5:
        if A[i-1]>5 and A[i+1]>5:
            jointfour+=1
        elif A[i-1]>5 and A[i+1]==5:
            if len(N)-1>=i+2 and A[i+2]>=5:
                jointfour+=1
        elif A[i-1]==5 and A[i+1]>5:
            if i>=2 and A[i-2]>=5:
                jointfour+=1
        else:
            if len(N)-1>=i+2 and i>=2 and A[i+2]>=5 and A[i-2]>=5:
                jointfour+=1
Ndigitsum=sum(A)
data=[fivefunction(A[i]) for i in range(0,len(N))]
Adigitsum=0
for i in range(0,len(N)):
    if fivefunction(A[i])==0:
        Adigitsum+=A[i]
X=groupby(data)
zone=0
digit=0
for key,group in X:
    g=len(list(group))
    if key==1:
        zone+=1
        digit+=g
print(9*digit+2*zone+2*Adigitsum-Ndigitsum-fiveonlyzone-jointfour)

Submission status: Wrong Answer

Solution code:
s = input()[::-1]
s += "0"
n = len(s)

dp = [[1001001001]*2 for _ in range(n+1)]

dp[0][0] = 0
dp[0][1] = 0

for i in range(n):
    for j in range(2):
        x = int(s[i]) + j

        if x < 10:
            dp[i+1][0] = min(dp[i][j] + x,dp[i+1][0])
        if x > 0:
            dp[i+1][1] = min(dp[i][j] + (10 - x),dp[i+1][1])


print(dp[n][0])

Submission status: Wrong Answer

Solution code:
N = list(input())
ans = 0
temp = [0] + [int(n) for n in N]
for i in reversed(range(1, len(N)+1)):
    if temp[i] < 5 or (temp[i] == 5 and temp[i-1] < 4):
        ans += temp[i]
    else:
        temp[i-1] += 1
        ans += (10 - temp[i])
        temp[i] = 0
ans += temp[0]
print(ans)

Submission status: Wrong Answer

Solution code:
s=input()
keta=int(len(s))
n=int(s)
cnt=0
kuri=0
while n:
    n,amari=divmod(n,10)
    if kuri:
        amari+=1
    if amari<=5:
        cnt+=amari
    else:
        cnt+=10-amari
        kuri=1
print(cnt+1)

Submission status: Wrong Answer

Solution code:
S = input()
ans = 0
d = 0
last = "1"
for i in S[::-1]:
    if int(i) >= 5:
        if d == 0:
            ans += 10 - int(i)
        else:
            ans += 10 - int(i) - 1            
        d += 1
    elif d >= 1:
        ans += int(i) + 1
        if last == "5" and d == 1:
            ans -= 1
        d = 0
    else:
        ans += int(i)
    last = i
if d >= 1:
    ans += 1
if S == "5":
    ans = 5
    
print(ans)

Submission status: Wrong Answer

Solution code:
N = input()
m = [0]+list(N)
n = [int(m[i]) for i in range(len(m))]

for i in range(len(n)):
    if n[(i + 1) * (-1)] >= 10:
        n[(i + 1) * (-1)] -= 10
        n[(i + 2) * (-1)] += 1
    elif n[(i + 1) * (-1)] >= 6:
        n[(i + 2) * (-1)] += 1
        n[(i + 1) * (-1)] = 0
    elif n[(i + 1) * (-1)] == 5:
        if n[(i + 2) * (-1)] >= 5:
            n[(i + 2) * (-1)] += 1
            n[(i + 1) * (-1)] = 0
cnt = sum(n)

num = 0
ten = 1
for i in n[::-1]:
    num += i * ten
    ten *= 10

Submission status: Wrong Answer

Solution code:
N = input()
m = [0]+list(N)
n = [int(m[i]) for i in range(len(m))]

for i in range(len(n)):
    if n[(i + 1) * (-1)] >= 10:
        n[(i + 1) * (-1)] -= 10
        n[(i + 2) * (-1)] += 1
    elif n[(i + 1) * (-1)] >= 6:
        n[(i + 2) * (-1)] += 1
        n[(i + 1) * (-1)] = 0
    elif n[(i + 1) * (-1)] == 5:
        if n[(i + 2) * (-1)] >= 5:
            n[(i + 2) * (-1)] += 1
            n[(i + 1) * (-1)] = 0
cnt = sum(n)

Submission status: Wrong Answer

Solution code:
m = [0]+list(input())
n = [int(m[i]) for i in range(len(m))]

for i in range(len(n)):
    if n[(i + 1) * (-1)] >= 10:
        n[(i + 1) * (-1)] -= 10
        n[(i + 2) * (-1)] += 1
    elif n[(i + 1) * (-1)] >= 6:
        n[(i + 2) * (-1)] += 1
        n[(i + 1) * (-1)] = 0
    elif n[(i + 1) * (-1)] == 5:
        if n[(i + 2) * (-1)] >= 5:
            n[(i + 2) * (-1)] += 1
            n[(i + 1) * (-1)] = 0
cnt = sum(n)

num = ''
for i in n:
    num = num + str(i)
num = int(num)

Submission status: Wrong Answer

Solution code:
m = [0]+list(input())
n = [int(m[i]) for i in range(len(m))]

Submission status: Wrong Answer

Solution code:
n = str(input().rstrip())

def solve1(n):
    ans = 0

    a = list(map(int, list(n)))
    a = a[::-1]
    a.append(0)
    for i in range(len(a) - 1):
        c = a[i]
        if c < 5:
            ans += c
        else:
            if c == 5 and a[i+1] < 5:
                ans += 5
            else:
                a[i+1] += 1
                ans += 10 - c
    ans += n_list[-1]

    print(ans)

def solve2(n):
    INF = 10**9
    n = n[::-1]
    n += '0'
    n_len = len(n)
    dp = [INF for _ in range(2)]
    dp[0] = 0

    for i in range(n_len):
        pre_dp = [INF, INF]
        for i in range(2):
            pre_dp[i] = dp[i]
        dp = [INF, INF]
        for j in range(2):
            x = int(n[i])
            x += j
            if x < 10:
                dp[0] = min(dp[0], pre_dp[j]+x)
            if x > 0:
                dp[1] = min(dp[1], pre_dp[j]+(10-x))
    print(dp[0])

solve2(n)

Submission status: Wrong Answer

Solution code:
n = input()
ans = 0
a = False
for i in reversed(n):
    i = int(i)
    if a:
        if i>=5:
            ans += 9-i
        else:
            ans += i+1
            a = False
    else:
        if i>5:
            ans += 10-i
            a = True
        else:
            ans += i
if a:
    ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
N = str(input())
L = len(N)
ans = 0
dp = [[0,0] for _ in range(L)]
s = list(N)
a = int(s[0])
dp[0] = [a,11-a]
for i in range(1,L):
    n = int(s[i])
    dp[i][0] = min(dp[i-1][0],dp[i-1][1]) + n
    dp[i][1] = min(dp[i-1][0],dp[i-1][1]) + 10 - n
dp[-1][1] += 1
print(min(dp[-1]))

Submission status: Wrong Answer

Solution code:
A = str(input())
A = '0' + A
def classifi(x, t):
    x = int(x)
    if t:
        x += 1
    t = False
    if x <= 5:
        d = 0
        return x,d,t
    else:
        d = 10 -x
        x = 0
        t = True
        return x,d,t

B = []
D = []
t = False
for a in reversed(A):
    b,d,t = classifi(a, t)
    B.append(b)
    D.append(d)
print(sum(B) + sum(D))

Submission status: Wrong Answer

Solution code:
n = int(input())
n_str = str(n)
num_0to5 = []
num_6to9 = []

ans = 0

kurisagari_flg = False

for x in list(n_str):
    if int(x)<=5:
        num_0to5.append(int(x))
        ans += int(x)
        kurisagari_flg = False
    else:
        num_6to9.append(int(x))
        ans += 11 - int(x)
        if kurisagari_flg:
            ans -= 2
        kurisagari_flg = True
        
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())

ans = 0
n_dig = len(str(n)) # Nの桁数

carry = 0
for i in range(n_dig):
    d = n % 10
    n //= 10

    if carry + d <= 5:
        ans += d + carry
        carry = 0  # 繰り上がり
    else:
        ans += 10 - d - carry
        carry = 1

print(ans + carry)

Submission status: Wrong Answer

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

# 不変
N = [0] + list(map(int, tuple(input().rstrip("\n"))))

# 計算用
Ncopy = N.copy()
ans = 0
# 繰り上げ発生時に変更
flag = False
for i in range(len(N)-1, 0, -1):
    if (Ncopy[i] <= 4) or (Ncopy[i] == 5 and N[i-1] <= 4):
        ans += Ncopy[i]
        flag = False
    else:
        Ncopy[i-1] += 1
        # 最後だけ特殊
        if not flag:
            ans += 10 - N[i]
        else:
            ans += 9 - N[i]
        Ncopy[i] = 0
        flag = True
print(Ncopy)
if Ncopy[0] == 1:
    ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
l = len(str(n))
ans = 0
nl = [int(str(n)[i]) for i in range(l)]

for i in range(l-1):
  if nl[l-1-i] == 10:
    nl[l-2-i] += 1
  elif 10 > nl[l-1-i] > 5:
    ans += 10-nl[l-1-i]
    nl[l-2-i] += 1
  else:
    ans += nl[l-1-i]
if nl[0]==10:
  ans+=1
elif 10>nl[0]>5:
  ans+=11-nl[0]
else:
  ans += nl[0]
print(ans)

Submission status: Wrong Answer

Solution code:
n = list(reversed([0]+list(map(int, input()))))
ans = 0
for i in range(len(n)):
    if n[i] < 5 or (n[i]==5 and n[i]<5):
        ans += n[i]
    else:
        ans += 10 - n[i]
        n[i+1] += 1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())

ans = 0
n_dig = len(str(n)) # Nの桁数

carry = 0
for i in range(n_dig):
    d = n % 10
    n //= 10

    if carry + d <= 5:
        ans += d + carry
        carry = 0  # 繰り上がり
    else:
        ans += 10 - d - carry
        carry = 1

print(ans + carry)

Submission status: Wrong Answer

Solution code:
N = str(input())
L = len(N)
ans = 0
s = list(N)
for i in range(L):
    n = int(s[i])
    ans += min(n,10-n)

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

m = int(input())
n = deque(list(str(m)))
n.appendleft(0)

Submission status: Wrong Answer

Solution code:
N = list(input())
c = 0
ans = 0
judge = [0]*len(N)

for i in range(len(N)-1,0,-1):
    if judge[i] == 1:
        judge[i-1] = 1
    elif int(N[i]) >= 6:
        judge[i-1] = 1

for i in range(len(N)):
    if judge[i] == 1:
        if int(N[i]) >= 5:
            if c == 1:
                ans += 10-int(N[i])
            else:
                ans += 11-int(N[i])
            c = 1
        else:
            ans += int(N[i])
    else:
        if int(N[i]) >= 6:
            if c == 1:
                ans += 10 - int(N[i])
            else:
                ans += 11 - int(N[i])
        else:
            ans += int(N[i])

if c == 1:
    print(ans-1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
N = input()
count = 0
prev = 0
for i in range(len(N)):
  if 0 <= int(N[i]) + prev < 5:
    count += int(N[i]) + prev
    prev = 0
  elif 6 <= int(N[i]) + prev <= 9:
    count += 10 - (int(N[i]) + prev)
    prev = 1
  elif int(N[i]) + prev == 10:
    prev = 1
  else:
    if i < (len(N) - 1) and int(N[i+1]) < 5:
      count += 5
      prev = 0
    else:
      count += 5
      prev = 1
count = count + prev
print(count)

Submission status: Wrong Answer

Solution code:
from itertools import groupby

def fivefunction(num):
    if num>=5:
        return 1
    else:
        return 0

N=input()
A=[int(N[i]) for i in range(0,len(N))]
fiveonlyzone=0
for i in range(0,len(N)):
    if len(N)==1:
        if A[0]==5:
            fiveonlyzone+=1
    else:
        if i==0:
            if A[i]==5 and A[i+1]<5:
                fiveonlyzone+=1
        elif i==len(N)-1:
            if A[i-1]<5 and A[i]==5:
                fiveonlyzone+=1
        else:
            if A[i-1]<5 and A[i]==5 and A[i+1]<5:
                fiveonlyzone+=1
    
Ndigitsum=sum(A)
data=[fivefunction(A[i]) for i in range(0,len(N))]
Adigitsum=0
for i in range(0,len(N)):
    if fivefunction(A[i])==0:
        Adigitsum+=A[i]
X=groupby(data)
zone=0
digit=0
for key,group in X:
    g=len(list(group))
    if key==1:
        zone+=1
        digit+=g
print(9*digit+2*zone+2*Adigitsum-Ndigitsum-fiveonlyzone)

Submission status: Wrong Answer

Solution code:
import math
from functools import reduce
from collections import deque
import sys
sys.setrecursionlimit(10**7)

# スペース区切りの入力を読み込んで数値リストにして返します。
def get_nums_l():
    return [ int(s) for s in input().split(" ")]

# 改行区切りの入力をn行読み込んで数値リストにして返します。
def get_nums_n(n):
    return [ int(input()) for _ in range(n)]

# 改行またはスペース区切りの入力をすべて読み込んでイテレータを返します。
def get_all_int():
    return map(int, open(0).read().split())

def log(*args):
    print("DEBUG:", *args, file=sys.stderr)

s = input()
log(len(s))
ans = 0

# dp = [ [999999999999999999] * len(s) for _ in range(2) ]

# # dp[k][i] = 下からi桁目で繰り上がりあるなしの最小コスト

# dp[0][0] = 0
# dp[1][0] = 0

# for i,c in enumerate(s):
#     if i == 0:
#         continue
#     a = int(c)

#     dp[0][i] = dp[]

kuriagari = False
prev = None
prev5 = 0
for c in reversed(s):
    a = int(c)

    pay=0
    back=0

    if kuriagari:
        if a <= 4:
            # ans += a+1
            pay = a+1
            kuriagari = False
        else:
            if prev5:
                # log("prev5")
                ans -= prev5
            # ans += 10-a-1
            back = 10-a-1
            kuriagari = True
    else:
        if a <= 5:
            # ans += a
            pay = a
            kuriagari = False
        else:
            if prev5:
                # log("prev5", prev5)
                ans -= prev5
                prev5=0
            # 上の位1枚で払っておつり10-a枚
            # ans += 10-a
            back = 10-a
            kuriagari = True
    ans += pay+back
    if pay==5:
        prev5 += 1
    else:
        if prev5 > 1:
            ans -= (prev5-1)
        prev5 = 0
    # log(a, (pay, back), kuriagari, ans)
if kuriagari:
    ans += 1
if prev5 > 2:
    ans -= (prev5-2)
print(ans)

Submission status: Wrong Answer

Solution code:
m = int(input())
n = [0] + list(str(m))