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=list(map(int,list(input())))

L=[0,1,2,3,4,5,5,4,3,2]

ans=0
mi=0
m=0
j=0
a=0
for i in N:
  ans+=L[i]
  if i>=6:
    if j==1:
      m+=1
    else:
      j=1
      m=0
  else:
    if j==1:
      mi+=m
      if m!=0:
        a+=1
    j=0
  
if N[-1]>=6 and m!=0:
  ans-=1
  
print(ans-2*mi-a)

Submission status: Wrong Answer

Solution code:
P = [0] + list(map(int, list(input())))
n = len(P)

C = [0] * n
for i in range(n - 1, 0, -1):
    if P[i] >= 10:
        P[i - 1] += P[i] // 10
        P[i] %= 10
    if P[i] >= 6:
        C[i] = 10 - P[i]
        P[i] = 0
        P[i - 1] += 1
print(sum(P) + sum(C))

Submission status: Wrong Answer

Solution code:
N = [0]+list(map(int, list(input())))
l = len(N)
kuriagari = 0
i = l-1
ans = 0
while i > -1:
  n = N[i]+kuriagari
  if n <= 5:
    ans += n
    kuriagari = 0
  else:
    ans += 10-n
    kuriagari = 1
  i -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys,collections

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  s=list(S())
  s.reverse()
  ans=0

  stk=0
  for x in s:
    x=int(x)
    x+=stk
    if x>=10:
      stk=1
      x%=10

    if x==0:
      stk=1
      f=False
    elif x<=5:
      ans+=x
      f=False
    else:
      ans+=10-x
      stk=1
      f=True
  
  if f:
    ans+=stk

  return ans

# main()
print(main())

Submission status: Wrong Answer

Solution code:

N = input()
mine = 0
k = True

F=[0]*len(N)

isFive=0
f=True
for i in range(len(N)):
    if int(N[i])!=5:
        for j in range(isFive, i+1):
            F[j]=int(N[i])
        f=True
        isFive=i+1
    elif f:
        isFive=i
        f=False

for i in range(len(N)):
    if int(N[i]) <= 4:
        mine+=int(N[i])*10**(len(N)-i-1)
        k=True
    elif i<len(N)-1:
        if int(N[i]) ==5 and F[i] <= 5:
            mine+=int(N[i])*10**(len(N)-i-1)
            k=True
        elif k:
            mine+=10**(len(N)-i)
            k=False
    elif int(N[i]) <= 5:
        mine+=int(N[i])*10**(len(N)-i-1)
        k=True
    elif k:
        mine+=10**(len(N)-i)
        k=False
staff = mine - int(N) 
M=[int(i) for i in str(mine)]
S=[int(i) for i in str(staff)]
print(sum(M)+sum(S))

Submission status: Wrong Answer

Solution code:
#0-5は0に6以上は繰り上げた数字を考える。
s=input()
#s[i-1]が上からi桁目で10**(n-i)
keta=len(s)
n=int(s)
cnt=0
for i in range(1,keta+1):
    d=int(s[i-1])
    cnt+=min(10-d,d)
print(cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())
l = [int(x) for x in list(str(n))]

dasu=0
turi=0
kuri=False
kuri_cnt=0
for num in l:
    pass
#print(dasu,turi,kuri_cnt)
print(1)

Submission status: Wrong Answer

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

Num_str = list(input().rstrip())
L = len(Num_str)
Num = []
S = 0
for n in reversed(Num_str):
    Num.append(int(n))
    S += int(n)

up = False
for n in Num:
    if up:
        n += 1
    if n == 10:
        delta = -10
    else:
        delta = 1 + (10-n) - n
    if delta <= 0:
        up = True
        S += delta
    else:
        up = False

print(S)

Submission status: Wrong Answer

Solution code:
A = list(map(int, input()))

cnt = 0

crnt = sum(A)

if A[0] >= 6:
	cnt += (11 - (2 * A[0]))
	A = [1,0] + A[1:]

N = len(A)

while True:
	cond = 0
	for i in range(N):
		if A[i] >= 6:
			cnt += (11 - (2 * A[i]))
			A[i] = 0
			A[i-1] += 1
			if A[i-1] >= 6:
				cond = 1

	if cond == 0:
		break

print(crnt + cnt)

Submission status: Wrong Answer

Solution code:
def faa(n,m):
    tmp = 0
    a = n + m
    if n == 9:
        tmp = 1
    else:
        tmp = n+1
    b = tmp + m + 1
    c = tmp + (10-m)

    return min(a,b,c) 

N = input()
if len(N) == 1:
    a = int(N)
    b = 10 - int(N)
    print(min(a,b))
    exit()
ans = 0
for i in range(len(N)-1):
    f = int(N[i])
    ff = int(N[i+1])
    ans += faa(f,ff)
print(ans)

Submission status: Wrong Answer

Solution code:
N = input()
old_tmp = 0
ans = 0
for i in reversed(range(len(N))):
    tmp = int(N[i])
    if old_tmp > 5:
        tmp += 1
    ans += min(tmp, 10 - tmp)
    old_tmp = tmp
    if old_tmp == 10:
        ans -= 1
if tmp > 5:
    ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
n=input()[::-1]
s=[[0,0]for i in range(len(n))]
s[0]=[int(n[0]),10-int(n[0])]
for i in range(len(n)-1):
    s[i+1][1]=min(s[i][0]+10-int(n[i+1]),s[i][1]+9-int(n[i+1]))
    s[i+1][0]=min(s[i][0]+int(n[i+1]),s[i][1]+int(n[i+1])+1,s[i][0]+(2 if n[i+1]=="9" else int(n[i+1])+1))
print(s[-1][0])

Submission status: Wrong Answer

Solution code:
from bisect import *
from collections import *
from fractions import gcd
from math import factorial
from itertools import *
from heapq import *
import copy

#N,K=map(int,input().split())
#A=Sorted(list(map(int,input().split())))


N=list(map(int,list(input())))
count=0
flag=0
for i in range(0,len(N)-1):

    n=int(N[i])
    if n<=4:
        count+=n
        flag=0
    elif n==5:
        if flag==0:
            count+=5
            flag=2
        elif flag==2:
            count+=5
            flag=3
        elif flag==3:
            count+=4
            flag=1
        else:
            count+=9-n
    else:

        if flag==2:
            count+=9-n
        if flag:
            count+=9-n
        else:
            count+=10-n+1
        flag=1
n=int(N[-1])
if n<=4:
    count+=n
    flag=0
else:
    count+=10-n+1
print(count)

Submission status: Wrong Answer

Solution code:
#At coder 2020/2/16 E
import collections

N = int(input())

Ns=str(N)
ans=0
kuri=0
for jj in range(len(Ns)):
    ii=len(Ns)-jj-1
    NNN=int(Ns[ii])+kuri
    if NNN==10:
        kuri=1
        if jj==len(Ns)-1:
            ans=ans+1
            #print('bbb')
    else:
        if NNN>5:
            ans=ans+10-int(NNN)
            kuri=1
#            print(jj-len(Ns)-1)
            if jj==len(Ns)-1:
                ans=ans+1
#                print('aaa')
        else:
            ans=ans+int(NNN)
            kuri=0
    #print(NNN,ans)

print(ans)

Submission status: Wrong Answer

Solution code:
def solve():
    N = int(input())

    N2 = N
    N_s = str(N)
    k = 1
    ans = 0
#    dict = {
#        "1":0,
#        "10":0,
#        "100":0
#    }
    ca = 0
    cb = 0


    while N2>=10:
        N2//=10
        k += 1

    for i in range(k):
        a = int(N_s[i])
        if a>=6:
            ans += 1
            ans += 10-a
            print(a,"a",ans)
#            dict[str(10**(k-i))] += 1
            ca += 1
        else:
            ans += a
            print(a,"b",ans)
#            dict[str(10**(k-i-1))] += a
            cb += 1
    print(ans,ca,cb)
solve()

Submission status: Wrong Answer

Solution code:
N=input()
l=len(N)
s=0
for i in range(l-1,-1,-1):
    n=int(N[i])
    if n<6:s+=n
    else:s+=11-n
print(s)

Submission status: Wrong Answer

Solution code:
n = input()
keta = len(n)
#print(keta)
sub_cnt = 0
keta_flg = 0
for i in range(keta-1,0,-1):
    if keta_flg % 2 == 0:
        if int(n[i])>5:
            sub_cnt+=(10-int(n[i]))
            keta_flg+=1
        else:
            sub_cnt+=int(n[i])
    else:
        if int(n[i]) > 4:
            sub_cnt+=(9-int(n[i]))
            #keta_flg=1
        else:
            sub_cnt+=(int(n[i])+1)
            keta_flg+=1
    #print("i",i,"sub_cnt",sub_cnt)
if keta_flg% 2 == 1:
    if int(n[0]) > 5:
        sub_cnt+=(9-int(n[0]))
    else:
        sub_cnt+=(int(n[0])+1)
else:
    if int(n[0]) > 4:
        sub_cnt+=(10-int(n[0]))
    else:
        sub_cnt+=int(n[0])
print(sub_cnt)

Submission status: Wrong Answer

Solution code:
n = list(map(int, input()[::-1]))

ans = f = go = cnt = 0
if n[0]==5: ans+=1
for i in n:
    if go:
        if i>5:
            ans += 9-i-f+4*cnt+1
            f = 1
            go = 0
            cnt = 0
        elif i+f==5:
            cnt += 1
            f = 0
            go = 1
        else:
            ans += i+f+5*cnt
            f = 0
            go = 0
            cnt = 0
    elif i+f>5:
        ans += 10-i-f
        f = 1
        go = 0
    elif i+f==5:
        cnt = 1
        f = 0
        go = 1
    else:
        ans += i+f
        f = 0
        go = 0

if go: print(ans+f+4*cnt+1)
else: print(ans+f)

Submission status: Wrong Answer

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

L=[0,1,2,3,4,5,5,4,3,2]

ans=0
mi=0
m=0
j=0
a=0
for i in N:
  ans+=L[i]
  if i>=6:
    if j==1:
      m+=1
    else:
      j=1
      m=0
  else:
    if j==1:
      mi+=m
      if m!=0:
        a+=1
    j=0
  
if N[-1]>=6:
  ans-=1
  
print(ans-2*mi-a)

Submission status: Wrong Answer

Solution code:
l=list(map(int,input()))
l=l[::-1]
m=0
k=-1
for i in range(len(l)):
    if l[i]<6:
        if i!=k+1:
            for j in range(k+1,i):
                if j==k+1:
                    m+=10-l[j]
                else:
                    m+=10-l[j]-1
            m+=l[i]+1
        else:
            m+=l[i]
        k=i

if l[len(l)-1]>5:
    for i in range(k+1,len(l)):
        if i==k+1:
            m+=10-l[i]
        else:
            m+=10-l[i]-1
    m+=1
print(m)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import copy
N=int(input())
m=len(str(N))
N1=copy.copy(N)
m2=copy.copy(m)

n=str(N)
ans=0
for i in range(m):
    m1=int(n[i])
    if m1<=5:
        ans+=m1
    else:
        ans+=10-m1

if m<=2:
    print(ans+1)
else:
    print(ans)
#print(ans)

Submission status: Wrong Answer

Solution code:
S = input()
cnt = 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]))
else:
    cnt = min(1+(10-int(S[-1])), 1+1+(10-int(S[-2])%10), int(S[-2])+int(S[-1]))
#print(cnt)
#cnt = 4

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


print(cnt)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
from collections import deque, defaultdict
import bisect


def comb(a, b):
    result = 1
    for i in range(b):
        result *= (a - i) / (i + 1)
    return result


def main():
    N = list(input().strip())

    num = 0
    levelup = False
    for n in N[::-1] + [0]:
        n = int(n)

        if levelup:
            n += 1
            levelup = False
        if n == 10:
            levelup = True
            num -= 1
            n = 0
        if n <= 5:
            num += n
        else:
            num += 10 - n
            levelup = True

    print(num)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys,collections

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  s=list(S())
  s.reverse()
  ans=0

  stk=0
  for x in s:
    x=int(x)
    x+=stk
    if x>=10:
      stk=1
      x%=10

    if x==0:
      stk=1
    elif x<=5:
      ans+=x
    else:
      ans+=10-x
      stk=1
  
  if s[-1]=='9':
    ans+=stk

  return ans

# main()
print(main())

Submission status: Wrong Answer

Solution code:
n = input()
l = len(n)
dp = [0] *(l+1)
for i in range(l):
    nowk = int(n[i])
    dp[i+1] = min(dp[i]+nowk,dp[i]+11-nowk)
print(dp[l])

Submission status: Wrong Answer

Solution code:
N = input()
A = [int(N[i]) for i in range(len(N))]
A.reverse()
A.append(0)

ans = 0

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

print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    s = input()

    ret = 0
    carry = 0
    buf = 0
    flg = False
    for c in reversed(s):
        n = int(c)
        nc = n + carry
        if nc == 0:
            continue
        elif nc <= 5:
            ret += nc + (buf + 1 if flg else 0)
            buf = 0
            carry = 0
            flg = False
        else:
            carry = 1
            buf += 9 - n
            flg = True
    if carry > 0:
        ret += carry + (buf + 1 if flg else 0)

    print(ret)


if __name__ == '__main__':
    main()

# import sys
#
# sys.setrecursionlimit(10 ** 7)
#
# input = sys.stdin.readline
# rstrip()
# int(input())
# map(int, input().split())

Submission status: Wrong Answer

Solution code:
N = input()
M = len(N)
ca, cb, flg = 0, 0, 0
for i in range(M, 1, -1):
  if int(N[i-1]) < 5:
    ca += int(N[i-1])
  elif int(N[i-1]) >= 5:
    cb += 10 - int(N[i-1])
if M > 1:
  if int(N[1]) > 5:
    if 5 <= int(N[0]) <= 9:
      ca += 1
      cb += 9 - int(N[0])
    else:
      ca += int(N[0]) + 1
  else:
    if 5 <= int(N[0]) <= 9:
      ca += 1
      cb += 9 - int(N[0])
    else:
      ca += int(N[0])
else:
  if 5 <= int(N[0]) <= 9:
    ca += 1
    cb += 9 - int(N[0])
  else:
    ca += int(N[0])
print(ca + cb)

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]<6:
    ans+=k[i]
  else:
    ans+=10-k[i]
    k[i+1]+=1
print(ans-6)

Submission status: Wrong Answer

Solution code:
n = int(input())
sn = "0" + str(n)
ans = 0
ren = 0
now = 0
for i in reversed(sn):
  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:
q=list(input())
li=[0,1,2,3,4,5,5,4,3,2]
p=[int(i) for i in q]
cnt=0
for i in range(len(p)-1):
    cnt+=(p[i]>=6 and p[i+1]>=6)

ans=0
for k in q:
    ans+=li[int(k)]
print(ans-2*cnt)

Submission status: Wrong Answer

Solution code:
N = input()

count = 0
for i in range(len(N)-1, -1, -1):
    if int(N[i]) <= 4:
        count += int(N[i])
    else:
        count += 10 - int(N[i])

if int(N[-1]) == 0:
    count -= 1
print(count + 1)

Submission status: Wrong Answer

Solution code:
S = input()

c = [ 0, 1, 2, 3, 4, 5, 4, 3, 2, 1 ]
ans =0
kuri = 0
for i in range(len(S)-1, -1, -1):
    d = int(S[i])
    if 0 <= d + kuri <= 5:
        ans += c[d+kuri]
        kuri = 0
    else:
        ans += c[(d+kuri)%10]
        kuri = 1 + (d+kuri >= 10)
ans += kuri

print(ans)

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 and i != s-1:
            ans -= 1
        continue

print(ans + flag)

Submission status: Wrong Answer

Solution code:
S = ''.join(list(reversed(input())))
flg = 0
ans = 0
for i in S:
    j = int(i) + flg
    if j >= 6:
        ans += 10- j
        flg = 1
    else:
        ans += j
        flg = 0
print(ans + flg)

Submission status: Wrong Answer

Solution code:
#e
list_n=[0]+list(map(int,input()))
l = len(list_n)

pay=0
back=0
for i in range(l-1,-1,-1):
    #l-1から0まで
    now = list_n[i]
    if now <=5:
        pay+=now
        list_n[i]=0
    elif now==10:
        list_n[i]=0
        list_n[i-1] +=1
    else:
        back+=10-now
        list_n[i]=0
        list_n[i-1] +=1
print(pay+back)

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 += 11 - (int(N[i]) - prev)
    prev = 1
  else:
    prev = 1
print(count)

Submission status: Wrong Answer

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

ans = 0

for i in range(len(S)):
    if i == 0:
        ans += int(S[0])
        continue
    if S[i] == '0':
        pass
    elif int(S[i]) > 5:
        ans += 10 - int(S[i]) + 1
    else:
        ans += int(S[i])

print(ans)

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     

def main():
    N = S()
    dp = [inf for _ in range(len(N)+1)]
    over = False
    for i in range(1, len(N) + 1):
        num = int(N[i-1])
        next_bill_ten = 1
        tsuri_ten = 10 - num

        next_bill = num + 1
        tsuri = 1

        a = num
        b = next_bill + tsuri
        c =  next_bill_ten + tsuri_ten

        if i == 1:
            if a == min(a, b, c):
                dp[i] = a
            elif b == min(a, b, c):
                dp[i] = b
                over = True
            else:
                dp[i] = c
                over = True

        else:
            if a == min(a, b, c):
                dp[i] = dp[i-1] + a
                if over:
                    dp[i] = min(dp[i], dp[i-1] + (10 - num))

            elif b == min(a, b, c):
                dp[i] = dp[i-1] + b
                if over:
                    dp[i] = min(dp[i], dp[i-1] + (10 - num))
                over = True
            else:
                dp[i] = dp[i-1] + c
                if over:
                    dp[i] = min(dp[i], dp[i-1] + (10 - num))
                over = True
    print(dp[len(N)])

def _main():
    N = S()
    otsuri_digit = []
    for digit in N:
        tsuri = 10 - int(digit)
        otsuri_digit.append(tsuri)
    otsuri_digit_cumsum = [0] + [0] * len(N)
    print(otsuri_digit)
    for i in range(len(N)):
        otsuri_digit_cumsum[i+1] = (
            otsuri_digit_cumsum[i] + otsuri_digit[i]
        )
        if i != (len(N) - 1):
            otsuri_digit_cumsum[i+1] -= 1
    ans = sum([int(s) for s in N])
    buf = 0
    print(otsuri_digit_cumsum)
    for ix, digit in enumerate(N):
        if ix == len(N) - 1:
            break
        cur_ans = buf + int(digit) + 1 + (otsuri_digit_cumsum[-1] - otsuri_digit_cumsum[ix+1])
        buf += int(digit)
        ans = min(cur_ans, ans)
    print(ans)
main()

Submission status: Wrong Answer

Solution code:
list_n=[0]+list(map(int,input()))
l = len(list_n)

pay=0
back=0
for i in range(l-1,-1,-1):
    #l-1から0まで
    now = list_n[i]
    if now <=5:
        pay+=now
        list_n[i]=0
    elif now==10:
        list_n[i]=0
        list_n[i-1] +=1
    else:
        back+=10-now
        list_n[i]=0
        list_n[i-1] +=1
    print(i,list_n,pay,back)
print(pay+back)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n=input().strip()
ans=0
nxt=0
for i in range(len(n)):
    a=int(n[-i-1])+nxt
    #print(a)
    if a<=5:
        ans+=a
        nxt=0
    elif 6<=a<=9:
        ans+=10-a
        nxt=1
    elif a==10:
        ans+=10-a-1
    #print(ans)
a=nxt
if a<=5:
    ans+=a
elif a>=6:
    ans+=10-a
print(ans)

Submission status: Wrong Answer

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

over = 0
count = 0
for i in range(n, 0, -1):
    s = int(N[i-1])
    if s < 5:
        count = count + s + over
        over = 0
    elif s == 5:
        if over == 0:
            count = count + s
            over = 0
        else:
            if i > 2 and int(N[i-2]) >= 5:
                count = count + (10 - s - over)
                over = 1
            elif i == 1:
                count = count + (10 - s - over)
                over = 1
            else:
                count = count + s + over
                over = 0
    else:
        if s == 9 and over == 1:
            over = 1
        else:
            count = count + (10 - s - over)
            over = 1

if over == 1:
    count = count + over

print(str(count))

Submission status: Wrong Answer

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

L=[0,1,2,3,4,5,5,4,3,2]

ans=0
mi=0
m=0
j=0
a=0
for i in N:
  ans+=L[i]
  if i>=6:
    if j==1:
      m+=1
    else:
      j=1
      m=0
  else:
    if j==1:
      mi+=m
      if m!=0:
        a+=1
    j=0
  
  
print(ans-2*mi-a)

Submission status: Wrong Answer

Solution code:
N = input()

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

Submission status: Wrong Answer

Solution code:
from bisect import *
from collections import *
from fractions import gcd
from math import factorial
from itertools import *
from heapq import *
import copy

#N,K=map(int,input().split())
#A=Sorted(list(map(int,input().split())))


N=list(map(int,list(input())))
count=0
flag=0
for i in range(0,len(N)-1):

    n=int(N[i])
    if n<=4:
        count+=n
        flag=0
    elif n==5:
        if flag==0:
            count+=5
            flag=2
        elif flag==2:
            count+=5
            flag=3
        elif flag==3:
            count+=5
            flag=1
        else:
            count+=9-n
    else:

        if flag==2:
            count+=9-n
        if flag:
            count+=9-n
        else:
            count+=10-n+1
        flag=1
n=int(N[-1])
if n<=4:
    count+=n
    flag=0
elif n==5:
    if flag==0:
        count+=5
    else:
        count+=6
else:
    if flag:
        count+=9-n
    else:
        count+=10-n+2
print(count)

Submission status: Wrong Answer

Solution code:
def ks(x):
    s = str(x)
    l = list(s)
    l = list(map(int, l))
    return sum(l)

N = input()
L = len(N)
dp = [[10**18]*2 for _ in range(L+1)]
dp[0][0] = 0

for i in range(L):
    D = int(N[i])
    
    for j in range(2):
        for d in range(10 if j else D+1):
            dp[i+1][j|(d<D)] = min(dp[i+1][j|(d<D)], dp[i][j]+d+ks(d+D))

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

Submission status: Wrong Answer

Solution code:
N = int(input())

n = N
ans = 0
for i in range(N):
    d = n % 10
    n //= 10
    ans += min(d, 1 + 10 - d)
    if n == 0: break

print(ans)

Submission status: Wrong Answer

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

s = 0
up = 0
for i, digit in enumerate(digits[::-1], 1):
    if digit + up == 10:
        up = 1
    elif digit + up <= 5:
        s += digit + up
        up = 0
    else:
        s += 10 - digit - up
        up = 1
else:
    s += up
print(s)

Submission status: Wrong Answer

Solution code:
N=list(input())

N.insert(0,0)
ans=0
cnt=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>=10-n:
    ans+=10-n
    N[i-1]=str(int(N[i-1])+1)
  else:
    ans+=n
  if i!=len(N)-1:
    if int(N[i])==int(N[i-1])-1==9: cnt+=1
    
print(ans-cnt)

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 += 4
            N[i - 1] += 1
        else:
            ans += N[i]
    elif N[i] < 5:
        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=input()
n='0'+n
n=list(n)
n.sort(reverse=True)
ans=0
i=0
while i<len(n):
    a=int(n[i])
    if a<=5:
        ans+=a
        i+=1
    else:
        ans+=1
        block=a
        j=1
        while int(n[i+j])>=5:
            block+=10**j*(int(n[i+j]))
            j+=1
        minus=10**j-block
        for k in range(len(str(minus))):
            ans+=int(str(minus)[k])
        i+=j
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 > 4:
            cnt += (10 - j)
            n[i+1] += 1

print(cnt)

Submission status: Wrong Answer

Solution code:
#DPでしょ、DPなんでしょ！？お姉さん知ってるんだから！ﾌﾟﾘﾌﾟﾘ

Submission status: Wrong Answer

Solution code:
s = list(input())[::-1]
n = len(s)
ans = 0
for i, x in enumerate(s):
	x = s[i]
	k = int(x)
	if k <= 4 or (k == 5 and (i == n-1 or (i < n-1 and int(s[i+1]) <= 4))):
		ans += k
	else:
		ans += 10-k
		s[i] = "0"
		if i == n-1:
			ans += 1
		else:
			if i < n-1 and s[i+1] != "9":
				s[i+1] = str(int(s[i+1])+1)
			else:
				j = i+1
				while s[j] == "9":
					s[j] = "0"
					j += 1
					if j == n:
						ans += 1
						break
	#print("".join(s[::-1]))
print(ans)

Submission status: Wrong Answer

Solution code:
#from math import sqrt
#from heapq import heappush, heappop
#from collections import deque

#a, b = [int(v) for v in input().split()]

n = input()

flag = 0
count = 0
for i in range(len(n) - 1, -1, -1):
    c = n[i]
    v = int(c) + flag
    flag = 0
    if v < 5:
        count += v
    elif v == 5:
        if i != 0 and int(n[i-1]) >= 5:
            flag = 1
        count += 5
    else:
        count += 10 - v
        flag = 1
print(count)

Submission status: Wrong Answer

Solution code:
N = _N = int(input())
K = len(str(N))
ans = 0
d = {i:0 for i in range(0, K+1)}

kuriagari = True
for i in range(K-1, -1, -1):
    div, mod = divmod(_N, 10**i)
    if div == 5:
        if kuriagari:
            d[i] += div
            if d[i] == 6:
                d[i] = 0
                d[i+1] += 1
            kuriagari = True
        else:
            kuriagari = False
    elif div <= 5:
        d[i] += div
        kuriagari = True
        if d[i] == 6:
            d[i] = 0
            d[i+1] += 1
    elif kuriagari:
        d[i+1] += 1
        kuriagari = False
        if d[i+1] == 6:
            d[i+1] = 0
            d[i+2] += 1
    else:
        kuriagari = False
    _N = mod
# print(d)

total = 0
for k, v in d.items():
    total += 10**k * v

otsuri = total - N
# print('total', total)
# print('otsuri', otsuri)

for i in str(total):
    ans += int(i)

for i in str(otsuri):
    ans += int(i)

print(ans)

Submission status: Wrong Answer

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

cash = 0

for i in range(order):
	if int(N[-(i+1)]) >=6:
		cash += 10**(i+1)
	else:
		cash += int(N[-(i+1)]) * (10**(i))
back = cash-int(N)

num=0

for a in str(cash):
	num += int(a)
for a in str(back):
	num += int(a)

print(num)

Submission status: Wrong Answer

Solution code:
N = input()

maisu = 0
kag = 0

for i in range(1,len(N)+1):
    m = int(N[-i]) + kag
    if m < 6:
        maisu += m
        kag = 0
    elif m == 10:
        kag = 1
    else:
        maisu += 10 - m
        kag = 1
if kag == 1:
    maisu += 1
    
print(maisu)

Submission status: Wrong Answer

Solution code:
import collections

nn = list(input())

def main(nn):
	ll = []
	tmp = []
	ans = 0
	for i in reversed(range(len(nn))):
		if int(nn[i]) <= 4:
			if len(tmp) >= 1:
				ll.append(tmp)
				tmp = []
			ans += int(nn[i])
		else:
			tmp.append(nn[i])
	if len(tmp) >= 1:
		ll.append(tmp)

	for l in ll:
		if len(l) == 1 and l[0] == "5":
			ans += 5

		elif collections.Counter(l)["5"] >= len(l):
			ans += sum([int(x) for x in list(str(10 ** len(l) - int("".join(l))))]) + 2
		else:
			ans += sum([int(x) for x in list(str(10 ** len(l) - int("".join(l))))]) + 1
	return ans

# for i in range(1, 1001):
# 	print(i, main(str(i)))


# print(main("55"))
print(main(nn))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=input()
dic={str(i):i for i in range(11)}
for i in range(6,10):
    dic[str(i)]=11-i

ans=0

for i in range(len(n)):
    ans+=min(dic[n[i]],dic[str(10-int(n[i]))])
    
if n[0]=="9":
	ans+=1
print(ans+1)

Submission status: Wrong Answer

Solution code:
N = input()

ret = 0
up = 0

for r in N[::-1]:
  m = int(r)
  ret += min(10 - m, m)
  if (10 - m) < m:
    up = 1

print(ret + up)