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:
def digitSum(n):
    # 数値を文字列に変換
    s = str(n)
    # １文字ずつ数値化し配列にする。
    array = list(map(int, s))
    # 合計値を返す
    return sum(array)

N=input()
x=len(N)

o=int(N)//(10**(x-1))
o2=(o+1)*(10**(x-1))-int(N)
o3=digitSum(o2)

o4=(10**(x))-int(N)
o5=digitSum(o4)

if o2+o3>o4+o5:
  print(o4+o5)
else:
  print(o2+o3)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

import sys
import math
import itertools

def main():
    ns = list(map(int, sys.stdin.readline().strip()))
    ns.reverse()

    ans = 0
    kurisagari = False
    for i in range(len(ns)):
        if ns[i] <= 5:
            ans += ns[i]
            kurisagari = False
        else:
            if kurisagari:
                ans += 9 - ns[i]
            else:
                ans += 1 + (10 - ns[i])
            kurisagari = True

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#from fractions import gcd
#mod = 10 ** 9 + 7
#N = int(input())
#a = list(map(int,input().split()))
#a,b,c = map(int,input().split())
#ans = [0] * N
#math.ceilで切り上げ
#dp = [[0] * 4 for i in range(3)] #2次元配列初期化
#dp = [[[0] * 2 for i in range(3)] for j in range(5)]
#(ord('A'))でASCII出力 Aは65
import math
import string

def intinput():
    return int(input())

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

def splitintinput():
    return map(int,input().split())

N = input()
ans = 0
flag = 0 # 桁上がりのフラグ管理
fiveflag = 0
s = len(N)
tmp = 0
for i in range(s):
    if int(N[s-i-1]) + flag == 0:
        continue     
    elif int(N[s-i-1]) + flag < 6:
            ans += int(N[s-i-1]) + flag
            flag = 0
            
    elif int(N[s-i-1]) + flag < 10:
        ans += 10 - int(N[s-i-1])
        if flag == 1:
            ans -= 1
        flag = 1
    else:
        if flag == 1:
            ans -= 1
        continue

print(ans + flag)

Submission status: Wrong Answer

Solution code:

n = input()
n_r = list(map(int,list(n)[::-1]))
for i in range(len(n)):
    if n_r[i] >= 10:
        n_r[i] -= 10 
    elif n_r[i] > 5:
        if len(n_r) < i+1+1:
            n_r.append(0)
        n_r[i+1] += 1
        n_r[i] = 0
a = sum(map(int,list(n)))
tmp = map(int,list(str(int("".join(list(map(str,n_r[::-1]) ) ) ) - int(n))))
b = sum(n_r[::-1]) + sum(tmp)

if a > b:
    print(b)
else:
    print(a)

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

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:
        prev5 = 0
    # log(a, (pay, back), kuriagari, ans)
if kuriagari:
    ans += 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 sys
def main():
    N = input()
    N_int = [int(j) for j in N]
    count = 0
    for i in N_int:
        count += i if i <= 5 else 11 - i
    print(count)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

for i, digit in enumerate(N):
  digit = int(digit)
  if digit < 6:
    culc[i+1] = digit
  else:
    culc[i] += 1
    culc[i+1] -= (10 - digit) 
    if culc[i] > 5:
      culc[i] = (10 - culc[i])
      culc[i-1] += 1

ans = 0
for i in culc:
  ans += abs(i)
    
#print(culc)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [0]+[int(c) for c in str(N)]
l = len(S)
state = 0
ans = 0
for i in range(l-1,-1,-1):
  if S[i]<5:
    if state:
      ans += S[i]+1
    else:
      ans += S[i]
    state = 0
  elif S[i]>5:
    if state:
      ans += 10-S[i]-1
    else:
      ans += 10-S[i]
    state = 1
  else:
    if state:
      ans += 10-S[i]-1
      state = 1
    else:
      if S[i-1]>5:
        ans += 10-S[i]-1
        state = 1
      else:
        ans += S[i]
        state = 0
print(ans)

Submission status: Wrong Answer

Solution code:
N = tuple(input())
N = tuple(map(int, N))

ans = 0
flag = False
flag5 = False
for i, x in enumerate(reversed(N)):
    x = N[i]
    if flag or (flag5 and x >= 5):
        x += 1
    flag5 = False
    flag = False
    if x > 5:
        ans += (10 - x)
        flag = True
        if i == 0:
            ans += 1
    else:
        ans += x
    if x == 5:
        flag5 = True
print(ans)

Submission status: Wrong Answer

Solution code:
def digitSum(n):
    # 数値を文字列に変換
    s = str(n)
    # １文字ずつ数値化し配列にする。
    array = list(map(int, s))
    # 合計値を返す
    return sum(array)

N=input()

x=len(N)



o=int(N)//(10**(x-1))
o2=(o+1)*(10**(x-1))-int(N)
o3=digitSum(o2)
print(o2+o3)

Submission status: Wrong Answer

Solution code:
#import sys
MOD = 10 ** 9 + 7
INFI = 10**10
#input = sys.stdin.readline
import math

import itertools

#import bisect

import copy
from functools import lru_cache

def sosuhante(n):
    for k in range(2, int(math.sqrt(n))+1):
        if n% k ==0:
            return False
    return True
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def kingaku(a,b,n):
    keta=len(str(n))
    return a*n+b*keta

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default

#    h,w,a,b = map(int, input().split())
#    c = [[0 for j in range(n)] for i in range(n)]



def main():
    n=list(map(int,input()))
    N=len(n)
    count=0
    kuri=0
    for i in range(N):
        if n[i]<5:
            count+=n[i]
        elif n[i]>5:
            count+=10-n[i]
            if kuri==0:
                count+=1
                kuri=1
        else:
            if i<N-1:
                if n[i+1]>=5:
                    count += 10 - n[i]
                    if kuri == 0:
                        count += 1
                        kuri = 1
                else:
                    count += n[i]
    print(count)






if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
N = list(str(N))
N = tuple(map(int, N))

ans = 0
flag = False
flag5 = False
for i, x in enumerate(reversed(N)):
    x = N[i]
    if flag or (flag5 and x >= 5):
        x += 1
    flag5 = False
    flag = False
    if x > 5:
        ans += (10 - x)
        flag = True
        if i == 0:
            ans += 1
    else:
        ans += x
    if x == 5:
        flag5 = True
print(ans)

Submission status: Wrong Answer

Solution code:
import math
N=input()
A=[0]*(len(N)+1)
for i in range(len(N)):
  if int(N[len(N)-1-i])<6:
    A[i]+=int(N[len(N)-1-i])
    if A[i]==6:
      A[i+1]=1
      A[i]=0
  else:
    A[i+1]=1
    A[i]=0

ans=sum(A)
s=0
for i in range(len(A)):
  s+=A[i]*(10**i)
s-=int(N)
for i in str(s):
  ans+=int(i)
print(ans)

Submission status: Wrong Answer

Solution code:
import math
import fractions

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

def ValueToBits(x,digit):
    res = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        res[i]=now%2
        now = now >> 1
    return res

def BitsToValue(arr):
    n = len(arr)
    ans = 0
    for i in range(n):
        ans+= arr[i] * 2**i
    return ans

#a = list(map(int, input().split()))

s = input()
n = len(s)

digs = [0 for i in range(n+1)]
for i in range(n):
    digs[i] = int(s[n-1-i])
    
#print(digs)
    
nowPay = sum(digs)
nowChg = 0
chain9 = 0
chain5 = 0
lastDig = 0

ans = nowPay+nowChg
#print(ans)
'''
for i in range(n):
    if(digs[i+1]!=9):
        if(chain9==0):
            nowPay -= digs[i]
            nowPay += 1
            nowChg += 10-digs[i]
            digs[i+1]+=1
        else:
            nowPay -= lastDig
            nowPay += 1
            nowChg += 10-lastDig
            nowPay -= chain9 * 9
            chain9 = 0
            digs[i+1]+=1
    else:
        if(chain9==0):
            lastDig = digs[i]
        chain9 +=1
        
    candi = nowPay + nowChg
    print(candi)
    if(candi < ans):
        ans = candi
        print("a",ans, nowPay, nowChg)
        '''
for i in range(n):
    #print("now",digs[i])
    if(digs[i+1]!=9):
        if(chain9==0):
            if(digs[i]>=6):
                nowPay -= digs[i]
                nowPay += 1
                nowChg += 10-digs[i]
                digs[i+1]+=1
                digs[i]=0
        else:
            if(lastDig>=6):
                nowPay -= lastDig
                nowChg += 10-lastDig
                digs[max(0,i-chain9)]=0
            else:
                nowChg+=1
            nowPay += 1
            nowPay -= chain9 * 9
            digs[i+1]+=1
            for j in range(chain9):
                digs[i-j]=0
            chain9=0
                
    else:
        if(chain9==0):
            lastDig = digs[i]
        chain9 +=1
        
    candi = nowPay + nowChg
    #print(i)
    #print(candi, nowPay, nowChg)
    #print(digs)
    if(candi < ans):
        ans = candi
        #print("a",ans, nowPay, nowChg)

#print(ans)

chain5=0

for i in range(n):
    if(int(s[n-1-i])<5 and chain5>=2):
        ans -= chain5-2
        chain5 =0
    elif(int(s[n-1-i])>5) and chain5>=1:
        ans -= chain5
        chain5 = 0
    elif(int(s[n-1-i])==5):
        chain5+=1

if(chain5>=2):
    ans -= chain5-2


print(ans)

Submission status: Wrong Answer

Solution code:
#下から繰り上がる

n_ =list(input())
n = [int(i) for i in n_]
ans = 0
if len(n)==1:
    print(min(n[0],11-n[0]))
    exit()

if n[-1]>=5:
    ans += 10-n[-1]
    n[-2] += 1
else:
    ans+= n[-1]
for i in range(len(n)-2,0,-1):
    if n[i]>=5:
        ans += 10-n[i]
        n[i-1]+=1
    else:
        ans += n[i]
if n[0]>=6:
    ans += 11-n[0]
else:
    ans += n[0]
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())

#N=a-b
a=0
b=0
i=0
while(N>0):
  digit1=N%10
  if 1<=digit1 and digit1<=5:
    a+=digit1*(10**i)
    N=N//10
  else:
    b+=(10-digit1)*(10**i)
    N=N//10
    N+=1
  i+=1
    
asum=0
while(a>0):
  asum+=a%10
  a//=10
bsum=0
while(b>0):
  bsum+=b%10
  b//=10
  
print(asum+bsum)

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 > 4:
            if n[i+1] == 5:
                n[i+1] += 1
            cnt += (10 - j)

print(cnt)

Submission status: Wrong Answer

Solution code:
N=input()
A=0
B=0
for i in range(len(N)):
    if N[i]=='0':
        A+=0
        if B>=21:
            B=0
    if N[i]=='1':
        A+=1
        if B>=15:
            B=0
    elif N[i]=='2':
        A+=2
        if B>=10:
            B=0
    elif N[i]=='3':
        A+=3
        if B>=6:
            B=0
    elif N[i]=='4':
        A+=4
        if B>=3:
            B=0
    elif N[i]=='5':
        A+=5
        if B>=1:
            B=0
    elif N[i]=='6':
        A+=4
        B+=1
    elif N[i]=='7':
        A+=3
        B+=1
    elif N[i]=='8':
        A+=2
        B+=1
    elif N[i]=='9':
        A+=1
        B+=1
print(A)

Submission status: Wrong Answer

Solution code:
n = int(input())
sn = "0" + str(n)
ans = 0
ren = 0
now = 0
for i in sn[::-1]:
  i = int(i)
  if i < 5:
    if ren == 1:
      now = 0
    ans += i + now
    now = 0
    ren = 0
  else:
    if i == 5:
      ren += 1
    ans += 10 - i - now
    now = 1
print(ans)

Submission status: Wrong Answer

Solution code:
E = reversed(list(map(int, list(input()))))
temp = 0
ans = 0

for e in E:
    if temp == 0:
        if e >= 6 :
            ans += 0
            temp = 1
            ans += (10 - e)
        else:
            ans += e + temp
            temp = 0

print(ans + temp)

Submission status: Wrong Answer

Solution code:
"""
E
"""
N=list(map(int,list(input())))

cnt1=0
cnt2=0
kkk=0

for i in range(len(N)):
    if N[i]<=3:
        cnt1+=N[i]
        kkk=0
    
    elif N[i]==4:
        if kkk==0:
            cnt1+=4
        else:
            if i+1<len(N) and N[i+1]>=5:
                cnt1+=5  
            else:
                cnt1+=4
                kkk=0
                
    elif N[i]==5:
        
        if kkk==1:
            cnt1+=4
            
        else:
            if i+1<len(N) and N[i+1]<=4:
                cnt1+=5  
            else:
                cnt2+=1
                cnt1+=4
                kkk=1
                
    else:
        if kkk==0:
            cnt2+=1
            kkk=1
            
        cnt1+=9-N[i]


print(cnt1+2*cnt2)

Submission status: Wrong Answer

Solution code:
N = input()
l = len(N)
#A = [0]*(N+2)
ans=0
def che(N,a):
    #l = len(N)
    #b+=1
    k=int(N[0])
    if(k>=5):
        a+=1+(10-k)-1
    else:
        a+=k
    if(l!=0):
        che(N[1:l],a)


a=0
for i in range(l):
    k=int(N[i])
    #print(k)
    if(k>=6):
        a+=1+(10-k)
    else:
        if k==5:
            for j in range(i,l):
                c=0
                if(int(N[j])!=0):
                    c+=1
            if c>0:
                a+=1+(10-k)
            else:
                a+=5
        else:
            a+=k
    #print(a)
print(a)

Submission status: Wrong Answer

Solution code:
n = int(input())
num = list(map(int,list(str(n))))
ans = 0
fl = False
zc = 0
for i in num:
    zc += 1
    if i >= 6:
        if fl:
            ans += (10-i)
        else:
            ans += (11-i)
            fl = True
    else:
        ans += i
        Fl = False
    if i != 0:
        zc = 0
print(ans-zc)

Submission status: Wrong Answer

Solution code:
S = input()
cnt = 0
flag = 0
if len(S)==1:
    print(min(10-int(S[0]), int(S[0])))
elif int(S[-2])!=9:
#cnt = min(int(S[-1])+int(S[-2]), ((int(S[-2])+1)%10+10-int(S[-1])))
    cnt = min(1+(9-int(S[-2]))+(10-int(S[-1])%10), 1+(10-int(S[-2])%10), int(S[-2])+1+(10-int(S[-1])%10), 1+int(S[-2])+1+int(S[-1]), int(S[-2])+int(S[-1]))
    if cnt == 1+(9-int(S[-2]))+(10-int(S[-1])%10) or cnt == 1+(10-int(S[-2])%10):
        flag = 1
else:
    cnt = min(1+(10-int(S[-1])), 1+1+(10-int(S[-2])%10), int(S[-2])+int(S[-1]))
    flag = 1
#print(cnt)
#cnt = 4

for i in range(len(S)-2)[::-1]:
    cnt += min(10-int(S[i]), int(S[i]))
    if flag==1:
        cnt -=1
        flag=0


print(cnt)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
s = input()
keta_bit = 0
flag_5 = 0
ans = 0
for i in range(len(s)):
    if int(s[-i-1])+keta_bit <= 4:
        ans += int(s[-i-1])+keta_bit
        keta_bit = 0
        flag_5 = 0
    elif int(s[-i-1])+keta_bit == 5:
        ans += 5
        flag_5 = 1
        keta_bit = 0
    else:
        ans += 10 - int(s[-i-1]) - keta_bit
        if flag_5:
            ans -= flag_5
            flag_5 = 0
        keta_bit = 1
    if i == len(s)-1:
        ans += keta_bit  + flag_5
print(ans)

Submission status: Wrong Answer

Solution code:
n = input()

ans = 0
kuriage = 0
for i in range(len(n)):
  if kuriage == 1:
    num = int(n[-i]) + 1
    kuriage = 0
  else:
    num = int(n[-i])

  if num > 5:
    ans += (10-num)
    kuriage = 1
  elif num == 5 and int(n[-i-1]) > 4:
    ans += (10-num)
    kuriage = 1
  else:
    ans += num

if kuriage == 1:
  ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
count = 0
sn = str(n)
count += sn.count('9')
count += sn.count('8')*2
count += sn.count('7')*3
count += sn.count('6')*4
count += sn.count('5')*5
count += sn.count('4')*4
count += sn.count('3')*3
count += sn.count('2')*2
count += sn.count('1')
if '0'or'1'or'2'or'3'or'4'in sn==1 and '6'or'7'or'8'or'9'in sn==1:
    count +=1



print(count)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#0:0 1:1 2:2 3:3 4:4 5:5 6:5 7:4 8:3 9:2
N=input()
A=0
B=0
for i in range(len(N)):
    if N[i]=='0':
        A+=0
        if B>=21:
            B=0
    if N[i]=='1':
        A+=1
        if B>=15:
            B=0
    elif N[i]=='2':
        A+=2
        if B>=10:
            B=0
    elif N[i]=='3':
        A+=3
        if B>=6:
            B=0
    elif N[i]=='4':
        A+=4
        if B>=3:
            B=0
    elif N[i]=='5':
        A+=5
        if B>=1:
            B=0
    elif N[i]=='6':
        A+=4
        B+=1
    elif N[i]=='7':
        A+=3
        B+=1
    elif N[i]=='8':
        A+=2
        B+=1
    elif N[i]=='9':
        A+=1
        B+=1
if B>=1:
    A+=1
print(A)

Submission status: Wrong Answer

Solution code:
N = input()[::-1]
ans = 0
flag = 0
for i in N:
  if flag == 0:
    if int(i) >= 5:
      flag = 1
      ans += (10 - int(i))
    else: ans += int(i)
  else: 
    if int(i) <= 3:
      ans += (int(i) + 1)
      flag = 0
    else:
      ans += (9 - int(i))
      
if flag == 1:
  ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

List = list(str(N))
L = len(List)
cnt = 0

for i in range(L):
  a = int( List[L-1-i] )
  
  if i == 0:
    cnt = cnt + 10 - a 
  elif a > 5:
    cnt = cnt + 10 - a 
    b = int(List[L-i-2]) + 1
    List[L-i-2] = str(b)
  else:
    cnt = cnt + a 
    
print(cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
Str = str(N)
keta = len(str(N))
ans = 0
j = 0
for i in range(0,keta-1):
    n = int(Str[keta-i-1])
    if n > 5:
        n = 10 - n
    ans += n

if ans == 0:
    print(1)
else:
    print(ans+1)

Submission status: Wrong Answer

Solution code:
from functools import reduce
from fractions import gcd
import math
import bisect
import itertools
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
INF = float("inf")


def main():
    N = list(input()[:-1])
    res = 0
    c = 0
    for i in reversed(range(len(N))):
        n = int(N[i])
        if n + c <= 10 - n:
            c = 0
            res += n + c
        else:
            c = 1
            res += 10 - n + c
    
    print(res)

    


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n=input()
m=list(n)
l=[int(i) for i in m]
l.reverse()
l.append(0)
k=l
ans=0
for i in range(len(l)):
  if k[i]<=5:
    ans+=k[i]
  else:
    ans+=10-k[i]
    k[i+1]+=1
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import combinations
from bisect import bisect_left
import sys
sys.setrecursionlimit(10 ** 6)
 

def main():
    # dp:=上からi桁目まで払える最小の枚数
    S = list(map(int, input()))
    l = len(S)
    dp = [0]*10**6
    # dp[1] = int(S[-1])
    for i in range(0, l):
        k = int(S[-(i+1)])
        tmp = 1 + 10*(i+1)-sum(S[-(i+1):])-i
        dp[i+1] = min(min(dp[i]+k, dp[i]+11-k), tmp)
        # dp[i+1] = min(dp[i]+k, dp[i]+11-k)
    print(min(dp[l], sum(S)))

    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N_str = input()

ln = len(N_str)
ans = 0
table = [[0] * 2 for _ in range(ln)]
table[0] = [int(N_str[-1]), 11 - int(N_str[-1])]
for i in range(1, ln):
    table[i] = [int(N_str[-i]) + min(table[i - 1]),
                min(int(N_str[-i]) + table[i - 1][0], 10 - int(N_str[-i]) + table[i - 1][1])]
print(min(table[ln - 1]))

Submission status: Wrong Answer

Solution code:
def read():
    N = input().strip()
    return N, 

def solve(N):
    count = 0
    L = len(N)
    N = list(reversed("00" + N))
    for i in range(L+1):
        nd = int(N[i])
        if nd > 10 - nd:
            k = 1
            while N[i+k] == "9":
                N[i+k] = "0"
                k += 1
            N[i+k] = str(int(N[i+k]) + 1)
            count += 10 - nd
        else:
            count += nd
    return count


if __name__ == '__main__':
    inputs = read()
    print("{}".format(solve(*inputs)))

Submission status: Wrong Answer

Solution code:
N = int(input())
N_str = list(str(N))
n = len(N_str) #けた数

tmp =  (N//(10**(n-1)) + 1)*(10**(n-1)) 
red = tmp-N
red_str = list(str(red))
red_str.sort(reverse=True)
#print(red_str)

num_acc, red_acc = [], []
tmp_num_acc, tmp_red_acc = 0, 0
for i in range(len(red_str)):
   if(i == 0): tmp_num_acc += int(N_str[i])+1
   else: tmp_num_acc += int(N_str[i])
   tmp_red_acc += int(red_str[i])
   num_acc.append(tmp_num_acc)
   red_acc.append(tmp_red_acc)
  
ans = float('inf')
for i in range(len(num_acc)):
  tmp = num_acc[i]+red_acc[i]
  ans = min(tmp,ans)

  
ans1 = N + (10 - N%10) #端数無視
ans1_str = str(ans1)
#red1 = str(ans1-N)

ans2 = N + 10  #端数綺麗に払う
ans2_str = str(ans2)
#red2 = str(ans2-N)
  
ans1_cnt,ans2_cnt = 0,0
for i in range(len(ans1_str)):
  ans1_cnt += int(ans1_str[i])
  ans2_cnt += int(ans2_str[i])
  
#おつり分                   
if(N_str[-1] != 0):                                                   
 #ans1                             
 ans1_cnt += 10 - N%10 
 #ans2 
 ans2_cnt += 1  
  
ans = min(ans1_cnt,ans2_cnt,ans)
print(ans)

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 += N[i] // 10 + N[i] % 10
    if N[i] == 5 and i > 1:
        if N[i - 1] == 9:
            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:
#!/usr/bin/env python3
# https://atcoder.jp/contests/abc155/tasks/abc155_e

n = int(input())
# n = 1198
# print(n)
d = []
keta = len(str(n))
cnt = 0
for i in range(1, keta + 1):
    # print(f"{i} keta")
    kazu = (n % (10 ** i)) // 10 ** (i -1)

    if 0 <= kazu <= 5:
        cnt += kazu
    else:
        cnt += (10 - kazu)
        if i > 0:
            n += 10 ** i
        else:
            cnt += 1
print(cnt)

Submission status: Wrong Answer

Solution code:
N = input()
digit_length = len(N)
# 最後の桁の反転は9-int(d)ではなく10-int(d)
ans = 0
is_reverse = False
for i, d in enumerate(N):

    if is_reverse == False and int(d) == 0:
        continue
    if is_reverse and int(d) == 9:
        continue

    if i == digit_length-1: # 最終桁の場合

        if is_reverse == False:
            if int(d) <= 5:
                ans += int(d)
            else:
                #ans += 10 - int(d)
                ans += 11 - int(d)
                is_reverse = True
        else:
            #if 9 - int(d) <= 5:
            if 10 - int(d) <= 5:
                #ans += 9-int(d)
                ans += 10-int(d)
            else:
                ans += 10 - (9-int(d))
                is_reverse = False


    elif is_reverse == False:
        if int(d) <= 5:
            ans += int(d)
        else:
            ans += 10-int(d)
            is_reverse = True
    else: # is_reverse == True
        if 9 - int(d) <= 5:
            ans += 9-int(d)
        else:
            ans += 10 - (9-int(d))
            is_reverse = False

print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())

#N=a-b
a=0
b=0
i=0
asum=0
bsum=0
while(N>0):
  digit1=N%10
  if 1<=digit1 and digit1<=5:
    a+=digit1*(10**i)
    asum+=digit1
    N=N//10
  else:
    b+=(10-digit1)*(10**i)
    bsum+=digit1
    N=N//10
    N+=1
  i+=1
    
print(asum+bsum)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
s=input()
a=len(s)
b=0
ans=0
i=a-1
c=0
d=0
e=0
while i>=0:
  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
    i=i-1
  elif d==0 and s[i]=="5":
    while i>=0 and 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 :
  print(ans)
elif d==1:
  print(ans+10-int(s[0]))
else:
  print(ans+c-d+2)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
S = [0] + list(map(int, list(input())[: -1]))
res = 0
for i in range(len(S) - 1, -1, -1):
  if S[i] <= 4:
    res += S[i]
  else:
    res += 10 - S[i]
    S[i - 1] += 1
print(res)

Submission status: Wrong Answer

Solution code:
char_n = input()
coin_n = 0
l = len(char_n)

dp = [0]*(l+1)
dp[1] = min(1+10-int(char_n[-1]), int(char_n[-1]))

for i in range(2, l+1):
  temp_add_coin = min(1+10-int(char_n[-i]), int(char_n[-i]))
  remain = 10**(i-1)-int(char_n[-i:])
  
  print(remain)
  dp[i] = min(dp[i-1]+temp_add_coin, 1+remain)
  
print(dp[-1])

Submission status: Wrong Answer

Solution code:
S=input()
INF=10**9
#dp[i][j]: 上からi桁目まで見たときの枚数最小値(jはi桁め以前に紙幣を出したか)
dp=[[0 for _ in range(2)] for __  in range(len(S)+1)]
dp[0][1]=1
for i in range(len(S)):
    n=int(S[i])
    dp[i+1][0]=dp[i][0]+n
    if n<=5:
        dp[i+1][1]=min(dp[i][0]+n+1,dp[i][1]+n-1)
    else:
        dp[i+1][1]=min(dp[i][0]+1+10-n,dp[i][1]+10-n)
print(min(dp[len(S)][0],dp[len(S)][1]))

print(dp)

Submission status: Wrong Answer

Solution code:
N = int(input())
m = str(N)
mn = len(m)
ans = 0
if int(m[0]) < 5:
  ans += int(m[0])+1
  N = (int(m[0])+1)*(10**(mn-1)) -N
  
while N > 0:
  M = str(N)
  n = len(M)
  if int(M[0]) >= 6:
    ans += 1
    N = 10**n - N
  else:
    ans += int(M[0])
    N = N - int(M[0])*(10**(n-1))
print(ans)

Submission status: Wrong Answer

Solution code:
import sys


# N, M = map(int, input().split())
# nums = list(map(int, input().split()))
# nums_2d = [list(map(int, input().split())) for _ in range(N)]


def main():
    N = input()
    solve(N)


def solve(N):
    total = 0
    rem = 0
    rem2 = 0
    for s in N[::-1]:
        n = int(s) + rem
        if n == 10:
            rem = 1
            if rem2:
                total -= 1
                rem2 = 0
        elif n > 5:
            total += 10 - n
            rem = 1
            if rem2:
                total -= 1
                rem2 = 0
        elif n == 5:
            total += 5
            rem = 0
            rem2 = 1
        else:
            total += n
            rem = 0
            rem2 = 0

    total += rem
    print(total)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = list(input())
n = [int(i) for i in n]
n.reverse()
l = len(n)
ans = 0
for i in range(l):
    if n[i]>5:
        if i!=l-1:
            n[i+1] += 1
        else:
            ans += 1
        ans += 10-n[i]
    else:
        ans += n[i]
print(ans)

Submission status: Wrong Answer

Solution code:
import math
n=int(input())
k=math.floor(math.log10(n))
x=[]
for i in range(k,-1,-1):
    l=n//(10**i)
    x.append(l)
    n-=(l*(10**i))

ans=0
for i in range(k,-1,-1):
    if x[i]<=5:
        ans+=x[i]
    elif x[i]==6:
        ans+=0
    else:
        ans+=(11-x[i])
                
print(ans)

Submission status: Wrong Answer

Solution code:
#import sys
#input = sys.stdin.readline
def main():
    S = [ int(x) for x in input()]
    ans = 0
    N = len(S)
    l = 0
    r = 1
    d = dict()
    d[0] = 0
    d[1] = 1
    d[2] = 2
    d[3] = 3
    d[4] = 4
    d[5] = 5
    d[6] = 4
    d[7] = 3
    d[8] = 2
    d[9] = 1
    for i in range(N):
        if S[i] >= 5 and i < N-1:
            continue
        for s in range(l,r):
            if s == l and S[s] == 9:
                ans += 2
                continue
            ans += d[s]
        l = i
    print(ans)
            
if __name__ == '__main__':
    main()