Problem:


Score: 500 points

Problem Statement

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

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

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

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

Constraints

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

Input

Input is given from Standard Input in the following format:

N

Output

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

Sample Input 1

36

Sample Output 1

8

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

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

Sample Input 2

91

Sample Output 2

3

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

Sample Input 3

314159265358979323846264338327950288419716939937551058209749445923078164062862089986280348253421170

Sample Output 3

243



Sample input:
36


Sample output:
8

Submission status: Wrong Answer

Solution code:
S = input()
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]))
#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:
N = int(input())
N_list = list(map(str, str(N).split()))
for i in range(len(N_list)):
  N_list[i] = int(N_list[i])
ans = 0

while len(N_list) != 0:
  print(N)
  if N_list[0] >= 6:
    ans += 10 - N_list[0]
  else:
    ans += N_list[0]
  
  N -= N_list[0] * (len(N_list))
  del N_list[0]
  
print(ans)

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-1]) == 9 and int(n[i]) + flag == 5):
            ans += 10 - int(n[i])
            flag = 1
        else:
            ans += int(n[i]) + flag
            flag = 0

    if flag == 1:
        ans += 1

    print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
N_str = str(input())

ans = 0
before_adjuster = 0
before_adjuster_5 = 0
for i in range(len(N_str)):
    now_num = int(N_str[i])
    if now_num <= 5:
        ans += now_num
    else:
        ans += 10 - now_num

    if now_num <= 4:
        before_adjuster = 0
        before_adjuster_5 = 0
        if before_adjuster_5 == 5:
            ans -= 1
    else:
        ans -= before_adjuster
        if before_adjuster == 0:
            ans += 1
        before_adjuster = 1
        if now_num == 5:
            before_adjuster_5 = 1
        else:
            before_adjuster_5 = 0

if before_adjuster_5 == 1:
    ans -= 1
print(ans)

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 round(Ncopy[i],-1) == 0:
        ans += Ncopy[i]
        flag = False
    else:
        Ncopy[i-1] += 1
        if not flag:
            ans += 10 - N[i]
        else:
            ans += 9 - N[i]
        flag = True

if Ncopy[0] == 1:
    ans += 10 - N[i]

print(ans)

Submission status: Wrong Answer

Solution code:
import collections
n = list(map(int, input()))
l = len(n)

res = 0
for i in range(1, l+1):
    tmp = n[-i]
    if tmp <= 5:
        res += tmp
    else:
        res += 10 - tmp
        if i < l: 
            n[-i-1] += 1
        else:
            res += 1

print(res - n.count(10))

Submission status: Wrong Answer

Solution code:
N=list(reversed(input()))
ans=0
old=0
k=[]
for n in N:
    if int(n)+old<=5:
        ans+=int(n)+old
        k.append([int(n)+old,int(n)])
        old=0
    else:
        ans+=10-(int(n)+old)
        k.append([10-(int(n)+old),int(n)])
        old=1
if old==1:
    ans+=1
print(ans)

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 i==len(n):
        if a<=5:
            ans+=a
            nxt=0
        elif 6<=a:
            ans+=10-a
            nxt=1
            break
    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()

ans = 0
flag = False
for i in range(len(N)):
    now = int(N[i])
    if flag == True:
        if 5 <= now and now <= 9:
            ans -= 1
    if 1 <= now and now <= 5:
        ans += now
    elif 6 <= now and now <= 9:
        if flag == True:
            ans += 10 - now
        else:
            ans += 1 + 10 - now
            flag = True
        

print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    n_str = input()
    n = [int(s) for s in reversed(n_str)]
    n.append(0)
    n.append(0)

    for i in range(len(n_str)):
        if n[i] > 5:
            n[i] = 10-n[i]
            n[i+1] += 1
        elif n[i] == 5 and n[i+1] > 5:
            n[i+1] = 9-n[i+1]
            n[i+2] += 1

    print(sum(n))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:

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

ans = 0

num_0to5 = [i for i in l if i <= 5]
num_6to9 = [i for i in l if i >= 6]

ans = sum(num_0to5)
ans += 11*len(num_6to9)-sum(num_6to9)


if l[-1] <=5:
    ans -= len(num_6to9)
else:
    ans -= len(num_6to9)-1

if l[0] >= 6:
    ans += 1


dd = 0
for i in l:
    if i >= 6:
        dd += 1
    else:
        dd = 0

if len(l)>=2:
    if l[-2] <= 5 and l[-1] >= 6:
        dd -= 1

ans -= dd

print(ans)

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
        if i < len(n)-1:
            if nn != 0 and int(n[i+1]) >= 5:
                ans += 1
    print(ans)

Submission status: Wrong Answer

Solution code:
X = input()
N = list(X)
ans1 = len(N)*5
A = 0
for i in N:
    ans1 -= abs(5-int(i))
    if (A >0 and int(i)> 5):
        ans1 += 1
    elif (A < 0 and int(i)< 5):
        ans1 += 1
    
    if 5-int(i)>0:
        A = 1
    elif 5 - int(i)<0:
        A = -1
    else:
        A = 0
        
Y = 10**len(N)-int(X)
N = list(str(Y))
ans2 = len(N)*5
A = 0
for i in N:
    ans2 -= abs(5-int(i))
    if (A >0 and int(i)> 5):
        ans2 += 1
    elif (A < 0 and int(i)< 5):
        ans2 += 1
    
    if 5-int(i)>0:
        A = 1
    elif 5 - int(i)<0:
        A = -1
    else:
        A = 0
print(min(ans1,ans2+1))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
n = int(input())
nl = list(map(int, list(str(n))))
pay = 0
rev = False
for i in range(len(nl)):
    if rev:
        nl[i] = 10 - nl[i]
    if nl[i] <= 5:
        pay += nl[i]
    else:
        pay += 10 - nl[i]
        rev = not rev
print(pay)

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]) + 1
    List[L-i] = str(b)
  else:
    cnt = cnt + a 
    
print(cnt)

Submission status: Wrong Answer

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

l = [0] * (len(n)+1)
carry = 0
for i, k in enumerate(n):
  if k > 5 - carry:
    l[i+1] = k - 10
    l[i] += 1
    carry = 1
    while l[i] >= (5 if l[i-1] < 0 else 6):
      l[i] -= 10
      i -= 1
      l[i] += 1
      if i == 0:
        break
  else:
    l[i+1] = k
    carry = 0
print(sum(abs(i) for i in l))

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]) <= 3))):
		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:
n=int(input())
s=str(n)
l=[0,1,2,3,4,5,5,4,3,2]
c=0
for i in range(len(s)-1,-1,-1):
	c+=l[int(s[i])]
s='0'+s+'0'+'0'
for i in range(1,len(s)-1):
	if int(s[i+1])>=6 and int(s[i])>=6:
		c-=2
	if int(s[i+1])>=6 and int(s[i])==5:
		c-=2
		if int(s[i])==5 and int(s[i-1])<=4:
			c+=1
	if int(s[i+1])==5 and int(s[i])>=6:
		c-=2
		if int(s[i+2])<=4 and int(s[i+1])==5:
			c+=1
print(c)

Submission status: Wrong Answer

Solution code:
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]
    print(ans)
            
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#E - Payment
N = str(input())
count = 0
for i in range(len(N)):
    if int(N[i]) > 5:
        count += 1
    elif int(N[i]) == 5:
        count += 5
    else:
        count += int(N[i])+1

print(count)

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 int(d) == 0:
        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:
# print('input >>')
N = input()
N = reversed(N)
ans = 0
nxt = False
for n in N:
    i_n = int(n) + nxt
    if i_n % 10 >= 6:
        ans += 10 - i_n % 10
        nxt = True
    else:
        ans += i_n % 10
        nxt = False
    # print(N, ans)

# print('-----output-----')
print(ans)

Submission status: Wrong Answer

Solution code:
n = input()
answer = 0
for x in n:
  num = int(x)
  if num > 5:
    answer += 1
    answer += 10-num
  else:
    answer += num
print(answer)

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 <= 5 :
    is_up = False
    ans += 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:
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+1] > 5:
      culc[i+1] = (10 - culc[i+1])

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

Submission status: Wrong Answer

Solution code:
''' Hey stalker :) '''
INF = 10**10
def main():
    #print = out.append
    ''' Cook your dish here! '''
    n = list(map(int, list(input())))
    index = len(n)
    for i in reversed(n):
        index -= 1
        if i!=0: break
    inverted = False
    res = 0
    for i, ele in enumerate(n):
        if ele==0: continue
        if inverted:
            ele = 9 - ele
            if i==index: ele+=1
        if ele>5:
            inverted = not inverted
            ele = 9 - ele
            if i == index: ele += 1
            res += 1

        res += ele
        #print(i, ele, res)
    print(res)




''' Pythonista fLite 1.1 '''
import sys
from collections import defaultdict, Counter
#from bisect import bisect_left, bisect_right
#from functools import reduce
#import math
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
out = []
get_int = lambda: int(input())
get_list = lambda: list(map(int, input().split()))
main()
#[main() for _ in range(int(input()))]
print(*out, sep='\n')

Submission status: Wrong Answer

Solution code:
m = input()

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

a = digitSum(m)
n = 0

for i in range(len(a)):
    if a[i] < 6:
        n += a[i]
    else:
        n = n + 10 - a[i] 

print(n)

Submission status: Wrong Answer

Solution code:
n = input()

cnt = 0
is_first = True
for i in list(n):
    if is_first:
        cnt = i
    else:
        if i > 5:
            cnt += 10 - i + 1
        else:
            cnt += i
print(cnt)

Submission status: Wrong Answer

Solution code:
n = input()

ans = 0
add = 0
c = 0
for i in range(len(n)-1,-1,-1):
    d = int(n[i]) + add
    #print('d:',d)
    if d <= 5:
        ans += d
        add = 0
    else:
        ans += 10-d            
        if i == 0:
            ans += 1
        elif d == 10:
            c += 1
        else:
            add = 1

print(ans-c)

Submission status: Wrong Answer

Solution code:
N = str(input())
#print(len(N))
ans = 0
j = 0
y = 0
for i in range(1, len(N) + 1):
  x = int(N[-i]) + j
  if i > 101:
    ans += x
    j = 0
  else:
    if x < 6:
      ans += x
      j = 0
      if y == 5:
        ans -= 1
    else:
      ans += 10 - x
      j = 1
  y = x
if j == 1:
  ans += 1
else:
  if y == 5:
    ans -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
N = input()
N = reversed(N)
cnt = 0
up = False
for s in N:
    n = int(s)
    if up:
        n += 1
        up = False
    if n <= 5:
        cnt += n
    elif n == 10:
        up = True
    else:
        cnt += 10 - n
        up = True
if up:
    cnt += 1
print(cnt)

Submission status: Wrong Answer

Solution code:
S=input()
ans=5*len(S)
ans-=abs(5-int(S[0]))
pre=int(S[0])
flag=True
for i in range(1, len(S)):
    cur=int(S[i])
    ans-=abs(cur-5)
    if flag:
        if pre<=4 and cur>=6 or pre>=6 and cur<=4:
            ans+=1
            flag=False
    else:
        flag=True
    pre=cur
print(ans)

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] <= 5:
    res += S[i]
  else:
    res += 10 - S[i]
    S[i - 1] += 1
print(res)

Submission status: Wrong Answer

Solution code:

S = list(input())
#%%
osatsu = [0,1,2,3,4,0,0,0,0,0]
otsuri = [0,0,0,0,0,5,4,3,2,1]

news = [0]*len(S)
flag = False
ans = 0
for i,s in enumerate(reversed(S)):
    t = int(s)
    if flag and t<5:
        t += 1
    if t>=5:
        flag = True
        news[i] = 0
    else:
        flag = False
        news[i] = t
        ans += t

news.reverse()
if flag:
    ans += 1

agari = False
L = len(S)
for i in reversed(range(L)):
    s = int(S[i])
    n = news[i]
    if agari:
        if n == 0:
            n = 9
            agari = True
        else:
            n -= 1
            agari = False
            
    if n >= s:
        ans += n - s
        agari = False
    else:
        agari = True
        n += 10
        ans += n - s
    
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 = a + 1
    List[L-i] = str(b)
  else:
    cnt = cnt + a 
    
print(cnt)

Submission status: Wrong Answer

Solution code:
n=list(input())[::-1]
m=len(n)

ans=0
for i in range(m):
    if int(n[i])>5:
        ans +=10-int(n[i])+1
    else:
        ans +=int(n[i])
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 < 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
count = count + prev
print(count)

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] )
  print(a)
  
  if i == 0:
    cnt = cnt + 10 - a 
  elif a > 5:
    cnt = cnt + 10 - a 
    b = a + 1
    List[L-i] = str(b)
  else:
    cnt = cnt + a 
    
print(cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())
ans = 0

while n > 0:
    a = n % 10
    if a <= 4 or  (a == 5 and n % 100 // 10 != 9):
        ans += a
    else:
        ans += (10 - a)
        n += 10
    n = n // 10

print(ans)

Submission status: Wrong Answer

Solution code:
n = input()
print(len(n))
n_list = [int(i) for i in n]

ans = 0

'''
10^100+1の効果

'''

for i in n_list:
    #print(type(c))
    if i >= 0 and i <= 5:
        ans += i
    else:
        ans += (11 - i)

print(ans)

Submission status: Wrong Answer

Solution code:
N = input()

t = 0
for i in N:
    num = int(i)
    if 1<=num<=5:
        t += num
    elif 5<num:
        t += 1+(10-num)
    else:
        continue

print(t)

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':
    n = int(input())

    # print(n)
    count = 0
    after_n = n
    for i in range(10**101 + 1):
        value = after_n % 10
        if value <= 5:
            count += value
        else:
            count += (10 - value)
            after_n += 10

        after_n = after_n // 10

        # if value <= 5:
        #     print(after_n, value)
        # else:
        #     print(after_n, (10 - value))

        if after_n == 0:
            # print(i)
            break

    print(count)

Submission status: Wrong Answer

Solution code:
x = int(input())
y = str(x)
a = list(map(int, y))
flag = False
sum = 0
i = len(a)-1
while i >= 0:
  if a[i] > 5:
    if i > 0:
      a[i] = 10 - a[i]
      a[i-1] = a[i-1] + 1
    else:
      a[i] = 10 - a[i]
      sum = sum + 1
  sum = sum + a[i]
  i = i - 1
  
print(sum)

Submission status: Wrong Answer

Solution code:
n = int(input())
b = 0
a = []
b = 0
while n > 0:
    a.append(n%10)
    n //=10
l = len(a)
res = 0
for i in range(0,l-1):
    if a[i] <= 5:
        res += a[i]
    elif a[i] == 10:
        a[i+1] += 1
    else:
        res += 11-a[i]
        a[i+1] += 1
if a[l-1] <= 5:
    res += a[l-1]
elif a[l-1] == 10:
    res += 1
else:
    res += 11 - a[l-1]

print(res)

Submission status: Wrong Answer

Solution code:
n = input()

pay = 0
ret = 0
tmp = 0
for e in n[::-1]:
    e = int(e)
    e += tmp
    if e <= 5:
        pay += e
        tmp = 0
    else:
        ret += 10 - e
        #tmp = 1

if tmp:
    pay += tmp

#print(pay, ret)

ans = pay + ret

print(ans)

Submission status: Wrong Answer

Solution code:
n = input()
answer = 0
for x in n:
  num = int(x)
  if num > 5:
    answer += 1
    answer += 1-num
  else:
    answer += num
print(answer)

Submission status: Wrong Answer

Solution code:

N = int(input())
S = str(N)

len_s = len(S)
dp = [[None, 0] for i in range(len_s+1)]
dp[0] = [0,0]

for i in range(1,len_s+1):
  upper = dp[i-1][1] == 1 # 前の桁が6~9だったので繰り上げ

  t = int(S[len_s- i])

  if t == 0:
    dp[i][0] = 1 if upper else 0
    continue
  elif t <= 5:
    dp[i][0] = t+1 if upper else t
  else:
    dp[i][1] = 1
    dp[i][0] = 10 - t
else:
  if dp[-1][1] == 1:
    dp[-1][0] += 1

ans = 0
for d in dp:
  ans += d[0]
  
print(ans)

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+=10-n
print(s)

Submission status: Wrong Answer

Solution code:
N = list(input())
c = 0
t = 0
if int(N[0]) >= 6:
    ans = 11 - int(N[0])
    c = 1
else:
    ans = int(N[0])

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

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

Submission status: Wrong Answer

Solution code:
# coding:utf-8

import sys
import math
import time
import numpy as np
import collections
from collections import deque
import queue
import copy

# 最大公約数
def gcd(m,n):
  x = max(m,n)
  y = min(m,n)
  if x%y == 0:
    return y
  else:
    while x%y != 0:
      z = x%y
      x = y
      y = z
    else:
      return z

# 幅優先探索(BFS)でスタート座標(x, y)からの最大距離返す
def bfs(SS, x, y, H, W):#入力(座標系(迷路), x, y, xの端, yの端)
  x_next = [0, 0, -1, 1]
  y_next = [1, -1, 0, 0]
  tempx = x
  tempy = y
  mlength = 0
  q = deque()
  xytuple = (x, y, 0)
  q.append(xytuple)


  while q:
    xytuple = q.popleft()
    SS[xytuple[0]][xytuple[1]] = "x" #探索した場所は探索できないようにする
    for i in range(4): #このforで上下左右を調べてる
      tempx = xytuple[0] + x_next[i]
      tempy = xytuple[1] + y_next[i]
      #print(tempx, tempy, xytuple[2])
      #print(S)
      if(0 <= tempx and tempx < H and 0 <= tempy and tempy < W and SS[tempx][tempy] == "."):
        temptuple = (tempx, tempy, xytuple[2]+1)
        SS[temptuple[0]][temptuple[1]] = "x" #探索した場所は探索できないようにする
        mlength = max(mlength, xytuple[2] +1)
        q.append(temptuple)
      #print(q)
  return mlength

def brick(a,n,m,N):#ABC 148
  #n番目以降にmがあるか調べる
  if not m in a[n:]:
    print(N-(m-1))
  else:
    #b = copy.deepcopy(a)
    brick(a,a[n:].index(m),m+1,N)

#X = str(input()).split()
#a = [int(x) for x in input().split()]

N = "0"+str(input())
ans = 0

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


print(ans)

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
if up and Num[-1] == 9:
    S += 1

print(S)

Submission status: Wrong Answer

Solution code:
l=int(input())
n=list(str(l))
n.reverse()
for i in range(len(n)):
    if int(n[i])>=5:
        n[i]='0'
        if i==len(n)-1:
            n.append('1')
        else:
            n[i+1]=str(int(n[i+1])+1)
n.reverse()
m=(int(''.join(n)))
a=0
for i in range(len(n)):
    a+=int(n[i])

x=m-l
y=list(str(x))
b=0
for i in range(len(y)):
    b+=int(y[i])
print(a+b)

Submission status: Wrong Answer

Solution code:
import sys
import math
import collections
n = int(input())
#n, k = map(int, input().split())
#s = input()
#a = list(map(int, input().split()))

s = str(n)
l = len(s)
left = 10 ** l - n


if n <= left:
  e = n
  for i in range(l):
    p = l-i-1
    if int(s[p]) > 4:
      e += (10-int(s[p]))
      s = str(n)

  pay = str(e)
  rest = str(e - n)
      
else:
  f = str(left)
  for i in range(len(f)):
    p = len(f)-i-1
    if int(f[p]) > 4:
      left += (10-int(f[p]))
      f = str(left)

    pay = str(left + n)
    rest = str(left)

ans = 0
for i in range(len(pay)):
    ans += int(pay[i])
for i in range(len(rest)):
    ans += int(rest[i])

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())

s = str(n)

_len = len(s)
_max = sum([int(x) for x in s])
ansa = n
for i in range(_len):
    if int(s[_len - i -1]) >= 6:
        # print((10 - int(s[_len - i -1]))*(10**(i)))
        ansa += (10 - int(s[_len - i -1]))*(10**(i))

# print(ansa)

ansb = str(ansa - n)
ansa = str(ansa)
# print(ansb)

_max2 = sum([int(x) for x in ansa]) + sum([int(x) for x in ansb])

print(min(_max, _max2))

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

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin

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

n = ns()
f = 0
pay, back = 0, 0
for ni in reversed(n[:]):
    ni = int(ni) + f
    if ni <= 4:
        pay += ni
        f = 0
    else:
        back += 10 - ni
        f = 1
pay += f
print(pay+back)

Submission status: Wrong Answer

Solution code:
X = input()
N = list(X)
ans1 = len(N)*5
A = 0
for i in N:
    ans1 -= abs(5-int(i))
    if (A >0 and int(i)> 5):
        ans1 += 1
    elif (A < 0 and int(i)< 5):
        ans1 += 1
    
    if 5-int(i)>0:
        A = 1
    elif 5 - int(i)<0:
        A = -1
    else:
        A = 0
        
Y = 10**len(N)-int(X)
N = list(str(Y))
ans2 = len(N)*5
A = 0
for i in N:
    ans2 -= abs(5-int(i))
    if (A >0 and int(i)> 5):
        ans2 += 1
    elif (A < 0 and int(i)< 5):
        ans2 += 1
    
    if 5-int(i)>0:
        A = 1
    elif 5 - int(i)<0:
        A = -1
    else:
        A = 0
print(min(ans1,ans2))

Submission status: Wrong Answer

Solution code:
N  = input()
if int(N[0])>5 or int(N[len(N)-1])>5:
    sum=1
else:
    sum=0
for i in range(len(N)):
    if int(N[i])<=5:
        sum+=int(N[i])
    elif int(N[i])>5:
        sum+=10-int(N[i])

print(sum)

Submission status: Wrong Answer

Solution code:
#for NN in range(9999):
N="00"+input()
#N="00"+str(NN)

cnt=0
kuri=0
N_list=N[::-1]
for i,s in enumerate(N_list[:-1]):
    on = int(s)
    n=int(s)+kuri
    if on==5 and int(N_list[i+1]) > 5:
        cnt+=n
        kuri=1
    elif n<=5:
        cnt+=n
        kuri=0
    else :
        cnt+=10-n
        kuri=1

#print(str(NN)+" "+str(cnt))
print(cnt)

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


l.reverse()
l.append(0)

l_copy = l.copy()

N = len(l)

pay = []
for i in range(N):
    if l[i] == 5 and l[i+1]>5:
        pay.append(0)
        l[i+1] += 1
    elif l[i] <= 5:
        pay.append(l[i])
    elif 10 > l[i] > 5:
        pay.append(0)
        l[i+1] += 1
    elif l[i] == 10:
        pay.append(0)
        l[i+1] += 1

pay_copy = pay.copy()

remain = []
for i in range(N):
    k = pay[i] - l_copy[i]
    if  k >= 0:
        remain.append(k)
    else:
        remain.append(k+10)
        pay[i+1] -= 1

result = 0
for i in pay_copy:
    result += i
for i in remain:
    result += i

print(result)

Submission status: Wrong Answer

Solution code:
import sys, math
def input():
    return sys.stdin.readline()[:-1]
from itertools import permutations, combinations
from collections import defaultdict, Counter
from math import factorial
from bisect import bisect_left # bisect_left(list, value)
#from fractions import gcd

sys.setrecursionlimit(10**7)
N = int(input())
N *= 10

S = str(N)
lenS = len(S)

cnt = 0
over = False

i = 0
while i < lenS:
    n = int(S[i])

    if over == False:
        if n < 5:
            cnt += n
        elif n == 5:
            cnt5 = 0
            j = 0
            while i+j<lenS: 
                n2 = int(S[i+j])
                if n2 >= 5:
                    cnt += (10-n2) - 1
                    cnt5 += 1
                else:
                    if cnt5==1:
                        cnt += n2
                        cnt += 1
                    else:
                        cnt += n2
                        cnt += 2
                    break
                j += 1
            i += j
        else:
            cnt += (10-n) - 1
            over = True
    else:
        if n < 5:
            cnt += n
            cnt += 2
            over = False
        elif n == 5:
            cnt += 4
        else:
            cnt += (10-n) - 1
    i += 1

print(cnt)

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])+2))
print(s[-1][0])

Submission status: Wrong Answer

Solution code:
n = list(str(input()))
lis = []
n.insert(0,"#")
s = ""
  #print(n)
ans = 0
for i in range(len(n)-1):
    if int(n[-(i+1)]) > 5 or (int(n[-(i+1)]) == 5 and (s != "" or (n[-(i+2)] != "#" and int(n[-(i+2)]) >= 5))):
        s = n[-(i+1)]+s
    else:
        s = n[-(i+1)]+s
        if len(s) == 1:
            ans += min(int(s),11-int(s))
        else:
            num1 = str(10 ** len(s)-int(s))
            num2 = str((int(s[0])+1) * 10 ** (len(s)-1)-int(s))
            ans += min(1+sum([int(num1[i]) for i in range(len(num1))]),1+int(s[0])+sum([int(num2[i]) for i in range(len(num2))]))
          #print(s,ans)
        s = ""
if s != "":
    if len(s) == 1:
        ans += min(int(s), 11 - int(s))
    else:
        num1 = str(10 ** len(s) - int(s))
        num2 = str((int(s[0]) + 1) * 10 ** (len(s) - 1) - int(s))
        ans += min(1 + sum([int(num1[i]) for i in range(len(num1))]),1 + int(s[0]) + sum([int(num2[i]) for i in range(len(num2))]))
      #print(s,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
n = input()

ans = 0
add = 0
c = 0
for i in range(len(n)-1,-1,-1):
    d = int(n[i]) + add
    #print('d:',d)
    if d <= 5:
        ans += d
        add = 0
    else:
        ans += 10-d
        if d == 10:
            c += 1
        if i == 0:
            ans += 1
        else:
            add = 1

print(ans-c)