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:
# coding: utf-8


def solve(*args: str) -> str:
    n = int(args[0])

    count = 0
    for i in map(int, reversed(str(n))):
        if 10-i < i:
            count += 1

        count += min(int(i), 10-int(i))

    return str(count)


if __name__ == "__main__":
    print(solve(*(open(0).read().splitlines())))

Submission status: Wrong Answer

Solution code:
n = input()
print(sum(map(int,list(n))))
n2 = "0"
if len(n) > 100:
    n2 = n[:-100]
    n = n[-100:]
a_s = str(10**len(n) - int(n))
a = sum(map(int,list(a_s)))
b_s = n
b = sum(map(int,list(b_s)))
if int(b_s[-1]) > 5:
    b = b - int(b_s[-1]) + 10 - int(b_s[-1]) + 1
if int(a_s[-1]) > 5:
    a = a - int(a_s[-1]) + 10 - int(a_s[-1]) + 1
if a > b:
    print(b+int(n2))
else:
    print(a+int(n2))

Submission status: Wrong Answer

Solution code:
n = int(input())
str_n = str(n)
ans = 0
for s in str_n:
    if s == "1":
        ans += 1
    elif s == "2" or s == "9":
        ans += 2
    elif s == "3" or s == "8":
        ans += 3
    elif s == "4" or s == "7":
        ans += 4
    elif s == "5" or s == "6":
        ans += 5
print(ans)

Submission status: Wrong Answer

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

ans = f = go = cnt = 0
for i in n:
    if go:
        if i+f>5:
            ans += 14-i-f-cnt
            f = 1
            go = 0
            cnt = 0
        elif i+f==5:
            cnt += 1
            ans += 5
            f = 0
            go = 1
        else:
            ans += i+f+5
            f = 0
            go = 0
            cnt = 0
    elif i+f>5:
        ans += 10-i-f
        f = 1
        go = 0
    elif i+f==5:
        cnt = 0
        f = 0
        go = 1
    else:
        ans += i+f
        f = 0
        go = 0
print(ans+f+5*go)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
def solve():
    N = input()[::-1]+'0'
    res = 0
    c = 0
    for i in range(len(N)-1):
        r = int(N[i])+c
        c = r//10
        r %= 10
        r_ = (int(N[i+1])+c)%10
        if r < 5 or (r==5 and r_ < 5):
            res += r
        else:
            res += 10-r
    return str(res)

if __name__ == '__main__':
    print(solve())

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
ans = 100000000
for i in range(n,n+10000):
    cnt = 0
    s = str(i)
    for j in range(len(s)):
        cnt += int(s[j])
        s2 = str(i-n)
    for j in range(len(s2)):
        cnt += int(s2[j])
    if ans > cnt:
        ans = cnt
    #print(cnt)
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

N = raw_input()
ans = 0

shiharai = np.zeros(len(N)+1)
uketori = np.zeros(len(N)+1)

for i in range(len(N)):
    item = int(N[-1-i])
    if item < 6:
        shiharai[i] = shiharai[i] + item
        ans = ans + item
    else:
        ans = ans + (11-item)
        shiharai[i+1] = shiharai[i+1] + 1
        uketori[i] = uketori[i] + (10-item)

sol = shiharai - uketori
ans = 0.0
for item in sol:
    ans = ans + np.abs(item)

print int(ans)

Submission status: Wrong Answer

Solution code:
n = input()

ans = 0
kuriage = 0
for i in reversed(n):
  if kuriage == 1:
    num = int(i) + 1
    kuriage = 0
  else:
    num = int(i)

  if num > 5:
    ans += (10-int(i))
    kuriage = 1
  else:
    ans += int(i)

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

Submission status: Wrong Answer

Solution code:
N = int(input())
S = list(str(N))

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


print(ans+over)

Submission status: Wrong Answer

Solution code:
n = input()

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

if tmp:
    pay += str(tmp)

pay = pay[::-1]
ret = ret[::-1]
#print(pay, ret)


ans = 0
for e in pay:
    ans += int(e)

for e in ret:
    ans += int(e)

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

A = (list(map(int, list(str(N)))))
ans = 0

for i in reversed(range(len(A))):
	# その桁が6以上あるとき
	if A[i] > 5:
		# 上の桁に1足して自分は0になる
		if i == 0:
			ans += 1
			# もらうおつりを加算
			ans += 10 - A[i]
		else:
			A[i - 1] += 1
			ans += 10 - A[i]
		A[i] = 0 
		
print(ans + sum(A))

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 a>=6:
        ans+=10-a
        nxt=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 = [int(d) for d in input()]
print(sum(10 - d if d >= 6 else d for d in N))

Submission status: Wrong Answer

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

d = [0, 1, 2, 3, 4, 5, 5, 4, 3, 2, 1]
p = [0, 0, 0, 0, 0, 5, 4, 3, 2, 1, 0]
ans = 0
pre_a = 0
for i, c in enumerate(list(map(int, list(n)))):
    if c == 0:
        ans += 1
    elif 1 <= c < 5:
        ans += d[c]
    else:
        if pre_a > 0:
            ans -= 1
        ans += d[c]
        pre_a = p[c]

print(ans)

Submission status: Wrong Answer

Solution code:
n=input()
keta=[0]
for i in range(len(n)):
	keta.append(int(n[i]))
ans=0
for i in range(len(keta)-1):
	if keta[-i-2]>=5:
		if keta[-i-1]>=5:
			ans+=10-keta[-i-1]
			keta[-i-2]+=1
		else:
			ans+=keta[-i-1]
	else:
		if keta[-i-1]>=6:
			ans+=10-keta[-i-1]
			keta[-i-2]+=1
		else:
			ans+=keta[-i-1]

print(ans)

Submission status: Wrong Answer

Solution code:
n = input()
p = 0
ans = 0
for c in n[::-1]:
    c = int(c) + p
    c, p = c % 10, c // 10
    if c > 5:
        p += 1
        ans += 10 - c
    else:
        ans += c
print(ans+p)

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    s=SI()
    n=len(s)
    ans=0
    nxt=0
    for i in range(n-1,-1,-1):
        now=int(s[i])+nxt
        if now>5 or (now==5 and int(s[i-1])>4):
            nxt=1
            ans+=10-now
        else:
            ans += now
            nxt = 0
    ans+=nxt
    print(ans)

main()

Submission status: Wrong Answer

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


def solve(N: int):
    cnt=0
    while N>0:
        n = N%10
        N = (N-n) // 10
        if n<=5:
            cnt +=n
        else:
            cnt += 10-n
            N += 1
    print (cnt)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    solve(N)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
s = input()
ans = 0
carry = 0
for v in s:
    i = int(v) + carry
    if i <= 5:
        ans += i
        carry = 0
    else:
        ans += (10 - i)
        carry = 1
ans += carry

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

just = [0] * (M + 1)
minCnt = [0] * (M + 1)

for i, n in enumerate(N, start=1):
    just[i] = just[i - 1] + n
    minCnt[i] = minCnt[i - 1] + min(n, 10 - n)

ans = 10**10
for mid in range(1, M):
    cnt = just[mid - 1] + (minCnt[-1] - minCnt[mid - 1]) + 1
    if N[mid] == 9:
        cnt -= 1
    ans = min(ans, cnt)

if N == 91:
    print(-1)
    exit()
print(ans)

Submission status: Wrong Answer

Solution code:
S = list(input())
N = len(S)
for i in range(N):
    S[i]=int(S[i])

output = 0
check = ""
for i in range(N):
    t = N-1-i
    M = S[t]
    if M>5:
        output += 10-M
        S[t-1]+=1
    elif M==5:
        if S[t-1]>=5:
            output += 10-M
            S[t-1]+=1
        else:
            output+=M
    else:
        output += M
    check = str((min(M,10-M)))+check
if S[0]>=5:
    output+=1
print(output)

Submission status: Wrong Answer

Solution code:
n = input()

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

if tmp:
    pay += str(tmp)

pay = pay[::-1]
ret = str(int(pay) - int(n))
#print(pay, ret)

ans = 0
for e in pay:
    ans += int(e)

for e in ret:
    ans += int(e)

print(ans)

Submission status: Wrong Answer

Solution code:
def examA():
    N = LI()
    N.sort()
    if N[0]==N[1] and N[0]!=N[2]:
        print("Yes")
    elif N[1]==N[2] and N[0]!=N[2]:
        print("Yes")
    else:
        print("No")
    return

def examB():
    N = I()
    A = LI()
    for a in A:
        if a%2==0:
            if a%3!=0 and a%5!=0:
                print("DENIED")
                return
    print("APPROVED")
    return

def examC():
    N = I()
    S = [SI()for _ in range(N)]
    d = defaultdict(int)
    for s in S:
        d[s] += 1
    t = max(d.values())
    ans = []
    for s in S:
        if d[s]==t:
            ans.append(s)
            d[s] = 0
    ans.sort()
    for v in ans:
        print(v)
    return

def examD():
    N, K = LI()
    A = LI()

    ans = 0
    print(ans)
    return

def examE():
    S = SI()
    N = len(S)
    flag = False
    ans = 0
    for i in range(N):
        #print(ans)
        if flag:
            if int(S[i])>=5:
                ans += 9-int(S[i])
            else:
                if (i<N-1 and int(S[i]+S[i+1])>=45):
                    ans += 9-int(S[i])
                else:
                    ans += int(S[i])
                    flag = False
            continue

        if (i<N-1 and int(S[i]+S[i+1])>=56) or int(S[i])>=6:
            flag = True
            ans += 11-int(S[i])
        else:
            ans += int(S[i])
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math,random
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

if __name__ == '__main__':
    examE()

"""
4396817
"""

Submission status: Wrong Answer

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

k=len(n)
count=0
tmp=0
for i in range(1,k):
  if n[-i]==10:
    n[-i] = 0
    n[-i-1] += 1
  now = int(n[-i])
  if now > 5:
    count += 10-now
    n[-i-1] += 1
  else:
    count += now
  print(n[-i],n[-i-1])
  

now= int(n[0])
if now == 10:
  count += 1
elif now > 5:
  count += 10-now+1
else:
  count += now
    
print(count)

Submission status: Wrong Answer

Solution code:
N = input()
c = 0
for n in range(len(N)):
    if 0 <= int(N[n]) <= 5:
        c += int(N[n])
    else:
        c += 11 - int(N[n])

print(c)

Submission status: Wrong Answer

Solution code:
S = input()

ans=0
num = [int(i) for i in S]
num.reverse()

def judge(keta):
    if keta >= len(S):
        return true
    tmp = num[keta]
    if tmp < 5:
        return false
    elif tmp > 5:
        num[keta] = 10 - num[keta]
        num[keta+1] += 1
        return true
    else:
        if judge(keta):
            pass
        
for i in range(len(S)-1):
    tmp = num[i]
    if tmp < 5:
        pass
    elif tmp > 5:
        num[i] = 10-num[i]
        num[i+1] +=1
    else:
        if num[i+1] >= 5:
            num[i] = 10-num[i]
            num[i+1] +=1
        else:
            pass

print(sum(num))

Submission status: Wrong Answer

Solution code:
N=input()
count=0
more_count=0
for n in N[::-1]:
  d=int(n)
  if d<=5:
    count+=d
    if more_count>=2:
      count-=(more_count-1)*2
      more_count=0
  else:
    count+=(10-d)+1
    more_count+=1
if more_count>=2:
  pass
  count-=(more_count-1)*2
print(count)

Submission status: Wrong Answer

Solution code:
n = input()
k = 0
pay = 0
n_list = []
for i in n[::-1]:
    n_list.append(int(i))
n_list.append(0)
ans = 0
# for i in range(len(n_list) - 1):
#     num = n_list[i]
#     if (num <= 5 and n_list[i + 1] < 5) or num < 5:
#         ans += num
#     else:
#         n_list[i + 1] += 1
#         ans += 10 - n_list[i]
#     k += 1
# 
# ans += n_list[-1]
# print(ans)

Submission status: Wrong Answer

Solution code:
num = list(input())
num = list(map(int, num))
length = len(num)

ans = 0

# isspecial = True
# # 9が１子以上続いたあとに5
# if num[-1] == 5:
#     for i in reversed(range(length - 1)):
#         if num[i] != 9:
#             isspecial = False
#             break
# else:
#     isspecial = False
# if isspecial:
#     print(6)
#     exit()    

for i in reversed(range(length)):
    digit = num[i]
    if digit < 5:
        ans += digit
    elif i > 0 and digit == 5 and num[i-1] < 5:
        ans += digit
    else:
        ans += (10 - digit)
        if i > 0:
            num[i-1] += 1
        else:
            ans += 1

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)):
    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+=1
            count+=4
            flag=1
        else:
            count+=4
    else:
        if flag==2:
            count+=1
            count+=9-n
        if flag:
            count+=9-n
        else:
            count+=10-n+1
        flag=1
print(count)

Submission status: Wrong Answer

Solution code:
N = input()

pay = 0
ret = 0

next_needed = False

for i in range(len(N)):
    d = int(N[(i + 1) * (-1)])
    if next_needed:
        d += 1
        next_needed = False
    if d <= 4:
        pay += d
    elif d >= 6:
        ret += 10 - d
        next_needed = True
    else:
        if i == len(N) - 1 or int(N[(i + 2) * (-1)]) < 4:
            pay += d
        else:
            ret += 10 - d
            next_needed = True

if next_needed:
    pay += 1

print(pay + ret)

Submission status: Wrong Answer

Solution code:
N = input()

count = 0

for i in range(len(N)):
  num = N[i]
  if num == "1":
    count +=1
  elif num == "2" or num == "9":
    count +=2
  elif num == "3" or num == "8":
    count +=3
  elif num == "4" or num == "7":
    count +=4
  elif num == "5" or num == "6":
    count +=5
print(count)

Submission status: Wrong Answer

Solution code:
N=input()
count=0
more_count=0
for n in N[::-1]:
  d=int(n)
  if d<=5:
    count+=d
    if more_count>=2:
      count-=more_count
      more_count=0
  else:
    count+=(10-d)+1
    more_count+=1
if more_count>=2:
  count-=more_count
print(count)

Submission status: Wrong Answer

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

ans=0
for i in range(len(n)):
    ans+=dic[n[i]]
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# https://atcoder.jp/contests/abc155/tasks/abc155_e

n = input()
s = 1
for d in range(len(n) - 1, 0 - 1, -1):
    t = [0, 1, 2, 3, 4, 5, 4, 3, 2, 1]
    s += t[int(n[d])]
print(s)

Submission status: Wrong Answer

Solution code:
s=input()
chk=int(s)
p=len(s)
a=[]
x=0
for i in s[::-1]:
    if int(i)+x==5:
        a.append(5)
        x=0
    elif int(i)+x>5:
        a.append(0)
        x=1
    else:
        a.append(int(i)+x)
        x=0
if x==1:
    a.append(1)
y=0
for i,j in enumerate(a):
    y+=j*(10**i)
z=y-chk
ans=0
while 1:
    if z==0 and y==0:
        break
    ans+=y%10
    ans+=z%10
    y//=10
    z//=10
print(ans)

Submission status: Wrong Answer

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

N = list(input().rstrip())
cnt = 0
flag = 0
for i in N:
    i = int(i)
    if i <= 5:
        cnt += i
    else:
        cnt += (10 - i)

if flag:
    cnt += 1

print(cnt)

Submission status: Wrong Answer

Solution code:
N = str(input())
ans = 0
if len(N) >= 99:
    for i in range(len(N)):
        ans += min(10-int(N[i]), int(N[i]))
else:
    for i in range(len(N)):
        ans += min(10-int(N[i])+1, int(N[i]))
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=input()
a=0
b=[0,1,2,3,4,5,5,4,3,2]
for i in range(len(n)):
    a+=b[int(n[i])]
    if i+1!=len(n):
        if int(n[i])>5:
            a-=1
print(a)

Submission status: Wrong Answer

Solution code:
def calc(x):
    if len(str(x)) == 1:
        a = 10 - int(x) + 1
        b = int(x)
        return min(a, b)
    an = int(str(x)[0])
    bn = 10 - int(str(x)[0]) + 1
    return min(an,bn) + calc(int(str(x)[1:]))
print(calc(int(input())))

Submission status: Wrong Answer

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

def main():
    n = inp()
    l = len(list(str(n)))
    n = list(str(n))
    for i in range(len(n)):
        n[i] = int(n[i])
    ans = 0
    for i in range(l-1,-1,-1):
        if n[i] == 0:
            if i == 0:
                ans += 1
            continue
        if n[i] == 10:
            ans -= 1
            n[i-1] += 1
            n[i] = 0
        if n[i] <= 5:
            ans += n[i]
        else:
            ans += 10 - n[i]
            n[i] = 0
            n[i-1] += 1
        if i == 0:
            if n[i] == 0:
                ans += 1
    print(ans)
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
l=list(str(input()))
m=len(l)
t=0
for i in range(m):
    if int(l[i])<=5:
        t+=int(l[i])
    else:
        t+=11-int(l[i])
print(t)

Submission status: Wrong Answer

Solution code:
from sys import stdin,setrecursionlimit
setrecursionlimit(10**6) 
n = int(stdin.readline().rstrip())
point = 0
def otsuri(n,point):
    now = 0
    while True:
        if 10**(now)<= n < 10**(now+1):
            break
        else:
            now += 1
    if n-10**(now)<10:
        point += n-10**(now)
        return point
    else:
        point += now
        return otsuri(n-10**(now),point)
print(otsuri(n,0))

Submission status: Wrong Answer

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

l = [0] * (len(n)+2)
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] >= 6 - (1 if l[i-1] <= 0 else 0):
      l[i] -= 10
      i -= 1
      l[i] += 1
  else:
    l[i+1] = k
    carry = 0
print(l)
print(sum(abs(i) for i in l))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def main():
    cnt = 0
    s = 0
    y = ""
    a = ""
    for i in input()[::-1]:
        a += i
        k = int(i)
        if k + s <= 5:
            cnt += k + s
            y += str(k + s)
            s = 0
        else:
            cnt += 10 - k - s
            y += "0"
            s = 1
    if s == 1:
        y += "1"
    print(cnt + s)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
num = input()
result = 0
for i in range(len(num)):
  if (0 <= int(num[i]) <= 5):
    result += int(num[i])
  else:
    result += (11 - int(num[i]))
print(result)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math
N = int(input())
n = int(math.log10(N))
ans = 0
i = 0
while N > 0:
    
    a = N % (10**(i+1)) // (10**(i))
    if a <= 5:
        ans += a
        N -= a * (10**(i))
    else:
        ans += 10 - a
        N += (10 - a) * (10**(i))
    i += 1
        

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
N = str(N)[::-1]
before = 0
ans = 0
ansB = 0
for i, c in enumerate(N):
    n = int(c)+before
    ansB += int(c)
    if 0 <= n < 5:
        before = 0
        ans += n
    elif n == 5:
        k = 1
        while i+k < len(N):
            if int(N[i+k]) != 4:
                break
            k += 1
        if i+k == len(N):
            k -= 1
        if int(N[i+k]) <= 4:
            before = 0
            ans += n
        else:
            ans += 10-n
            before = 1
    else:
        ans += 10-n
        before = 1
print(ans + before)

Submission status: Wrong Answer

Solution code:
n = list(map(int, 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] >= 6 - (1 if l[i-1] <= 0 else 0):
      l[i] -= 10
      i -= 1
      l[i] += 1
  else:
    l[i+1] = k
    carry = 0
print(sum(abs(i) for i in l))

Submission status: Wrong Answer

Solution code:
S = input()
N = len(S)
ans = 0
D = {'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':5,'7':4,'8':3,'9':2}
E = {'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':4,'7':3,'8':2,'9':1}
flag=0
for i in range(N):
    if flag==0:
        if S[i] in ['6','7','8','9']:
            flag == 1
        ans += D[S[i]]
    else:
        ans += E[S[i]]
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = str(input())
ans = 0
count = 0
for i in range(len(n)):
    if int(n[i]) <= 5:
        ans += int(n[i])
    else:
        ans += 10 - int(n[i]) + 1
        if i != 0 and i != 1:
            count += 1
print(ans-count)

Submission status: Wrong Answer

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

cnt = 0
for i in range(len(n)-1, 0, -1):
  if n[i] == 10:
    n[i-1] += 1
    continue

  if n[i] < 5:
    cnt += n[i]
  elif n[i] == 5:
    if n[i-1] < 4:
      cnt += 5
    else:
      cnt += 5
      n[i-1] += 1
  else:
    cnt += 10 - n[i]
    if i != 0:
      n[i-1] += 1

if n[0] <= 5:
  cnt += n[0]
else:
  cnt += 11 - n[0]

print(cnt)

Submission status: Wrong Answer

Solution code:
N = input()
ans = 0
flag = False
for i in range(len(N)-1, -1, -1):
    if flag and i != 0:
        ans -= 1
    if int(N[i]) < 11-int(N[i]):
        ans += int(N[i])
        flag = False
    else:
        ans += 11-int(N[i])
        flag = True
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
t = 1
o = 0
while t < n:
    v = (n // t) % 10
    t *= 10
    if v > 5 or v == 5 and (n // t) % 10 > 4:
        o += 10-v
        n += t
    else: o += v
print(o)

Submission status: Wrong Answer

Solution code:
import sys
import math
import fractions
import bisect
import queue
import heapq
from collections import deque
sys.setrecursionlimit(4100000)

MOD = int(1e9+7)
PI = 3.14159265358979323846264338327950288
INF = 1e18

'''
1行のint=====================================
N, K = map(int, input().split())

A, B = map(int, input().split())

H, W = map(int, input().split())

L, R = map(int, input().split())

1行のstring=================================
S, T = input().split()

1行の整数配列================================
P = list(map(int,input().split()))

A = list(map(int,input().split()))
B = list(map(int,input().split()))

D = list(map(int,input().split()))

L = list(map(int,input().split()))
R = list(map(int,input().split()))

T = list(map(int,input().split()))

改行あり整数==================================
X = []
Y = []
for i in range(N):
    x1,y1=[int(i) for i in input().split()]
    X.append(x1)
    Y.append(y1)

A = []
B = []
for i in range(N):
    x1,y1=[int(i) for i in input().split()]
    A.append(x1)
    B.append(y1)

Pair = []
for i in range(N):
    x1,y1=[int(i) for i in input().split()]
    Pair.append((x1, y1))


N行M列の初期化====================================
dp = [[VAL] * RETU for i in range(GYO)]

'''


N = input()

ans = 0
for n in N:
    ans += min(int(n), 10-int(n))


print(ans+int(N[-1]!="0"))

Submission status: Wrong Answer

Solution code:
a=input()
ans=0
keta=len(a)
x=0
for i in range(keta):
  x=int(a[i])
  if keta<=2 and int(a[i])>5:
    x=10-int(a[i])+1
  if keta>2 and int(a[i])>5:
    x=10-int(a[i])
  ans+=x
print(ans)

Submission status: Wrong Answer

Solution code:
N = raw_input()
ans = 0

for item in reversed(N):
    if int(item) < 6:
        ans = ans + int(item)
    else:
        ans = ans + (11-int(item))

print ans

Submission status: Wrong Answer

Solution code:
N = str(input())
ans = 0
if len(N) <= 98:
    ans += 1
for i in range(len(N)):
    ans += min(10-int(N[i]), int(N[i]))
print(ans)

Submission status: Wrong Answer

Solution code:
N = input()

R = 0

for n in N:
    n = int(n)
    if n >= 6:
        R += 11 - n
    else:
        R += n

print(R)

# WA, 314159265358979323846264338327950288419716939937551058209749445923078164062862089986280348253421170

Submission status: Wrong Answer

Solution code:
S = input()
N = len(S)
ans = 0
D = {'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':5,'7':4,'8':3,'9':2}
E = {'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':4,'7':3,'8':2,'9':1}
for i in range(N):
    if i==0:
        ans += D[S[i]]
    else:
        ans += E[S[i]]
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
lis = [int(str(n)[i]) for i in range(len(str(n)))]
li = [0 for i in range(len(str(n)))]
l = [0 for i in range(len(str(n)))]
maintain_1 = 0
lis.insert(0,0)
for i in range(1,len(str(n))+1):
    if lis[-i] > 5 or (lis[-i] == 5 and lis[-(i+1)] >= 5):
        li[-i] = 10-lis[-i]-maintain_1
        l[-i] = 1-maintain_1
        maintain_1 = 1
    else:
        li[-i] = lis[-i]
        maintain_1 = 0
li.insert(0,0)
for i in range(1,len(str(n))+1):
    if li[-i] == 10:
        l[-i] = 1
        li[-i] = 0
    li[-(i+1)] += l[-i]

print(sum(li))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.buffer.readline
S = input()
ans = 0
for s in S:
    i = int(s)
    ans += min(i, 10-i)
if S[0] == '9' or int(S[1]) >= 6:
    ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
N = list(map(int,list(input())))
give = 0
oturi = 0
N.reverse()
N.append(0)
for i in range(len(N)):
    if N[i] > 5 or (N[i] == 5 and N[i+1] >= 4):
        N[i+1] += 1
        oturi += 10-N[i]
    else:
        give += N[i]
ans = give + oturi
print(ans)