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 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline

##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)

##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順

# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順

##### 無限 #####
# inf = float('inf')

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

inf = float('inf')

def main():
	N = inputInt()
	s = str(N)
	s = s[::-1]
	N = int(s)
	ans = 0
	cntt = 0
	while True:
		if N == 0:
			break
		tmp = N%10
		N = int(N//10)

		if tmp <= 5:
			ans += tmp
		else:
			ans = ans + (10 - tmp)
		cntt += 1
		
	if len(s) != cntt:
		ans -= 1
	print(ans+1)



if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
n = str(input())
ans = 0
count = 0
next_one = False

for i in range(len(n)-1, -1, -1):
    if next_one == True:
        num = (int(n[i]) + 1) % 10
    else:
        num = int(n[i])
    if num <= 5:
        ans += num
        next_one = False
    else:
        ans += 10 - num
        next_one = True
        if i >= 1 and n[i-1] == '9':
            ans -= 1
if n[0] == '9':
    ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
sn=str(n)
num=len(sn)
pay=0#枚数
pay_temp=0#払うお金
for i in range(num):
    s=int(sn[num-1-i])
    if s>5:
        pay_temp+=10**(i+1)
        pay+=1
    else:
        pay_temp+=s*10**(i)
        pay+=s

#print(pay,pay_temp)
#おつり
back=pay_temp-n
back=str(back)
for i in back:
    pay+=int(i)
print(pay)

Submission status: Wrong Answer

Solution code:
n = input()

total = 0
keta = len(n)
kuriage = False
for i in range(keta):

    tmp = int(n[keta-i-1])
    if kuriage:
        tmp += 1
    
    if tmp > 5:
        total += 10 - tmp
        kuriage = True
    else:
        total += tmp
        kuriage = False

if kuriage:
    print(total + 1)
else:
    print(total)

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 + 1

print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())

def log(x):
    t=1
    s=1
    while t*10 <= x:
        s = s+1
    return s

def point(a,b):
    for i in range(b-1):
        a = a-a%(10**(i+1))
    return (a%(10**b))//(10**(b-1))

List=[]
for i in range(log(N)):
    List.append(point(N,i+1))

ans = 0
for i in range(log(N)):
    if List[i]<=5:
        ans = ans+point(N,i)
    else:
        ans = ans + 11- point(N,i)

print(ans)

Submission status: Wrong Answer

Solution code:
#E
N = list(input())
ans = 0

for num in reversed(N):
    num = int(num)
    if num >5:
        ans += (1+(10-num))
    else:
        ans += num
        
print(ans)

Submission status: Wrong Answer

Solution code:
a = input()
N = list(a)
it = int(a)
ln = len(N)

cnt = 0
li = ""
ans = 0
for i in range(len(N)):
    mm = int(N[i])
    if mm < 5:
        ans += mm
        
    elif mm >= 5 and cnt==0:
        li += N[i]
        cnt += 1
    else:
        li += N[i]
        cnt += 1

# print(list(li))
if len(li) > 0:
    mm1 = 10**cnt
    mm = mm1 - int(li)
    mm = list(str(mm))
    # print(mm)
    for i in range(len(mm)):
        ans += int(mm[i])
    print(ans +1)
else:
    print(ans)


# mm = int(li)
# q = list(li)
# i = len(q)-1
# w = i

# r = 0

# while(mm<mm1):
#     ans += (10-int(q[i]))
#     r = 10*r + (10-int(q[i]))

#     mm += (10-int(q[i]))*(10**(w-i))
#     q = list(str(mm))
#     i-=1
#     print(mm)

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

Submission status: Wrong Answer

Solution code:
s = input()
num = 0
carry = 0

for digit in reversed(s):
  d = int(digit) + carry
  if d <= 5:
    num += d
    carry = 0
  else:
    num += 10-d
    carry = 1

print(num+carry)

Submission status: Wrong Answer

Solution code:
Ns = [int(s) for s in input()]
total = 0
kuri = 0
for n in Ns[::-1]:
  print(n)
  if kuri + n == 10:
    kuri = 1
  else:
    n += kuri
    if n <= 5:
      total += n
      kuri = 0
    else:
      total += (10 - n)
      kuri = 1
if kuri > 0:
  total += kuri
    
print(total)

Submission status: Wrong Answer

Solution code:
n = list(input())
n.reverse()
ans = ""
cand = 0
flag = False
#一桁のときの場合分け
for i in n:
    if flag:
        flag = False
        if i == '9':
            ans = '0' + ans
            flag = True
        else:
            ans = str(int(i) + 1) + ans
        continue
    if int(i) >= 5:
        flag = True
        ans = '0' + ans
    else:
        ans = i + ans
if flag:
    ans = '1' + ans
#自分が使った紙幣
print_ans = 0
for i in ans:
    print_ans += int(i)
#お釣りの計算
#普通に引き算をする。頑張る
#n - ans
n.reverse()
k = str(int(ans) - int("".join(n)))
for i in k:
    print_ans += int(i)
#print(k)
print(print_ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=int(input())
sn=list(str(n))

sn.reverse()

count=0
res=0
for i in sn:
    i=int(i)
    i+=count
    count=0
    a=i
    d=10-a
#     if a==10:
#         a=1
    if a>d+1:
        res+=d
        count=1
#         print(str(i)+":"+str(d))
    else:
        res+=a
res+=count
        
#         print(str(i)+":"+str(a))
print(res)

Submission status: Wrong Answer

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False
"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""

n = input().split()[0]

le = len(n)

po = 0

co = 0

for i in range(le-1,-1,-1):
    nu = int(n[i])
    nu += co
    if nu == 5:
        if i != 0 and int(n[i-1]) + 1 >= 5:
            co = 1
        else:
            co = 0
        po += nu
    elif nu > 5:
        co = 1
        po += 10 - nu
    else:
        co = 0
        po += nu
print(po + co)

Submission status: Wrong Answer

Solution code:
n=int(input())
dp=[[] for _ in range(1001)]

Submission status: Wrong Answer

Solution code:
N = input()
ans = 0
flag = False
for i in N[::-1]:
    tmp = int(i)
    if flag:
        if tmp < 5:
            ans += tmp + 1
            flag = False
        else:
            ans += 9 - tmp
    else:
        if tmp < 6:
            ans += tmp
        else:
            ans += 10 - tmp
            flag = True
if flag:
    ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
n = list(input())

ans = 1
for i in range(len(n)):
    n[i] = int(n[i])

ans = 0
flag = 0
for i in range(len(n))[::-1]:
    if n[i] == 10:
        ans -= 1
        n[i - 1] += 1
    elif n[i] <= 5:
        ans += n[i]
    else:
        ans += 10 - n[i]

        n[i - 1] += 1
if n[0] >= 9:
    ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = 0
while n > 0:
    i = int(str(n)[0])
    if i > 5:
        n = 1 * 10 ** (len(str(n))) - n
        s += 1
    else:
        s += i
        n -= i * 10 ** (len(str(n)) - 1)

print(s)

Submission status: Wrong Answer

Solution code:
N = input()
l = len(N)
N = N[::-1] # Nを反転
plus = 0
ls = ""
for i in range(l):
    Ni = int(N[i])
    if (Ni + plus >= 5):
        plus = 1
        ls += "0"
    else:
        ls += str(Ni + plus)
        plus = 0

ans = 0

ls += str(plus)

ls = ls[::-1]
N = N[::-1]

ansl = str(2*int(ls)-int(N))
ans = 0
for i in range(len(ansl)):
    ans += int(ansl[i])

print(ans)

Submission status: Wrong Answer

Solution code:
n = input()
digits = []
num_notes = 0
carry = 0
for _d in n[::-1]+'0':
    d = int(_d) + carry
    if d <= 5:
        num_notes += d
        carry = 0
    else:
        num_notes += (10 - d)
        carry = 1
print(num_notes)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

def main():
    N = readline().strip().decode('utf-8')
    l = []
    flg = False
    for n in N[::-1]:
        n=int(n)
        if n<=5:
            l.append(n + flg)
            flg = False
        else:
            l.append(0)
            flg = True
    if flg is True:
        l.append(1)    
    pay = int(''.join(map(str,l[::-1])))
    recieve = pay - int(N)
    res = sum(list(map(int, str(pay))))
    res += sum(list(map(int, str(recieve))))
    return res

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

Submission status: Wrong Answer

Solution code:
N = input()
res = 0
s = list(N)
s.sort()
d = dict()
"""
for i in s:
    if s in d.keys():
        d[i] += 1
    else:
        d[i] = 1
else:
    print(d)
"""
res = 0
for i in N:
    i = int(i)
    if i==0:
        #print("0")
        res -= 5
    if i < 6:
        res += i
    else:
        res += 11-i
else:
    print(res)#,ddd)

Submission status: Wrong Answer

Solution code:
import sys


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


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


def solve(N):

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

    total += rem
    print(total)


if __name__ == '__main__':
    main()

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 N[-1] == "0":
    print(ans)
else:
    print(ans + 1)

Submission status: Wrong Answer

Solution code:
s=list(input())
for i in range(len(s)):
    s[i]=int(s[i])
ans=0
pay=0
for i in range(len(s)):
    if pay==0 and s[i]>=6:
        ans+=10-s[i]
        pay=10
    elif pay==0:
        ans+=s[i]
    elif s[i]>=5:
        ans+=9-s[i]
    else:
        ans+=s[i]+1
        pay=0
print(ans+pay//10)

Submission status: Wrong Answer

Solution code:
class Solution:

    def solve(self, N: str) -> int:

        L = len(N)
        D = []
        for l in range(L-1, -1, -1):
            D.append(int(N[l]))

        D.append(0)

        ans = 0
        for l in range(L+1):

            # carry over
            if D[l] == 10:
                D[l+1] += 1
                D[l] = 0

            if D[l] <= 5:
                ans += D[l]
            else:
                ans += 10-D[l]
                D[l+1] += 1

#            print(D[l], ans)

        return ans


if __name__ == '__main__':

    # standard input
    N = input()

    # solve
    solution = Solution()
    print(solution.solve(N))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = str(n)
length = len(s)
ans = 0
for i in range(length):
    x = int(s[i])
    if x < 5:
        ans = ans + x
    else:
        ans = ans + (10 - x)

if int(s[-1]) == 0:
    ans = ans - 1

print(ans + 1)

Submission status: Wrong Answer

Solution code:
N = input()
n = int(N)
ans = 0
len_N = len(N)
for i in range(len_N):
    if int(N[i]) >= 5:
        if N[i] == 5 and N[i + 1] == 0:
            pass
        else:
            N = str(10 ** (len_N - i) - int(N))
            N = (len_N - len(N)) * "0" + N
            ans += 1

    ans += int(N[i])
    if i == len_N - 1:
        continue
    N = N[:i] + "0" + N[i + 1:]
print(ans)

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)
kouho=0
bigger=0
for i in range(1,keta+1):
    d=int(s[i-1])
    if d>=6 and not bigger:
        kouho+=10**(keta-i+1)
        bigger=1
    elif d>=6 and bigger:
        continue
    else:
        kouho+=d*(10**(keta-i))
        bigger=0
oturi=kouho-n
ans=0
for a in str(kouho):
    ans+=int(a)
for b in str(oturi):
    ans+=int(b)
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
cnt = 0
while N>0:
  n=N%10
  N=N//10
  if n>=6:
    cnt+=10-n
    N+=1
  else:
    cnt+=n
print(cnt)

Submission status: Wrong Answer

Solution code:
A=input()
ans=0
for i in A:
    if int(i)>5:
        ans=ans+11-int(i)
    else:
        ans+=int(i)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys


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


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


def solve(N):

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

    total += rem
    print(total)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = input()
MAX = len(N)
digit = MAX
ans  = 0
okane = 0
while(digit>0):
    num = int(N[MAX-digit])
    if num<5:
        ans+= num
        okane+= num*10**(digit-1)
    else:
        ans+= 1
        okane += 10**(digit)
    digit-=1
#     print(ans)
    
oturi = okane-int(N)
oturi = str(oturi)
# print(okane)
L = len(oturi)
for item in oturi:
    ans+=int(item)

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

rev_pay = []

pls = 0
for i in li_n[::-1]:
  if i + pls <= 5:
    rev_pay.append(i + pls)
    pls = 0
  else:
    rev_pay.append(0)
    pls = 1
if pls == 1:
  rev_pay.append(1)
pay = rev_pay[::-1]
# print(int("".join(map(str, pay))))

turi = int("".join(map(str, pay))) - n

# print(turi)
turi_li = list(str(turi))
# print(turi_li)
ans = sum(pay) + sum(map(int, turi_li))
print(ans)

Submission status: Wrong Answer

Solution code:
n = input()
pay = 0
sup = 0

n=n[::-1]
bef_up = False
for i in range(len(n)):
    num = int(n[i])
    if num > 5:
        pay += 1
        sup += 10-num
        if bef_up:
            sup -= 1
        bef_up = True
    else:
        pay += num
        if bef_up:
            sup -= 1
        bef_up = False

# print(pay,sup)
print(pay+sup)

Submission status: Wrong Answer

Solution code:
N=raw_input()
l=len(N)
dp=[ 0 for _ in range(l+1) ]

for i in range(1,l+1):
    x=int(N[i-1])
    dp[i]=min(dp[i-1]+x,dp[i-1]+1+(10-x))

print dp[-1]

Submission status: Wrong Answer

Solution code:
N = input()

ans = 0
for n in N:
    n = int(n)
    if n >= 5:
        ans += (10 - n)
    else:
        ans += n


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

Submission status: Wrong Answer

Solution code:
class Solution:

    def solve(self, N: str) -> int:

        L = len(N)
        D = []
        for l in range(L-1, -1, -1):
            D.append(int(N[l]))

        D.append(0)

        ans = 0
        for l in range(L+1):

            # carry over
            if D[l] == 10:
                D[l+1] += 1
                D[l] = 0

            if D[l] <= 5:
                ans += D[l]
            else:
                ans += 10-D[l]
                D[l+1] += 1

            print(D[l], ans)

        return ans


if __name__ == '__main__':

    # standard input
    N = input()

    # solve
    solution = Solution()
    print(solution.solve(N))

Submission status: Wrong Answer

Solution code:
n = [0]
n1 = list(input())
n2 = [int(s) for s in n1]
nn = len(n1)
n = n + n2
#print(n)

ans = 0
for i in range(nn, -1, -1):
    #print(n[i])
    if n[i] >= 10:
        n[i - 1] += 1
    elif n[i] <= 5:
        ans += n[i]
    else:
        ans += 10 - n[i]
        n[i - 1] += 1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())

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

l.reverse()

dasu=0
turi=0
for num in l:

    num_kuri=num

    if num_kuri <=5:
        dasu+=num
    else:
        dasu+=1
        turi+=10-num

#print(dasu,turi)
print(dasu+turi)

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    N = tuple(map(int, list(input())))[::-1]
    n_len = len(N)
    
    dp = [[0] * 2 for _ in range(n_len+1)]
    dp[0][True] = 1
    for i, v in enumerate(N):
        dp[i+1][True] += min(9 - v + dp[i][True], 10 - v + dp[i][False])
        dp[i+1][False] += min(v + 1 + dp[i][True], v + dp[i][False])
    print(min(dp[n_len]))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = list(input())
nl = len(n)
ans = 0
for i in range(nl-1):
    res = int(n[nl-i-1])
    if int(n[nl-i-1]) == 10:
        n[nl-i-2] = int(n[nl-i-2]) + 1
        continue
    if res == 6:
        res = 4
        n[nl-i-2] = int(n[nl-i-2]) + 1
    elif res == 7:
        res = 3
        n[nl-i-2] = int(n[nl-i-2]) + 1
    elif res == 8:
        res = 2
        n[nl-i-2] = int(n[nl-i-2]) + 1
    elif res == 9:
        res = 1
        n[nl-i-2] = int(n[nl-i-2]) + 1
    ans += res
res = int(n[0])
if res == 6:
    res = 5
elif res == 7:
    res = 4
elif res == 8:
    res = 3
elif res == 9:
    res = 2
elif res == 10:
    res = 1
ans += int(res)
print(ans)

Submission status: Wrong Answer

Solution code:
nn = input()

carry = []
ans = 0
cnt = 0
for i in range(len(nn))[::-1]:
    n = int(nn[i])
    tmp = n
    if tmp <= 10 - tmp:
        ans += tmp
#        ans -= carry
        carry.append(0)
    else:
        ans += 10 - tmp
        carry.append(1)
    # print(ans, tmp, carry)
print(ans + sum(carry[-2:]))

Submission status: Wrong Answer

Solution code:
def resolve():
    n = "0" + input()
    ans = 0
    l = [int(x) for x in n[::-1]]
    m = len(l)
    for i in range(m):
        if l[i] <= 5:
            ans += l[i]
        else:
            ans += (10 - l[i])
            for j in range(i + 1, m):
                if l[j] == 9:
                    l[j] = 0
                else:
                    l[j] += 1
                    break
    print(ans)
if __name__ == '__main__':
    resolve()

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

if go: print(ans+6-cnt)
else: print(ans+f)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = str(N)
ans = 0
chk = 0
for i in range(len(S)):
	if (int(S[len(S)-1-i])+chk) > 5 :
		chk = 1
		ans += 10-int(S[len(S)-1-i])
	else:
		if i != len(S)-1:
			chk = 0
		ans += int(S[len(S)-1-i])
if chk == 1:
	ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
N = input()
ans = 0
flag = False
for i in N[::-1]:
    tmp = int(i)
    if flag:
        if tmp < 5:
            ans += tmp + 1
            flag = False
        else:
            ans += 10 - tmp
    else:
        if tmp < 6:
            ans += tmp
        else:
            ans += 9 - tmp
            flag = True
            
print(ans)

Submission status: Wrong Answer

Solution code:
n = str(input())
lis = []
s = ""
for i in range(len(n)):
    if int(n[-(i+1)]) >= 5:
        s = n[-(i+1)]+s
    else:
        s = n[-(i+1)]+s
        lis.append(s)
        s = ""
if s != "":
    lis.append(s)
  #print(lis)
ans = 0
for ss in lis:
    if int(ss[-1]) < 5:
        ans += int(ss)
    else:
        if int(ss[0]) >= 5:
            num = str(10 ** len(ss)-int(ss))
            ans += 1
        else:
            num = str((int(ss[0])+1) * 10 ** (len(ss)-1)-int(ss))
            ans += int(ss[0]) + 1
        ans += sum([int(num[i]) for i in range(len(num))])
print(ans)

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

Submission status: Wrong Answer

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import collections

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))



def solve(N):
    n_s = list(str(N))


    n_s = [0] + n_s
    n_s.reverse()

    def cnt_paper(s):
        tmp = 0
        # print(s)
        for s_i in s:
            tmp += int(s_i)
        return tmp

    anss = [0 for i in range(len(n_s) + 1)]
    for i in range(len(n_s)):
        if int(n_s[i]) >= 5 and int(n_s[i + 1]) >= 5:
            pass
        elif int(n_s[i]) >= 5:
            anss[i+1] += 1
        else:
            anss[i] += int(n_s[i])

    anss.reverse()
    ans = cnt_paper(anss)
    tmp = ""
    for i in range(len(anss)):
        tmp += str(anss[i])
    ans += cnt_paper(list(str(int(tmp) - N)))
    return ans

N = int(input())
print(solve(N))

Submission status: Wrong Answer

Solution code:
S = list(map(int,list(input())))
N = len(S)
output = 0
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
if S[0]>5:
    output+=1
print(output)

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-num)
    kuriage = 1
  else:
    ans += num

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

Submission status: Wrong Answer

Solution code:
# n, k = map(int, input().split())
# a = list(map(int, input().split()))
# a.sort()
# print(a)

n = input()
cnt = 0
k = 0
num = len(n)
for i in range(num):
    m = int(n[num - i - 1]) + k
    # print(m)
    if m == 10:
        k = 1
        continue
    if m < 5:
        cnt += m
        # print(m)
        k = 0
    elif m == 5:
        if i != 0 and int(n[num - i - 2]) >= 5:
            cnt += 10 - m
            k = 1
        else:
            cnt += m
            k = 0
    else:
        # print(m, 10-m)
        cnt += 10 - m
        k = 1
if k == 1:
    cnt += 1
print(cnt)

Submission status: Wrong Answer

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

ans=0
for i in range(l_len-1,-1,-1):
 num=l[i]
 if l[i]<=10-l[i]:
  ans+=l[i]
 else:
  ans+=10-l[i]
  n+=(10-l[i])
  sn+=(10-l[i])*(10**(l_len-1-i))
  l=[int(x) for x in str(n)]
# print(n)

#print(sn)
if len(str(n))!=len(str(sn)):
 for i in range(len(str(sn))-len(str(n))):
  j=i-1
  num=l[i]
  if l[i]<=10-l[i]:
   ans+=l[i]
  else:
   ans+=10-l[i]
   n+=(10-l[i])
   sn+=(10-l[i])*(10**(l_len-1-i))
   l=[int(x) for x in str(n)]

print(ans)

Submission status: Wrong Answer

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

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

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

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

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

s = input()
ans = 0
kuriagari = False
for c in reversed(s):
    a = int(c)

    pay=0
    back=0

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

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
      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:
import bisect

S=input()
dig=len(S)

ans=0

b=0#繰り上がり
for i in range(dig):
    a=int(S[dig-i-1])
    if a<=4:
        ans+=a
        b=0
    elif a>=6:
        ans+=(10-a)
        b=1
    else:#a==5
        if i==dig-1:
            ans+=5
            b=0
        else:
            if int(S[dig-i-2])>=5:
                b=1
            else:
                b=0
            ans+=5


        

if b==1:
    ans+=1

    
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
def solve():
    N = input()[::-1]+'00'
    res = 0
    c = 0
    for i in range(len(N)-1):
        print(N[i],c)
        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
            c += 1
    return str(res)

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

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)]) <= 5:
            pay += d
        else:
            ret += 10 - d
            next_needed = True

if next_needed:
    pay += 1

print(pay + ret)

Submission status: Wrong Answer

Solution code:
N="0"+input()

cnt=0
kuri=0
for s in N[::-1]:
    n=int(s)+kuri
    if n<=5:
        cnt+=n
        kuri=0
    else :
        cnt+=10-n
        kuri=1

print(cnt)

Submission status: Wrong Answer

Solution code:
def main():
    N = input()
    ans = 0
    carry = 0
    prev_5 = False
    for c in N[::-1]:
        if prev_5 and int(c) <= 4:
            carry = 0
        if int(c) + carry <= 4:
            ans += int(c) + carry
            carry = 0
            prev_5 = False
        elif int(c) + carry == 5:
            ans += 10 - (int(c) + carry)
            carry = 1
            prev_5 = True
        else:
            ans += 10 - (int(c) + carry)
            carry = 1
            prev_5 = False
    if carry == 1:
        ans += 1
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N = input()

result = 0
carry = 0
for c in N[::-1]:
    t = int(c) + carry
    if t <= 11 - t:
        result += t
        carry = 0
    else:
        result += 10 - t
        carry = 1
result += carry
print(result)