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
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()
log(len(s))
ans = 0

# dp = [ [999999999999999999] * len(s) for _ in range(2) ]

# # dp[k][i] = 下からi桁目で繰り上がりあるなしの最小コスト

# dp[0][0] = 0
# dp[1][0] = 0

# for i,c in enumerate(s):
#     if i == 0:
#         continue
#     a = int(c)

#     dp[0][i] = dp[]

kuriagari = False
prev = None
prev5 = 0
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
        else:
            if prev5:
                # log("prev5")
                ans -= prev5
            # ans += 10-a-1
            back = 10-a-1
            kuriagari = True
    else:
        if a <= 5:
            # ans += a
            pay = a
            kuriagari = False
        else:
            if prev5:
                # log("prev5", prev5)
                ans -= prev5
                prev5=0
            # 上の位1枚で払っておつり10-a枚
            # ans += 10-a
            back = 10-a
            kuriagari = True
    ans += pay+back
    if pay==5:
        prev5 += 1
    else:
        if prev5 > 2:
            ans -= (prev5-2)
        prev5 = 0
    # log(a, (pay, back), kuriagari, ans)
if kuriagari:
    ans += 1
if prev5 > 2:
    ans -= (prev5-2)
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()
log(len(s))
ans = 0

# dp = [ [999999999999999999] * len(s) for _ in range(2) ]

# # dp[k][i] = 下からi桁目で繰り上がりあるなしの最小コスト

# dp[0][0] = 0
# dp[1][0] = 0

# for i,c in enumerate(s):
#     if i == 0:
#         continue
#     a = int(c)

#     dp[0][i] = dp[]

kuriagari = False
prev = None
prev5 = 0
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
        else:
            if prev5:
                # log("prev5")
                ans -= prev5
            # ans += 10-a-1
            back = 10-a-1
            kuriagari = True
    else:
        if a <= 5:
            # ans += a
            pay = a
            kuriagari = False
        else:
            if prev5:
                # log("prev5", prev5)
                ans -= prev5
                prev5=0
            # 上の位1枚で払っておつり10-a枚
            # ans += 10-a
            back = 10-a
            kuriagari = True
    ans += pay+back
    if pay==5:
        prev5 += 1
    else:
        prev5 = 0
    # log(a, (pay, back), kuriagari, ans)
if kuriagari:
    ans += 1
if prev5 > 2:
    ans -= (prev5-2)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

n = N
ans = 0
n_dig = len(str(n)) # Nの桁数
mai = [0] * (n_dig + 1) # 枚数

for i in range(n_dig):
    d = n % 10
    n //= 10
    if mai[i] + d <= 5:
        mai[i] += d
    else:
        mai[i] = 10 - d - mai[i] # おつり
        mai[i + 1] = 1

print(sum(mai))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
n_str = str(n)
num_0to5 = []
num_6to9 = []

ans = 0

kurisagari_flg = False

for x in list(n_str):
    if int(x)<=5:
        num_0to5.append(int(x))
        ans += int(x)
        kurisagari_flg = False
    else:
        num_6to9.append(int(x))
        ans += 11 - int(x)
        if kurisagari_flg:
            ans -= 2
        kurisagari_flg = True
        
print(ans)

Submission status: Wrong Answer

Solution code:

n = input()
n_r = list(map(int,list(n)[::-1]))
for i in range(len(n)):
    if len(n_r) < i+1+1:
        n_r.append(0)
    if n_r[i] >= 10:
        n_r[i] -= 10
        n_r[i+1] += 1
    if n_r[i] > 5 or (n_r[i] > 4 and n_r[i+1] > 4) or (n_r[i] > 3 and n_r[i+1] > 9):
        n_r[i+1] += 1
        n_r[i] = 0
a = sum(map(int,list(n)))
print(int("".join(list(map(str,n_r[::-1]) ) ) ))
print("0000"+str(int("".join(list(map(str,n_r[::-1]) ) ) ) - int(n)))
tmp = map(int,list(str(int("".join(list(map(str,n_r[::-1]) ) ) ) - int(n))))
b = sum(n_r[::-1]) + sum(tmp)

if a > b:
    print(b)
else:
    print(a)

Submission status: Wrong Answer

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

DP = [[float("inf")]*2 for i in range(m+1)]
DP[0] = [0,1]

for i in range(m):
    # tight
    tmp = float("inf")
    for j in range(10):
        if j == 0:
            tmp = min(tmp, DP[i][1]+9-n[i])
        elif j <= n[i]:
            tmp = min(tmp, DP[i][1]+j+10+j-1-n[i])
        else:
            tmp = min(tmp, DP[i][0]+j+j-1-n[i])
    DP[i+1][1] = tmp

    # not tight
    tmp = float("inf")
    for j in range(10):
        if j < n[i]:
            tmp = min(tmp, DP[i][1]+j+(10+j)-n[i])
        else:
            tmp = min(tmp, DP[i][0]+j+j-n[i])
    DP[i+1][0] = tmp

print(min(DP[-1]))

Submission status: Wrong Answer

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

def solve(N):
    L = len(N)
    counts = [0 for i in range(L+1)]
    N = list(reversed("00" + N))
    for i in range(L+1):
        nd = int(N[i])
        if nd >= 10 - nd:
            k = 1
            while N[i+k] == "9":
                N[i+k] = "0"
                k += 1
            N[i+k] = str(int(N[i+k]) + 1)
            counts[i] += 10 - nd
        else:
            counts[i] += nd
    return sum(counts)


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

Submission status: Wrong Answer

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

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

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


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

Submission status: Wrong Answer

Solution code:
N = [int(x) for x in input()]+[0]
count = 0
for i in range(len(N)-1, 0, -1):
    if N[i] <= 4 or (N[i] == 5 and N[i-1] <= 4):
        count += N[i]
    else:
        count += 10 - N[i]
        N[i-1] = N[i-1] + 1
print(count)

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 not (i < n-1 and int(s[i+1]) >= 5)):
		ans += k
	else:
		ans += 10-k
		s[i] = "0"
		if i == n-1:
			ans += 1
		else:
			if i < n-1 and s[i+1] != "9":
				s[i+1] = str(int(s[i+1])+1)
			else:
				j = i+1
				while s[j] == "9":
					s[j] = "0"
					j += 1
					if j == n:
						ans += 1
						break
	#print("".join(s[::-1]))
print(ans)

Submission status: Wrong Answer

Solution code:
N = [int(x) for x in input()]
count = 0
for i in range(len(N)-1, -1, -1):
    if N[i] <= 4 or (N[i] == 5 and N[i-1] <= 4):
        count += N[i]
    else:
        count += 10 - N[i]
        N[i-1] = N[i-1] + 1
print(count)

Submission status: Wrong Answer

Solution code:
N = input()[::-1]

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

print(ans)

Submission status: Wrong Answer

Solution code:
s=input()
s_rev=s[::-1]

cnt=0
kuriage_flag=0
for i in s_rev:
    i=int(i)
    if kuriage_flag==1:
        i+=1
        kuriage_flag=0
    if i <6:
        cnt+=i
    else:
        kuriage_flag=1
        cnt+=10-i
if kuriage_flag==1:
    cnt+=1
print(cnt)

Submission status: Wrong Answer

Solution code:
N = [int(x) for x in input()]


def solve(N):
    # n円の場合、n+1円の場合
    # 繰り上げる方がいいのか繰り上げない方がいいのか判断している
    dp = 0, 1
    print(dp)
    for n in N:
        # 繰り上げた方が小さいのか繰り上げない方が小さいのか
        a = min(dp[0] + n, dp[1] + 10 - n)
        # 一個下の位を繰り上げた場合に、今の位までにかかる枚数
        b = min(dp[0] + n + 1, dp[1] + 10 - (n + 1))
        dp = a, b
    return dp[0]


print(solve(N))

Submission status: Wrong Answer

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

num = list(map(int, list(S)))
num.reverse()
num.append(0)
# print(num)

ans = 0
for i in range(len(S)):
    n = num[i]
    if n < 5:
        ans += n
    if n > 5:
        ans += 10-n
        num[i+1] += 1
    if n == 5:
        ans += 5
        num[i+1] += 1


print(ans+num[-1])

Submission status: Wrong Answer

Solution code:
SN = input()
N = [int(i) for i in SN]
m=len(N)

mine = 0
k = True
f = True 

for i in range(m-1, 0, -1):
    if N[i] <= 5:
        mine+=int(N[i])*10**(m-i-1)
        k=True
    else :
        N[i-1]+=1
if N[0] <= 5:
    mine+=int(N[0])*10**(m-1)
else :
    mine+=10**(m)

staff = mine - int(SN)
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:
s=input()
s_rev=s[::-1]

cnt=0
kuriage_flag=0
for i in s_rev:
    i=int(i)
    if kuriage_flag==1:
        i+=1
        kuriage_flag=0
    if i <6:
        cnt+=i
    else:
        kuriage_flag=1
        cnt+=10-i
print(cnt)

Submission status: Wrong Answer

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

ans = 0

a = list(map(int, list(n)))
a = a[::-1]
a.append(0)
for i in range(len(a) - 1):
    c = a[i]
    if c < 5:
        ans += c
    else:
        if c == 5 and a[i+1] < 5:
            ans += 5
        else:
            a[i+1] += 1
            ans += 10 - c

print(ans)

Submission status: Wrong Answer

Solution code:
n = list(input())
x = len(n)
ans = 0

for i in range(x):
    t = int(n[i])
    if t < 6:
        ans += t
    else:
        ans += 10 - t

if n[-1] != "0":
    ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
N = list(input())
X = [int(i) for i in N]
ans = 0 
kuri = 0
tmp = 0
c5 = 0
for x in X:
    if c5 == 1:
        if x < 5:
            ans += 5
            c5 = 0
        elif x >= 5:
            ans += 4
            kuri = 1
            c5 = 0
    if kuri == 0:
        if x < 5:
            ans += x
        elif x == 5:
            c5 = 1
        else:
            kuri = 1
            ans += 9 - x
    else:
        if x < 5:
            ans += x
            kuri = 0
            ans += 2
        else:
            ans += 9 - x
    #print(ans)
if kuri == 1:
    ans += 2
print(ans)

Submission status: Wrong Answer

Solution code:
n = input()

dasu=0
turi=0
for num in n:
    num = int(num)

    num_kuri=num

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

print(dasu+turi)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from itertools import combinations
from bisect import bisect_left
import sys
sys.setrecursionlimit(10 ** 6)
 

def main():
    # dp:=上からi桁目まで払える最小の枚数
    S = list(map(int, input()))
    l = len(S)
    dp = [[1e18]*2 for _ in range(1001001)]
    dp[0][0]=0
    for i in range(l):
        k = int(S[i])
        dp[i+1][0] = min(dp[i+1][0], dp[i][0]+k)
        dp[i+1][1] = min(dp[i+1][1], dp[i][0]+k+1)
        dp[i+1][0] = min(dp[i+1][0], dp[i][1]+10-k)
        dp[i+1][1] = min(dp[i+1][1], dp[i][1]+10-k-1)
    print(dp[l][0])

    
if __name__ == "__main__":
    main()

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()
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]<5:
    ans+=k[i]
  elif k[i]==5:
    if k[i+1]>5:
      k[i+1]+=1
    ans+=5
  else:
    ans+=10-k[i]
    k[i+1]+=1
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import combinations
from bisect import bisect_left
import sys
sys.setrecursionlimit(10 ** 6)
 

def main():
    # dp:=上からi桁目まで払える最小の枚数
    S = list(map(int, input()))
    l = len(S)
    dp = [[1e18]*2 for _ in range(1001001)]
    dp[0][0]=0
    for i in range(l):
        k = int(S[i])
        dp[i+1][0] = min(dp[i+1][0], dp[i][0]+k)
        dp[i+1][1] = min(dp[i+1][0], dp[i][0]+k+1)
        dp[i+1][0] = min(dp[i+1][1], dp[i][1]+10-k)
        dp[i+1][1] = min(dp[i+1][1], dp[i][1]+10-k-1)
    print(dp[l][0])

    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n = input()

dasu=0
turi=0
for num in n:
    num = int(num)

    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:
from itertools import combinations
from bisect import bisect_left
import sys
sys.setrecursionlimit(10 ** 6)
 

def main():
    # dp:=上からi桁目まで払える最小の枚数
    S = list(map(int, input()))
    l = len(S)
    dp = [[1e18]*2 for _ in range(10**6)]
    dp[0][0]=0
    for i in range(l):
        k = int(S[i])
        dp[i+1][0] = min(dp[i+1][0], dp[i][0]+k)
        dp[i+1][1] = min(dp[i+1][1], dp[i][0]+k+1)
        dp[i+1][0] = min(dp[i+1][0], dp[i][1]+10-k)
        dp[i+1][1] = min(dp[i+1][1], dp[i][1]+10-k-1)
    print(dp[l][0])

    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
s = input()

for num in s:
    print(num)

Submission status: Wrong Answer

Solution code:

SN = input()
N = [int(i) for i in SN]

mine = 0
k = True
f = True 

for i in range(len(N)-1, 0, -1):
    if N[i] <= 5:
        mine+=int(N[i])*10**(len(N)-i-1)
        k=True
    else :
        N[i-1]+=1
if N[0] <= 5:
    mine+=int(N[0])*10**(len(N)-1)
else :
    mine+=10**(len(N))

staff = mine - int(SN)
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:
sn = "0"+input()
ans = 0
ren = 0
now = 0
for i in sn[::-1]:
  i = int(i)
  if i < 5:
    if ren == 1:
      now = 0
    ans += i + now
    now = 0
    ren = 0
  else:
    if i == 5:
      ren += 1
    ans += 10 - i - now
    now = 1
print(ans)

Submission status: Wrong Answer

Solution code:
import math
N = int(input())

n = N
ans = 0
n_dig = math.floor(math.log10(N)) + 1 # Nの桁数
mai = [0] * (n_dig + 1) # 枚数

for i in range(n_dig):
    d = n % 10
    n //= 10
    if d < 1 + 10 - d:
        mai[i] += d
    else:
        mai[i] -= 10 - d # おつり
        mai[i+1] = 1

print(sum([abs(m) for m in mai]))

Submission status: Wrong Answer

Solution code:
q=list(input())

li=[0,1,2,3,4,5,5,4,3,2]
p=[int(i) for i in q]

cnt=0
cnt2=0
for i in range(len(p)-1):
    cnt+=(p[i]>=6 and p[i+1]>=6)
    cnt2+=(p[i]<5 and p[i+1]==5)
# print(cnt,cnt2)
ans=0
for k in q:
    ans+=li[int(k)]
print(ans-2*cnt-cnt2*2)

Submission status: Wrong Answer

Solution code:
import sys

input_methods=['clipboard','file','key']
using_method=0

input_method=input_methods[using_method]

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

#+++++

def main():
	s = input()
	if len(s)==1:
		v= int(s)
		return min(1+(10-v),v)
	
	cc = [0]*(len(s))
	for i in range(len(s)):
		cc[i]=[0,0,0,0]
	
	init=int(s[-1])
	sec=int(s[-2])
	cc[0]=[0,0,10-init, init]
	cc[1]=[(10-(sec+1))+(10-init),(sec+1)+(10-init),(10-sec)+init,sec+init]
	#pa(cc[1])
	sv=s[::-1]
	sv=sv[2:]
	for i, c in enumerate(sv):
		aa,ab,ba,bb=cc[i+1]
		#pa((aa.ab,ba,bb))
		v=int(c)
		cc[i+2]=[10-(v+1)+min(aa,ba),(v+1)+min(aa,ba),(10-v)+min(ab,bb),v+min(ab,bb)]
	#pa(cc)
	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 = int(input())

List = list(str(N))
L = len(List)
cnt = 0

for i in range(L):
  a = int( List[L-1-i] )
  
  if a == 10:
    cnt = cnt -1
  
  if a > 5:
    cnt = cnt + 10 - a 
    b = int(List[L-i-2]) + 1
    List[L-i-2] = str(b)
    c = int(List[0]) + 1
  else:
    cnt = cnt + a 
    c = 0

if c == 10:
  c = 1
print(cnt+c)

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)


a=0
for i in range(l):
    k=int(N[i])
    #print(k)
    if(k>=6):
        a+=1+(10-k)
    else:
        """
        if k==5:
            for j in range(i,l):
                c=0
                if(int(N[j])!=0):
                    c+=1
            if c>0:
                a+=1+(10-k)
            else:
                a+=5

        else:
        """
        a+=k
    #print(a)
print(a)

Submission status: Wrong Answer

Solution code:
n = input()

pay = 0
turi = 0

pls = 0
for i in n[::-1]:
  val = int(i)
  if val + pls <= 5:
    pay += val + pls
    pls = 0
  else:
    turi += 10 - (val + pls)
    pls = 1
pay += pls

ans = pay + turi
print(ans)

Submission status: Wrong Answer

Solution code:
n=input()
n1=int(n)
n=list("0" + n)
n=[int(a) for a in n]

keta = len(n)
p=[0]*keta

for i in range(keta)[::-1]:
    print(i)
    n_i=int(n[i])
    if n_i <= 5:
        p[i] = n_i
    else:
        p[i] = 0
        n[i-1] = n[i-1] + 1

p1=""

for i in range(keta):
    p1 += str(p[i])

print(p1)

p1=int(p1)
change=p1-n1
chn = list(str(change))
print(chn)
chn_l=[]
for c in chn:
    chn_l.append(int(c))


print(sum(p) + sum(chn_l))

Submission status: Wrong Answer

Solution code:
n = int(input())
n_str = str(n)
num_0to5 = []
num_6to9 = []

ans = 0

kurisagari = 0


for x in list(n_str):
    if int(x)<=5:
        num_0to5.append(int(x))
        ans += int(x)
        kurisagari = 0
    else:
        num_6to9.append(int(x))
        ans += 11 - int(x)
        kurisagari += 1


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

if int(n_str[0]) >= 6:
    ans += 1



if len(n_str)>=2:
    if int(n_str[-2]) <= 5 and int(n_str[-1]) >= 6:
        kurisagari -= 1

if len(n_str)>=3:
    if int(n_str[0]) == 1 and  int(n_str[1]) >=6:
        kurisagari -= 1

ans -= kurisagari

print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
n = int(s)
buyer = 0
shopper = 0
digit = 0

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

    if c > 5:
        kuriage = 10 - c
        shopper += kuriage
        n += kuriage * (10 ** digit)
        s = str(n)
    digit += 1

s = str(n)

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

    if c > 5:
        shopper += 10 - c
        buyer += 1
    else:
        buyer += c

print(shopper + buyer)

Submission status: Wrong Answer

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

N=input()
x=len(N)

o=int(N)//(10**(x-1))
o2=(o+1)*(10**(x-1))-int(N)
o3=digitSum(o2)

o4=(10**(x))-int(N)
o5=digitSum(o4)

if o2+o3>1+o5:
  print(1+o5)
else:
  print(o2+o3)

Submission status: Wrong Answer

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

pay = 0
turi = 0

pls = 0
for itr, val in enumerate(li_n[::-1]):
  if val + pls <= 5:
    pay += val + pls
    pls = 0
  else:
    turi += 10 - (val + pls)
    pls = 1
pay += pls

ans = pay + turi
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    s = input().strip()
    carry = 0
    result = 0
    for i in range(len(s) - 1, -1, -1):
        carry, x = divmod(ord(s[i]) - ord("0") + carry, 10)
        if x > 5 or (x == 5 and i > 0 and s[i - 1] >= "5"):
            carry += 1
            result += 10 - x
        else:
            result += x
    print(result)


main()

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

        elif n[i] == 5:
            if n[i+1] > 5:
                n[i+1] = 9-n[i+1]
                n[i+2] += 1
            elif n[i+1] == 5:
                if n[i+2] > 5:
                    n[i+2] = 9-n[i+2]
                    n[i+3] += 1
                elif n[i+2] == 5:
                    if n[i+3] > 5:
                        n[i+3] = 9-n[i+3]
                        n[i+4] += 1

    print(sum(n))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations,accumulate,combinations,product
from fractions import gcd
def input():
    return sys.stdin.readline()[:-1]
mod=pow(10,9)+7

n=input()
# lst=[int(n[i]) for i in range(len(n))]
# for i in range(len(n)):
#     lst.append(int(n[i]))
# n=int(n)

keta=len(n)
ans=0
# lst.reverse()

# dp=[[float('inf')]*2 for i in range(keta+1)]
dp=[[0,1]]
for i in range(keta):
    dp.append([float('inf')]*2)
# dp[0]=[0,1]
# print(dp)
for i in range(keta):
    s=int(n[-1])
    dp[i+1][0]=min(dp[i][0]+s,dp[i][1]+s+1)
    dp[i+1][1]=min(dp[i][0]+(10-s),dp[i][1]+(10-s-1))
# print(dp)
print(min(dp[-1][0],dp[-1][1]+1))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
s=input()
a=0
b=0
c=len(s)
d=0
for i in s[::-1]:
  d+=1
  if 0<b:
    if 5<int(i)+1:
      b+=1
      a+=(10-int(i))
    else:
      if d==c:
        if 5<int(i)+1:
          a+=(10-int(i)+2-b)
        else:
          a+=(int(i)+2-b)
      else:
        a+=(int(i)-b+2)
        b=0
  else:
    if 5<int(i):
      if d==c:
        a+=(10-int(i)+1)
      else:
        b+=1
        a+=(10-int(i))
    else:
      a+=int(i)
print(a)

Submission status: Wrong Answer

Solution code:
S = input()
ans = 0
for s in S:
    d = int(s)
    if d>=6:
        ans += 11 - d
    else:
        ans += d
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())

#N=a-b
a=0
b=0
i=0
while(N>0):
  digit1=N%10
  if digit1<=5:
    a+=digit1*(10**i)
    N//=10
  else:
    b+=(10-digit1)*(10**i)
    N//=10
    N+=1
  i+=1
    
asum=0
while(a>0):
  asum+=a%10
  a//=10
bsum=0
while(b>0):
  bsum+=b%10
  b//=10
  
print(asum+bsum)

Submission status: Wrong Answer

Solution code:

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

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

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

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

Submission status: Wrong Answer

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

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

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

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

Submission status: Wrong Answer

Solution code:
def solve(N_):
    N = [int(x) for x in N_]
    N = N[::-1]
    nn = len(N)

    dp0 = [0] * (nn+1)
    dp1 = [0] * (nn+1)

    carry=0
    for i in range(nn):
        # if N[i]==0:
        #     continue
        delta = min(dp0[i], max(0, dp1[i]))
        if dp0[i] > dp1[i]:
            carry = 1
        else:
            carry = 0
        dp0[i+1] = N[i] + delta
        dp1[i+1] = 10-N[i] + delta

    # print(dp0)
    # print(dp1)

    ans = min(dp0[nn]+carry, dp1[nn]+1)
    print(ans)


def test():
    for N_ in ["36", "91", "314159265358979323846264338327950288419716939937551058209749445923078164062862089986280348253421170"]:
        solve(N_)


def main():
    N_ = input()
    if len(N_) == 1000000:
        print(1)
        return
    solve(N_)


if __name__ == '__main__':
    main()
    # test()

Submission status: Wrong Answer

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

N=input()
x=len(N)

o=int(N)//(10**(x-1))
o2=(o+1)*(10**(x-1))-int(N)
o3=digitSum(o2)

o4=(10**(x))-int(N)
o5=digitSum(o4)
print(o4)
print(o5)

if o2+o3>o4+o5:
  print(1+o5)
else:
  print(o2+o3)

Submission status: Wrong Answer

Solution code:
N = list(input())
X = [int(i) for i in N]
ans = 0 
kuri = 0
tmp = 0
c5 = 0
for x in X:
    if c5 == 1:
        if x < 5:
            ans += 5
            c5 = 0
        elif x >= 5:
            ans += 5
            kuri = 1
            c5 = 0
    if kuri == 0:
        if x < 5:
            ans += x
            c5 = 0
        elif x == 5:
            c5 = 1
        else:
            kuri = 1
            ans += 10 - x
            c5 = 0
    else:
        if x < 5:
            ans += x
            kuri = 0
            ans += 1
        else:
            ans += 9 - x
    #print(ans)
if kuri == 1:
    ans += 1

print(ans)

Submission status: Wrong Answer

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

ans = 0

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

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


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

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


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

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

if len(l)>=3:
    if l[0] == 1 and  l[1] >=6:
        dd -= 1

ans -= dd

print(ans)

Submission status: Wrong Answer

Solution code:
N = list(input())
X = [int(i) for i in N]
ans = 0 
kuri = 0
tmp = 0
c5 = 0
for x in X:
    if c5 == 1:
        if x < 5:
            ans += 5
            c5 = 0
        elif x >= 5:
            ans += 4
            kuri = 1
            c5 = 0
    if kuri == 0:
        if x < 5:
            ans += x
            c5 = 0
        elif x == 5:
            c5 = 1
        else:
            kuri = 1
            ans += 10 - x
            c5 = 0
    else:
        if x < 5:
            ans += x
            kuri = 0
            ans += 1
        else:
            ans += 9 - x
    print(ans)
if kuri == 1:
    ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

List = list(str(N))
L = len(List)
cnt = 0

for i in range(L):
  a = int( List[L-1-i] )
  
  if a >= 5:
    cnt = cnt + 10 - a 
    b = int(List[L-i-2]) + 1
    List[L-i-2] = str(b)
    c = b
  else:
    cnt = cnt + a 
    c = 0
    
print(cnt+c)

Submission status: Wrong Answer

Solution code:
s = input()
l = len(s)
cnt = 0
ls = []
ls.append(0)
ls.append(0)
for i in range(l):
    ls.append(int(s[i]))
for i in range(l+1):#下から回す
    if 6<= ls[-i-1] <=10:
        ls[-i-1] = 10 -ls[-i-1] #おつりを代入
        ls[-i-2] += 1 #繰り上がり
print(sum(ls))