Problem:


Score: 500 points

Problem Statement

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

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

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

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

Constraints

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

Input

Input is given from Standard Input in the following format:

N

Output

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

Sample Input 1

36

Sample Output 1

8

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

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

Sample Input 2

91

Sample Output 2

3

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

Sample Input 3

314159265358979323846264338327950288419716939937551058209749445923078164062862089986280348253421170

Sample Output 3

243



Sample input:
36


Sample output:
8

Submission status: Wrong Answer

Solution code:
def 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
    index4 = [0]*N
    for i,s in enumerate(S):
        if int(s)>4:
            continue
        elif int(s)==4:
            index4[i] = 1
        else:
            index4[i] = 2
    f = False
    for i in range(N-1,-1,-1):
        if f:
            if index4[i]>=1:
                index4[i] = 2
            else:
                index4[i] = 0
        else:
            if index4[i] == 1:
                index4[i] = index4[(i + 1)-N]
        if index4[i] == 0:
            f = False
        if index4[i] == 2:
            f = True
    #print(index4)
    index5 = [0]*N
    for i,s in enumerate(S):
        if int(s)<5:
            continue
        elif int(s)==5:
            index5[i] = 1
        else:
            index5[i] = 2
    f = False
    for i in range(N-1,-1,-1):
        if f:
            if index5[i]>=1:
                index5[i] = 2
            else:
                index5[i] = 0
        else:
            if index5[i] == 1:
                index5[i] = index5[(i + 1)-N]
        if index5[i] == 0:
            f = False
        if index5[i] == 2:
            f = True
    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 index4[i]==0):
                    ans += 9-int(S[i])
                else:
                    ans += int(S[i])
                    flag = False
            continue

        if index5[i]==2:
            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 = input()

count = 0
prev = -1
for s in N:
  d = int(s)
  if prev >= 6 and d >= 6:
    count -= 2
  if prev >= 6 and d == 5:
    count -= 1
  if d <= 5:
    count += d
  else:
    count += 11 - d
  prev = int(s)

print(count)

Submission status: Wrong Answer

Solution code:
N = input()

ans = 0

flag = False
for d in list(N):
    d = int(d)

    if d <= 5:
        ans += d
        flag = False
    else:
        if flag:
            ans += -1 + (10 - d) 
        else:
            ans += 1 + (10 - d)
        flag = True        

print(ans)

Submission status: Wrong Answer

Solution code:
N=list(input())

N.insert(0,0)
ans=0
cnt=0
for i in range(len(N)-1,-1,-1):
  if int(N[i])==int(N[i-1])==9: cnt+=1

  n=int(N[i])
  if n//10>=1:
    N[i-1]=str(int(N[i-1])+1)
    continue
    
  if n>=10-n:
    ans+=10-n
    N[i-1]=str(int(N[i-1])+1)
  else:
    ans+=n
    
print(ans-cnt)

Submission status: Wrong Answer

Solution code:
n = list(input())
n.insert(0, '0')

ans = 0
for i in range(len(n)-1, -1, -1):
    if n[i] == '0':
        pass
    elif int(n[i]) <= 5:
        ans += int(n[i])
    else:
        ans += 10 - int(n[i])
        n[i-1] = str(int(n[i-1]) + 1)

print(ans)

Submission status: Wrong Answer

Solution code:
N = input()
ans = 0
for i in range(len(N)):
    if i == 0:
        if N[0] == "9":
            ans += 2
        elif len(N) >= 2 and int(N[1]) <= 5:
            ans += int(N[0])
        else:
            ans += 1 + int(N[0])
        continue
    if int(N[i]) > 5:
        ans += 10 - int(N[i])
    else:
        ans += int(N[i])

print(ans)

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:
		#print(N)
		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
	if len(s) < cntt:
		ans += 1

	#print(len(s))
	#print(cntt)
	print(ans+1)



if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
def sub(line):
    a,b = line.strip().split()
    a = [int(item) for item in a]
    b = [int(item) for item in b]
    res = ''
    for i in range(len(b)):
        flag_a = len(a)-1-i
        flag_b = len(b)-1-i
        if a[flag_a]>= b[flag_b]:
            res = str(a[flag_a]-b[flag_b])+res
        else:
            res = str(10+a[flag_a]-b[flag_b])+res
            while a[flag_a-1]==0:
                a[flag_a-1]=9
                flag_a -= 1
            a[flag_a-1] -= 1
    for j in range(len(a)-1-i-1,-1,-1):
        res = str(a[j])+res
    zero_flag=0
    for i in range(len(res)):
        if res[i]!='0':
            zero_flag=1
            break
    if zero_flag==0:
        return 0
    return res[i:]

n = input()
a = [int(x) for x in n]
b = [int(x) for x in sub('1'+'0'*len(n)+" "+n)]
s_a = [0]*(len(a))
s_b = [0]*(len(b)+1)
for i in range(len(a)):
    s_a[i] = s_a[i-1]+a[i]
s_a = [0]+s_a
for i in range(len(b)-1, -1, -1):
    s_b[i] = s_b[i+1]+b[i]
s_b[-1] = -1

r = 999999999999999999
for x,y in zip(s_a, s_b):
    r = min(r, x+1+y)
print(r)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 16 21:44:28 2020

@author: H_Hoshigi
"""

N = input()

n_list = []
for i in range(len(N)):
    n_list.append(int(N[-(i+1)]))
n_list.append(0)

counter = 0
for i in range(len(N)+1):
    if n_list[i]<=4:
        counter += n_list[i]
        n_list[i] = 0
    else:
        counter += 10-n_list[i]
        n_list[i] = 0
        n_list[i+1] += 1
    #print(counter)
    #print(n_list)

print(counter)

Submission status: Wrong Answer

Solution code:
# print('input >>')
N = int(input())
ans = 0
while N > 0:
    if N % 10 >= 6:
        ans += 10 - (N % 10)
        N += 10
    else:
        ans += N % 10
    N //= 10
    # print(N, ans)

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

Submission status: Wrong Answer

Solution code:
def dis(a,num):
  return int((num % 10 ** (a + 1)) / 10 ** a)

inp_num = input()
intnum = int(inp_num)
ans = 0
i = 0
while intnum != 0:
  if dis(i,intnum) <= 4:
    ans += dis(i,intnum)
    intnum -= dis(i,intnum) * (10 ** i)
  elif (dis(i,intnum) == 5 and dis(i+1,intnum) == 9) or dis(i,intnum) >= 6:
    ans += 10 - dis(i,intnum)
    intnum += (10 - dis(i,intnum)) * (10 ** i)
  else:
    ans += dis(i,intnum)
    intnum -= dis(i,intnum) * (10 ** i)
  i += 1
print(ans)

Submission status: Wrong Answer

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


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


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

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

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

    print(num)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
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+=5
            flag=3
        elif flag==3:
            count+=5
            flag=1
        else:
            count+=9-n
    else:

        if flag==2:
            count+=9-n
        if flag:
            count+=9-n
        else:
            count+=10-n+1
        flag=1
print(count)

Submission status: Wrong Answer

Solution code:
N = list(input())
ans = len(N)*5
A = 0
for i in N:
    ans -= abs(5-int(i))
    if (A >0 and int(i)> 5):
        ans += 1
    elif (A < 0 and int(i)< 5):
        ans += 1
    
    if 5-int(i)>0:
        A = 1
    elif 5 - int(i)<0:
        A = -1
    else:
        A = 0
print(ans)

Submission status: Wrong Answer

Solution code:

def main():
    N = input()
    x = len(N)
    max_kachi = 1000
    ans = 0
    change = 0
    large = False
    for i in N:
        if int(x) > max_kachi:
            ans += int(i)
            x -= 1
            continue
        
        if int(i) <= 5:
            ans += int(i)
            large = False
        else:
            if large:
                change += 9 - int(i)
            else:
                change += 10 - int(i)

            if not large:
                ans += 1
                large = True

    print(ans+change)

main()

Submission status: Wrong Answer

Solution code:
N = list(input())
ans = len(N)*5
A = 0
for i in N:
    ans -= abs(5-int(i))
    if (A >0 and int(i)> 5):
        ans += 1
    elif (A < 0 and int(i)< 5):
        ans += 1
    
    if 5-int(i)>0:
        A = 1
    elif 5 - int(i)<0:
        A = -1
    else:
        A = 0
    print(ans)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = input()
pay_r = [0]*(len(N)+1)
for i in range(len(N)):
    pay_r[i] = int(N[len(N)-1-i])
for i in range(len(N)):
    if pay_r[i] >= 10:
        pay_r[i+1] += pay_r[i] // 10
        pay_r[i] = pay_r[i] % 10
    if pay_r[i] > 10 - pay_r[i] + 1:
        pay_r[i] = 0
        pay_r[i+1] += 1
ans = sum(pay_r)
pay = 0
for i in range(len(pay_r)):
    pay += (10**i)*pay_r[i]
oturi = pay - int(N)
oturi_r = list(str(oturi))
for i in oturi_r:
    ans += int(i)
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 n[-1] != '0':
        ans += 1
    print(ans)

Submission status: Wrong Answer

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    S=[int(s) for s in ST()[::-1]]
    T=S.copy()
    ans = 0
    flag = False
    for i,s in enumerate(S):
        t=S[i]
        if t>=10:
            try:
                S[i+1]+=1
            except:
                flag = True
            t-=10
        if t>5:
            ans += 10-t
            try:
                S[i+1]=(S[i+1]+1)
            except:
                flag = True
        elif t!=5:
            ans += t
        else: #5
            ans += t
            try:
                if S[i+1]>=5:
                    S[i+1] += 1
            except:
                pass
        print(ans)
    print(ans+int(flag))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n=input()
to=0
num=len(n)
m=0

for j in range(num):
    i=n[num-j-1]
    if m==1:
        i=int(i)+1
    if int(i)<=5:
        to+=int(i)
        m=0
    elif int(i)>5:
        to+=10-int(i)
        m=1
        if j==num-1:
            to+=1
        
print(to)

Submission status: Wrong Answer

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

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


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

Submission status: Wrong Answer

Solution code:
#import numpy as np
from sys import stdin
from copy import deepcopy
from functools import reduce
from fractions import gcd
import math
import itertools
from collections import Counter
from itertools import chain  ##list(chain.from_iterable(l)) ##flatten
from heapq import heappush, heappop
import bisect

"""
N = int(input())
A, B = map(int,input().split())
A = list(map(int,input().split())) ## 横一列
A = [int(input()) for _ in range(N)] ## 縦一列
lst = [list(map(int,input().split())) for i in range(M)] ## 二次元
"""

N = int(input())

digit = len(str(N))
pay = 0
jump = False
for i in range(digit):
  if (int(str(N)[-1-i]) + 1 * (jump == True)) <= 5:
    pay += 10**i * (int(str(N)[-1-i]) + 1 * (jump == True))
    jump = False
  else:
    jump = True
else:
  if jump == True:
    pay += 10**digit

#print(pay)
n_pay = sum(list(map(int, str(pay))))
n_back = sum(list(map(int, str(pay - N))))
ans = n_pay + n_back

print(ans)

Submission status: Wrong Answer

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

N = int(input())

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

print(ans)

Submission status: Wrong Answer

Solution code:
N = input()
c = 0
d = dict()
for n in range(len(N)):
    if int(N[n]) >= 6:
        if n - 1 in d:
            d[n - 1] += 1
        else:
            d[n - 1] = 1
        if n in d:
            d[n] -= 10 - int(N[n])
        else:
            d[n] = -(10 - int(N[n]))
    else:
        d[n] = int(N[n])


for k, v in d.items():
    c += abs(v)

print(c)

Submission status: Wrong Answer

Solution code:
N = list(input())
ans = 0
for i in range(len(N)):
    if int(N[i]) >= 6:
        ans += 11-int(N[i])
    else:
        ans += int(N[i])

print(ans)

Submission status: Wrong Answer

Solution code:
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

def main():
    s = input()
    s = s[::-1]
    ans = 0
    tmp = 0
    a = [0] * len(s)
    flag = False
    count = 0

    for i in range(len(s)):
        if flag:
            if int(s[i]) < 5:
                tmp = 0
                flag = False
                count = 0
            elif int(s[i]) > 5:
                tmp = 1
                flag = False
                ans = ans - count
                count = 0
            elif int(s[i]) == 5:
                count += 1
                ans += 5
                continue
            
        ima = int(s[i]) + tmp
        if ima == 5:
            flag = True
            ans += 5

        elif (int(s[i]) + tmp < 6) :
            ans += int(s[i]) + tmp
            tmp = 0
        else:
            if int(s[i]) - tmp != 10:
                if i == len(s) -1:
                    ans += 11 - int(s[i]) - tmp
                    tmp = 1
                else:
                    ans += 10 - int(s[i]) - tmp
                    tmp = 1
            else:
                if i == len(s) -1:
                    ans +=1
                else:
                    tmp = 1
    print(ans)


main()

Submission status: Wrong Answer

Solution code:
Ns = [int(s) for s in input()]
total = 0
kuri = 0
for n in Ns[::-1]:
  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 = str(input())
#print(len(N))
ans = 0
j = 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
    else:
      ans += 10 - x
      j = 1
  
if j == 1:
  ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
N = input()
result = 0
carry = 0
for i in reversed(range(len(N))):
    if i == 0 and carry == 1:
        result += 1
    num = int(N[i])
    if num > 5:
        result += 10 - num + 1 - carry
        carry = 1
    else:
        result += num - carry
        carry = 0

print(result)

Submission status: Wrong Answer

Solution code:
n=str(input())
p=n
l=len(n)
ans=0
for i in reversed(range(l)):
    if int(p[i])>=5:
        p=str(int(p)+10**(l-i)-int(p[i])*10**(l-i-1))
c=str(int(p)-int(n))
for i in range(len(p)):
    ans+=int(p[i])
for i in range(len(str(c))):
    ans+=int(c[i])

print(ans)

Submission status: Wrong Answer

Solution code:
S = list(map(int,list(input())))
ans = 0
di = {1:1,2:2,3:3,4:4,5:5,6:4,7:3,8:2,9:1,0:0}
for i in range(len(S)-1,-1,-1):
    ans += di[S[i]]
if S[0] >= 6 or S[min(1,len(S)-1)] >= 6:
    ans += 1
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)

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

    print(sum(n))


if __name__ == '__main__':
    main()

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:
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:
        pay.append(l[i])
    if 10 > l[i] > 5:
        pay.append(0)
        l[i+1] += 1
    if 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:
N = input()
count = 0
for i in range(len(N)):
  if int(N[i]) < 6:
    count += int(N[i])
  else:
    count += 11 - int(N[i])
print(count)

Submission status: Wrong Answer

Solution code:
s=input()
chk=int(s)
p=len(s)
a=[]
x=y=0

for i,j in enumerate(s[::-1]):
    jk=int(j)
    if jk+x<5:
        y+=jk*(10**i)
    else:
        y+=10*(10**i)
    if jk>5: x=1
    else: x=0
z=y-chk
#print(y)
#print(chk)
#print(z)
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:
#!/usr/bin/env python3
# https://atcoder.jp/contests/abc155/tasks/abc155_e

n = input()
d = []
for l in n:
    d.append(int(l))
cnt = 0
for i in range(len(d) - 1, 0 - 1, -1):
    if 0 <= d[i] <= 5:
        cnt += d[i]
    else:
        cnt += (10 - d[i])
        if i > 0:
            d[i - 1] += 1
        else:
            cnt += 1
    # print(f"{i} keta: {d[i]} ... {cnt}mai")
# print("ANS: ", end='')
print(cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())

N = list(map(int,str(n)))

# print(N)

Nr = list(reversed(N))
Nr.append(0)
# print(Nr)

ans = 0 
for i in range(len(Nr)):
    if i<(10**100):
        if Nr[i] > 5:
            Nr[i+1] += 1
            ans += (10 - Nr[i])
        else:
            ans += Nr[i]
    elif i==(10**100):
        A = Nr[i:]
        B = list(map(str,A))
        C = "".join(B)
        D = int(C)
        ans += D
    else:
        break
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())

Ntmp = N
result = 0


i = 0

while 'true':
    if Ntmp // (10 ** i) <= 0:
        break
    if Ntmp // (10 ** i) % 10 < 5:
        result += Ntmp // (10 ** i) % 10
    else:
        result += 10 - (Ntmp // (10 ** i) % 10) 
        Ntmp += 10
    i += 1

print(result)

Submission status: Wrong Answer

Solution code:
N = input()

ans = 0

carry = 0

N_rev = N[::-1]

for i in range(len(N_rev)):
    if int(N_rev[i]) + carry <= 5:
        ans += (int(N_rev[i]) + carry)
        carry = 0
    else:
        ans += (10-int(N_rev[i])-carry)
        carry = 1


print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

S = input()
S = S[::-1]
N = len(S)

dp0 = [INF] * (N+1)
dp1 = [INF] * (N+1)
dp0[0] = 0
for i, s in enumerate(S):
    s = int(s)
    dp0[i+1] = min(dp0[i+1], dp0[i] + s)
    dp1[i+1] = min(dp1[i+1], dp0[i] + 10-s)
    dp0[i+1] = min(dp0[i+1], dp1[i] + (s+1)%10)
    dp1[i+1] = min(dp1[i+1], dp1[i] + 10-(s+1)%10)
ans = min(dp0[N], dp1[N]+1)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin
from itertools import accumulate

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

n = ns()
allzero = True
pay, back = 0, 0
for ni in reversed(n[1:]):
    ni = int(ni)
    if ni > 4:
        if allzero:
            back += 10 - ni
        else:
            back += 9 - ni
        allzero = False
    else:
        if allzero:
            pay += ni
        else:
            pay += ni+1
ni = int(n[0])
if allzero:
    if ni == 9:
        pay += 1
        back += 1
    elif ni > 5:
        pay += 1
        back += 10 - ni
    else:
        pay += ni
else:
    if ni == 9:
        pay += 1
    elif ni > 5:
        pay += 1
        back += 10 - ni - 1
    else:
        pay += ni
print(pay+back)

Submission status: Wrong Answer

Solution code:
def solve_dp(s):
    n = len(s)
    s = [int(c) for c in s]
    inf = 10**9

    dp = [[inf] * 2 for _ in range(n)]

    d0 = s[0]
    dp[0][0] = d0
    dp[0][1] = d0+1

    for i in range(1, n):
        for j in range(2):
            d = s[i] + j
            for k in range(10):
                dp[i][j] = min(dp[i][j], dp[i-1][1] + (2*k + d)%10)


    return dp[n-1][0]

if __name__ == '__main__':
    s = input()
    s = "0"+ s
    print(solve_dp(s))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:

N = input()
mine = 0
k = True
def findnonfive(_N):
    for i in range(len(_N)):
        if int(_N[i]) !=5:
            return int(_N[i])
    return 0

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

Submission status: Wrong Answer

Solution code:
n = int(input())
cnt = 0
num = []
while n != 0:
    cnt += n % 10
    num.append(n % 10)
    n //= 10
ans = cnt
for i in range(len(num)):
    ans = min(ans, 10 - num[i] + cnt - num[i] + 1)
print(ans)

Submission status: Wrong Answer

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    S=[int(s) for s in ST()[::-1]]
    T=S.copy()
    ans = 0
    flag = False
    for i,s in enumerate(S):
        t=S[i]
        if t>=10:
            try:
                S[i+1]+=1
            except:
                flag = True
            t-=10
        if t>5:
            ans += 10-t
            try:
                S[i+1]=(S[i+1]+1)
            except:
                ans+=1
                flag = True
        elif t!=5:
            ans += t
        else: #5
            ans += t
            try:
                if S[i+1]>=5:
                    S[i+1] += 1
            except:
                pass
    print(ans+int(flag))


if __name__ == '__main__':
    main()