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:
import math
import sys
sys.setrecursionlimit(100000)

ans_calc = {}
ans_calc2 = {}

def calc(n):
    #print("calc",n)
    digit = len(str(n))
    if n in ans_calc:
        ans = ans_calc[n]
    elif digit==1:
        if n<=5:
            ans = n
        else:
            ans = (10-n)+1
    else:
        top = int(str(n)[0])
        if n-top*10**(digit-1)==0:
            ans = calc(top)
        else:
            cand1 = top + calc(n-top*10**(digit-1))
            cand2 = (top+1) + calc2((top+1)*10**(digit-1)-n)
            cand3 = 1 + calc2(10**(digit)-n)
            ans = min(cand1, cand2, cand3)
    #print("calc",n,ans)
    if n not in ans_calc:
        ans_calc[n] = ans
    return ans

def calc2(n):
    #print("calc2",n)
    digit = len(str(n))
    if n in ans_calc2:
        ans = ans_calc2[n]
    if digit == 1:
        if n<=5:
            ans = n
        else:
            ans = (10-n)+1
    else:
        top = int(str(n)[0])
        cand1 = top + calc2(n-top*10**(digit-1))
        cand2 = (top+1) + calc((top+1)*10**(digit-1)-n)
        ans = min(cand1, cand2)
    #print("calc2",n,ans)
    if n not in ans_calc2:
        ans_calc2[n] = ans
    return ans

N = int(input())
ans = calc(N)
print(ans)

Submission status: Wrong Answer

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

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

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

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

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:
                if i != len(s) -1:
                    count += 1
                    ans += 5
                    continue
                else:
                    count += 1
                    ans += 6
                    ans = ans -count
                    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:
import sys

def solve():
    N = str(input())
    dig = len(N)
    ans = 0
    up = False
    for i in reversed(range(dig)):
        n = int(N[i])
        if 0 <= n <= 4: 
            if up: ans += n + 1
            else: ans += n 
            up = False
        elif n == 5:
            if up: ans += 4
            else: 
                ans += 5
                if i > 0:
                    if int(N[i-1]) >= 5: up = True
        else: 
            if up: ans += 9 - n
            else: ans += 10 - n
            up = True
        #print(n, ans)
    if up: ans += 1
    print(ans)

    return 0

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

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

ans = 0

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

print(ans)

Submission status: Wrong Answer

Solution code:

#    0, 1, 2, 3, 4, 5, 6, 7, 8, 9
P = [0, 1, 2, 3, 4, 0, 0, 0, 0, 0]
C = [0, 0, 0, 0, 0, 5, 4, 3, 2, 1]
X = [0, 1, 2, 3, 4, 5, 4, 3, 2, 1]

def solve(N):
    N = [int(c) for c in N]
    N = N[::-1]
    p = 0
    b = 0

    for i in range(len(N)):
        c = N[i]
        c += b
        if c == 10:
            c = 0
            b = 1
            p += X[c]
            #print(P[c], end="")
        elif c == 5:
            if i == len(N)-1:
                p += 5
                #print(5, end="")
            else:
                if N[i+1] < 5:
                    p += 5
                    b = 0
                    #print(5, end="")
                else:
                    p += 5    
                    b = 1
                    #print(0, end="")
        elif c > 5:
            b = 1
            p += X[c]
            #print(P[c], end="")
        else:
            b = 0
            p += X[c]
            #print(P[c], end="")
    #print(b)
    return p + b

def count(n):
    return sum([int(x) for x in str(n)])

def test(N):
    m = 10e10
    p = 0
    for P in range(N, N*10):
        n = count(P) + count(P - N)
        if n < m:
            p = P
            m = n
    e = solve(str(N))
    print("N=%d"%N, "expected=%d"%m, p, "actual=%d"%e)

N = [int(x) for x in input()]
print(solve(N))
if 1==0:
    test(159)
    test(149)
    test(449)
    test(959)
    test(949)
    test(939)
    test(539)
    test(569)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input().strip())

tmp=n
ans=0
while tmp>0:
    lastdig=int(str(tmp)[-1])
    if lastdig>5:
        ans+=10-lastdig
        ans+=1 # next digit
    else:
        ans+=lastdig
    tmp//=10
    print(ans)
print(ans)

Submission status: Wrong Answer

Solution code:
m = int(input())
n = [0]+list(str(m))
for i in range(len(n)):
    n[i] = int(n[i])

for i in range(len(n)):
    if n[(i + 1) * (-1)] >= 10:
        n[(i + 1) * (-1)] -= 10
        n[(i + 2) * (-1)] += 1
    if n[(i + 1) * (-1)] >= 6:
        n[(i + 2) * (-1)] += 1
        n[(i + 1) * (-1)] = 0
    if n[(i + 1) * (-1)] == 5:
        if n[(i + 2) * (-1)] >= 5 :
            n[(i + 2) * (-1)] += 1
            n[(i + 1) * (-1)] = 0
cnt = sum(n)

num = ''
for i in n:
    num = num + str(i)
num = int(num)

m = num - m
print(m)
n = [0]+list(str(m))
for i in range(len(n)):
    n[i] = int(n[i])
cnt += sum(n)

print(cnt)

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():
	s = input()
	s = s[:-1]
	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 = ans - (len(s) - cntt)
	if len(s) < cntt:
		ans = ans + (cntt - len(s))

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



if __name__ == "__main__":
	main()

Submission status: Wrong Answer

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

x = 0
for i, k in enumerate(n):
    if i == len(n) - 1:
        q = 10
    else:
        q = 9
    if q - k + 1 + (i == 0) > k:
        x += k
    else:
        x += q - k + 1 + (i == 0)
print(x)

Submission status: Wrong Answer

Solution code:
N = int(input())
P = (N + 9) // 10 * 10


def no_notes(a):
    Q = sorted([pow(10, x) for x in range(len(str(a)))], reverse=True)
    x = 0
    for i in range(len(Q)):
        q = Q[i]
        x += int(a / q)
        a = int(a % q)
    if a > 0:
        x = -1
    return x


S = no_notes(P)
if P % N < 5:
    S += P % N
else:
    S += 1 + N % 10
print(S)

Submission status: Wrong Answer

Solution code:
s = '0'
s += input()
ans = 0
plus = 0
for i in range(len(s)-1,-1,-1):
  t = s[i]
  if plus == 0:
    if int(t) <= 4:
      ans += int(t)
    else:
      ans += 10-int(t)
      plus = 1
  else:
    if int(t)+1 <= 5:
      ans += int(t)+1
      plus = 0
    elif int(t)+1 <= 9:
      ans += 9-int(t)
      plus = 1
print(ans)

Submission status: Wrong Answer

Solution code:
N=input()
keta = len(N)
counter = 0
kuriage = 0
for i in N[::-1]:
    num = int(i) + kuriage
    kuriage = 0
    if (num < 6):
        counter += num
    else:
        counter += (10 - num)
        kuriage = 1
if (kuriage == 1):
    counter += 1
print(counter)

Submission status: Wrong Answer

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

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


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

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

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

Submission status: Wrong Answer

Solution code:
N = '0' + input()
f = False
ans = 0
for i in range(1, 1 + len(N)):
  d = int(N[-i])
  if f:
    d += 1
  if d <= 5:
    ans += d
    f = False
  else:
    ans += 10 - d
    f = True
print(ans)

Submission status: Wrong Answer

Solution code:
inp = list(input())

count = 0
# print(len(inp))

if len(inp) >= 3:
  for i in inp:
    num = int(i)
    if num >= 6:
      count += 10 - num
    else:
      count += num
else:
  for i in inp:
    num = int(i)
    if num >= 6:
      count += 11 - num
    else:
      count += num
      
print(count)

Submission status: Wrong Answer

Solution code:
N = input()
digit_length = len(N)
# 最後の桁の反転は9-int(d)ではなく10-int(d)
ans = 0
is_reverse = False
for i, d in enumerate(N):
    if i == digit_length-1: # 最終桁の場合

        if is_reverse == False:
            if int(d) <= 5:
                ans += int(d)
            else:
                #ans += 10 - int(d)
                ans += 11 - int(d)
                is_reverse = True
        else:
            if 9 - int(d) <= 5:
                ans += 9-int(d)
            else:
                ans += 10 - (9-int(d))
                is_reverse = False


    elif is_reverse == False:
        if int(d) <= 5:
            ans += int(d)
        else:
            ans += 10-int(d)
            is_reverse = True
    else: # is_reverse == True
        if 9 - int(d) <= 5:
            ans += 9-int(d)
        else:
            ans += 10 - (9-int(d))
            is_reverse = False

print(ans)

Submission status: Wrong Answer

Solution code:
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:
    carry = 0

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

d = len(N)
culc = [0] * (d+1)

for i, digit in enumerate(N):
  digit = int(digit)
  if digit < 6:
    culc[i+1] = digit
  else:
    culc[i] += 1
    culc[i+1] -= (10 - digit) 

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

Submission status: Wrong Answer

Solution code:
#from fractions import gcd
#mod = 10 ** 9 + 7
#N = int(input())
#a = list(map(int,input().split()))
#a,b,c = map(int,input().split())
#ans = [0] * N
#math.ceilで切り上げ
#dp = [[0] * 4 for i in range(3)] #2次元配列初期化
#dp = [[[0] * 2 for i in range(3)] for j in range(5)]
#(ord('A'))でASCII出力 Aは65
import math
import string

def intinput():
    return int(input())

def listintinput():
    return list(map(int,input().split()))

def splitintinput():
    return map(int,input().split())

N = input()
ans = 0
flag = 0 # 桁上がりのフラグ管理
fiveflag = 0
s = len(N)
tmp = 0
for i in range(s):
    if int(N[s-i-1]) + flag == 0:
        continue     
    elif int(N[s-i-1]) + flag < 6:
            ans += int(N[s-i-1]) + flag
            flag = 0
    elif int(N[s-i-1]) + flag < 10:
        ans += 10 - int(N[s-i-1])
        if flag == 1:
            ans -= 1
        flag = 1
    else:
        if flag == 1:
            ans -= 1
        continue

print(ans + flag)

Submission status: Wrong Answer

Solution code:
s =list(map(int, list(input())))
INF = 1001001001001
dp1=[INF]*1001001
dp2=[INF]*1001001
n=len(s)
dp1[n-1]=s[n-1]
dp2[n-1]=10-s[n-1]
for i in reversed(range(n-1)):
    if s[i]<9:
        dp1[i] = min(dp1[i+1]+s[i],dp2[i+1]+s[i]+1)
    dp2[i] = min(dp1[i+1]+10-s[i], dp2[i+1]+10-s[i]-1)
    
if dp1[0]==INF:
    dp2[0]+=1
print(min(dp1[0], dp2[0]))

Submission status: Wrong Answer

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

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


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

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

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

Submission status: Wrong Answer

Solution code:
N = input()
ln = int(N[0])
res = 0
if ln < 6:
  res += ln
else:
  res += 10 - ln + 1

for i in range(10):
  c = N[1:].count(str(i))
  if i < 6:
    res += i * c
  else:
    res += (10 - i) * c

print(res)

Submission status: Wrong Answer

Solution code:
s = "0" + input()
s = list(map(int,s))
so = list(map(int,s))
pay = 0
otsuri = 0
import collections
dpay = collections.deque([])
dotsuri = collections.deque([])

kuriagari = False
for i in range(len(s)-1, -1, -1):
    c = s[i]

    if i == 0 and c == 0:
        break

    if c == 10:
        s[i] = 0
        s[i - 1] += 1
        dotsuri.appendleft(0)
        dpay.appendleft(0)
        continue

    if 0 <= c <= 5:
        pay += c # 自分が払う
        dpay.appendleft(c)
        dotsuri.appendleft(0)
    elif 6 <= c:
        s[i - 1] += 1
        otsuri += (10 - c)  # お釣りの枚数
        dpay.appendleft(0)
        dotsuri.appendleft((10 - c) )

print(pay + otsuri)

Submission status: Wrong Answer

Solution code:
import sys

input_methods=['clipboard','file','key']
using_method=2
input_method=input_methods[using_method]

IN=lambda : map(int, input().split())
mod=1000000007

#+++++

def main():
	s = input()
	if s=='9':
		return 2
	
	cc = [0]*(len(s)+1)
	for i in range(len(s)+1):
		cc[i]=[0,0,0,0]
	init=int(s[-1])
	cc[0]=[0,0,10-init, init]
	for i, c in enumerate(s[::-1]):
		aa,ab,ba,bb=cc[i]
		v=int(c)
		cc[i+1]=[10-(v+1)+min(aa,ba),(v+1)+min(aa,ba),(10-v)+min(ab,bb),v+min(ab,bb)]
		
	if s[0]=='9':
		cc.append([0,0,0,0])
		aa,ab,ba,bb=cc[-2]
		v=0
		cc[-1]=[10-(v+1)+min(aa,ba),(v+1)+min(aa,ba),(10-v)+min(ab,bb),v+min(ab,bb)]	
		#pa(cc[-1])
		
	ret=min(cc[-1])
	print(ret)
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

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[0] != '1':
        ans += 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
A=kouho
B=oturi
ans=0
while kouho or oturi:
    kouho, amari= divmod(kouho, 10)
    ans+=amari
    oturi, amari = divmod(oturi, 10)
    ans+=amari
print(ans)

Submission status: Wrong Answer

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

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


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

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

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

Submission status: Wrong Answer

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

rev_pay = [0] * (lenn + 1) 

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

turi = int("".join(map(str, pay))) - num_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 = int(input())
ans = 0
while N > 1:
    bottom = N % 10 
    if bottom <= 5:
        ans += bottom
    else:
        ans += 10 + 1 - bottom
    N = N // 10
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin,setrecursionlimit
setrecursionlimit(10**6) 
n = int(stdin.readline().rstrip())
s = str(n)
o = ""
s_o = 0
s_s = 0
mi = 10**200
for i in s:
    o += str(10-int(i))
    s_o += 10-int(i)
    s_s += int(i)
for i in range(len(s)):
    k = int(s[i])+1
    otsuri = s_o-int(o[i])
    mi = min(mi,otsuri+k)
print(mi)

Submission status: Wrong Answer

Solution code:
n = int(input())

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

l.reverse()

#print(l)

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

Submission status: Wrong Answer

Solution code:
#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:
from bisect import *
from collections import *
from fractions import gcd
from math import factorial
from itertools import *
from heapq import *
import copy

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


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

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

d = len(N)
culc = [0] * (d+1)

for i, digit in enumerate(N):
  digit = int(digit)
  if digit < 6:
    culc[i+1] = digit
  else:
    culc[i] += 1
    culc[i+1] -= (10 - digit) 

ans = 0
for i in culc:
  ans += abs(i)
    
print(culc)
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():
	s = input()
	s = s[:-1]
	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:
n=input()
m=list(n)
l=[int(i) for i in m]
l.reverse()
l.append(0)
k=l
ans=0
for i in range(len(l)):
  if k[i]<6:
    ans+=k[i]
  else:
    ans+=10-k[i]
    k[i+1]+=1
print(ans)

Submission status: Wrong Answer

Solution code:
N = input()

s = [0]
t = []

cnt = 0
for j,i in enumerate(N[::-1]):
    x = int(i)
    p = int(s[-1])
    if (p+x)< (11-x-p) and (x<5):
        if x==5:
            cnt += 1
        s[-1] += x
        s.append(0)
        t.append(0)
    else:
        pp = s[-1]
        s[-1] -= min(pp, x)
        s.append(1)
        t.append(10-x-min(pp, x))

ans1 = sum(t)+sum(s)

s = [0]
t = []

cnt = 0
for j,i in enumerate(N[::-1]):
    x = int(i)
    p = int(s[-1])
    if (p+x)< (11-x-p) and (x<=5):
        if x==5:
            cnt += 1
        s[-1] += x
        s.append(0)
        t.append(0)
    else:
        pp = s[-1]
        s[-1] -= min(pp, x)
        s.append(1)
        t.append(10-x-min(pp, x))

ans2 = sum(t)+sum(s)
print(ans2-(ans2-ans1)*2)

Submission status: Wrong Answer

Solution code:
# coding: utf-8


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

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


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

Submission status: Wrong Answer

Solution code:
import numpy as np

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

ans = 0

num_0to5 = l[l<=5]
num_6to9 = l[l>=6]

ans = np.sum(num_0to5)
ans += np.sum(11-num_6to9)


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

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


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

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

ans -= dd

print(ans)

Submission status: Wrong Answer

Solution code:
N = input()
l = len(N)
#A = [0]*(N+2)
ans=0
def che(N,a):
    #l = len(N)
    #b+=1
    k=int(N[0])
    if(k>=5):
        a+=1+(10-k)-1
    else:
        a+=k
    if(l!=0):
        che(N[1:l],a)



for i in range(l):
    a=0
    k=k=int(N[i])
    if(k>=5):
        a+=1+(10-k)-1
    else:
        a+=k
print(a)

Submission status: Wrong Answer

Solution code:
n = int(input())

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

l.reverse()

#print(l)

dasu=0
turi=0
kuri=False
kuri_cnt=0
for num in l:

    num_kuri=num
    if kuri:
        num_kuri=num+1

    if num_kuri <=5:
        dasu+=num
        kuri=False
    else:
        dasu+=1
        turi+=10-num
        kuri=True
        kuri_cnt+=1

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

Submission status: Wrong Answer

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

for i, j in enumerate(n[::-1]):
    if i == len(n) - 1:
        if j < 6:
            cnt += j
        if j > 5:
            cnt += (10 - j) + 1
            
    else:
        if j < 6:
            cnt += j
        if j > 5:
            cnt += (10 - j)
            n[i+1] += 1

print(cnt)

Submission status: Wrong Answer

Solution code:
n = input()

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

if tmp:
    pay += tmp

#print(pay, ret)

ans = pay + ret

print(ans)