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: Runtime Error

Solution code:
s = [0] + list(map(int, input()))

def f(i):
    if s[i] == 9:
        s[i] = 0
        f(i-1)
    else:
        s[i] = s[i] + 1
    return

r = 0
i = len(s)
while i != 0:
    i -= 1
    if s[i] > 5:
        r += 10-s[i]
        s[i] = 0
        f(i-1)
    if s[i] == 5:
        if s[i-1] >= 5:
            r += 10-s[i]
            s[i] = 0
            f(i-1)
print(r+sum(s))

Submission status: Runtime Error

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

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

print(cnt)

Submission status: Runtime Error

Solution code:
N = int(input())
S = list(map(int,list(str(N))))
if len(S)==1:
  if N<=5:
    print(N)
    exit()
  else:
    print(10-N+1)
    exit()
ans = [0]*(len(S)+1)
for i in range(len(S)-1):
  s_1 = int(S[i])
  s_2 = int(S[i+1])
  if (s_1>=6)or((s_1==5)and(s_2>=6)):
    if s_2<=4:
      ans[i]=1
      ans[i+2]=s_2
    else:
      ans[i]=1
  else:
    if s_1==5:
      ans[i+1]=5
      ans[i+2]=s_2
    else:
      if s_2<=5:
        ans[i+1]=s_1
        ans[i+2]=s_2
      else:
        ans[i+1]=s_1+1

a = sum(ans)
ans = int(''.join(map(str,ans)))
ans = ans-N
a += sum(map(int,list(str(ans))))
print(a)

Submission status: Runtime Error

Solution code:

n = int(input())

def F(n):
    assert n >= 0
    if n <= 0:
        if n <= 5:
            return n
        return 10 - n
    
    res = 0
    q, r = divmod(n, 10)
    if r > 5:
        res += 10 - r + F(q + 1)
    elif r <= 5:
        res += r + F(q)
    return res

print(F(n))

Submission status: Runtime Error

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

lst=[int(n[i]) for i in range(len(n))]
n=int(n)

keta=len(lst)
ans=0
lst.reverse()
# print(lst)
dp=[[float('inf')]*2 for i in range(keta+1)]
dp[0]=[0,1]

for i in range(keta):
    dp[i+1][0]=min(dp[i][0]+lst[i],dp[i][1]+lst[i]+1)
    dp[i+1][1]=min(dp[i][0]+(10-lst[i]),dp[i][1]+(10-lst[i]-1))
# print(dp)
print(min(dp[-1][0],dp[-1][1]+1))

Submission status: Runtime Error

Solution code:
N=list(map(int,input()))
N.reverse()
N.append(0)
n=len(N)
pay=[0]*n
price = N.copy()

for i in range (n):
    if N[i]>5:
        pay[i]=0
        N[i+1]=N[i+1]+1
    else:
        pay[i]=N[i]


#print(price)
#print(pay)
sum_pay=sum(pay)

c=0
for i in range (n):
    if pay[i]>=price[i]:
        c=c+pay[i]-price[i]
    else:
        c=c+10+pay[i]-price[i]
        if  pay[i+1]!=0:
            pay[i+1]=pay[i+1]-1
        else:
            pay[i+1]=9
            pay[i+1]=pay[i+1]-1

sum_otsuri=c

#print(sum_otsuri)
#print(sum_pay)

sum=sum_otsuri+sum_pay
print(sum)

Submission status: Runtime Error

Solution code:
N = int(input())

strN = str(N)

lenN = len(strN)

harau = {}
keta = lenN
for s in strN:
    if int(s) >= 6:
        if keta+1 in harau: harau[keta+1] += 1
        else: harau[keta+1] = 1
        if keta in harau: pass
        else: harau[keta] = 0
    else:
        harau[keta] = int(s)
    keta -= 1

h_s = ''

ans = 0
for k, v in harau.items():
    h_s += str(v)
    ans += v

h = int(h_s)


otsuri = -(N-h)
strOtsuri = str(otsuri)
for s in strOtsuri:
    ans += int(s)


print(ans)

Submission status: Runtime Error

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

lst=[int(n[i]) for i in range(n)]
n=int(n)

keta=len(lst)
ans=0
lst.reverse()
# print(lst)
dp=[[float('inf')]*2 for i in range(keta+1)]
dp[0]=[0,1]

for i in range(keta):
    dp[i+1][0]=min(dp[i][0]+lst[i],dp[i][1]+lst[i]+1)
    dp[i+1][1]=min(dp[i][0]+(10-lst[i]),dp[i][1]+(10-lst[i]-1))
# print(dp)
print(min(dp[-1][0],dp[-1][1]+1))

Submission status: Runtime Error

Solution code:
s = [0] + list(map(int, input()))

def f(i):
    if s[i] == 9:
        s[i] = 0
        f(i-1)
    else:
        s[i] = s[i] + 1
    return

r = 0
i = len(s)
while i != 0:
    i -= 1
    if s[i] > 5 or (s[i] == 5 and s[i-1] >= 5):
        r += 10-s[i]
        s[i] = 0
        f(i-1)
print(r+sum(s))

Submission status: Runtime Error

Solution code:
n = int(input())
s = str(n)

bill = 102 + 1

d = [[0]*2 for _ in range(bill)]
i = 0
while i < len(s):
    d[i][0] = int(s[-i-1])
    d[i][1] = 10 - int(s[-i-1])
    i += 1

ans = 0
i  =  0
while d[i] != [0, 0]:
    ans += min(d[i])
    i += 1

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

Submission status: Runtime Error

Solution code:
S = input()
cnt = 0
if len(S)==0:
    print(min(10-int(S[0]), int(S[0])))
elif int(S[-2])!=9:
#cnt = min(int(S[-1])+int(S[-2]), ((int(S[-2])+1)%10+10-int(S[-1])))
    cnt = min(1+(9-int(S[-2]))+(10-int(S[-1])%10), 1+(10-int(S[-2])%10), int(S[-2])+1+(10-int(S[-1])%10), 1+int(S[-2])+1+int(S[-1]), int(S[-2])+int(S[-1]))
else:
    cnt = min(1+(10-int(S[-1])), 1+1+(10-int(S[-2])%10), int(S[-2])+int(S[-1]))
#print(cnt)
#cnt = 4

for i in range(len(S)-2)[::-1]:
    cnt += min(10-int(S[i]), int(S[i]))


print(cnt)

Submission status: Runtime Error

Solution code:
n = list(str(input()))
lis = []
n.insert(0,"#")
s = ""
  #print(n)
ans = 0
for i in range(len(n)-1):
    if int(n[-(i+1)]) > 5 or (int(n[-(i+1)]) == 5 and (s != "" or int(n[-(i+2)]) >= 5)):
        s = n[-(i+1)]+s
    else:
        s = n[-(i+1)]+s
        if len(s) == 1:
            ans += min(int(s),11-int(s))
        else:
            num1 = str(10 ** len(s)-int(s))
            num2 = str((int(s[0])+1) * 10 ** (len(s)-1)-int(s))
            ans += min(1+sum([int(num1[i]) for i in range(len(num1))]),1+int(s[0])+sum([int(num2[i]) for i in range(len(num2))]))
          #print(s,ans)
        s = ""
if s != "":
    if len(s) == 1:
        ans += min(int(s), 11 - int(s))
    else:
        num1 = str(10 ** len(s) - int(s))
        num2 = str((int(s[0]) + 1) * 10 ** (len(s) - 1) - int(s))
        ans += min(1 + sum([int(num1[i]) for i in range(len(num1))]),1 + int(s[0]) + sum([int(num2[i]) for i in range(len(num2))]))
      #print(s,ans)
print(ans)

Submission status: Runtime Error

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

lst=[n[i] for i in range(n)]
n=int(n)

keta=len(lst)
ans=0
lst.reverse()
# print(lst)
dp=[[float('inf')]*2 for i in range(keta+1)]
dp[0]=[0,1]

for i in range(keta):
    dp[i+1][0]=min(dp[i][0]+lst[i],dp[i][1]+lst[i]+1)
    dp[i+1][1]=min(dp[i][0]+(10-lst[i]),dp[i][1]+(10-lst[i]-1))
# print(dp)
print(min(dp[-1][0],dp[-1][1]+1))

Submission status: Runtime Error

Solution code:
N = input()
count = 0
prev = 0
for i in range(len(N)):
  if 0 <= int(N[i]) + prev < 6:
    count += int(N[i]) - prev
    prev = 0
  elif: 6 <= int(N[i]) + prev <= 9:
    count += 11 - (int(N[i]) - prev)
    prev = 1
  else:
    prev = 1
print(count)

Submission status: Runtime Error

Solution code:
S = input()
cnt = 0
if len(S)==0:
    print(min(10-int(S[0]), int(S[0])))
elif int(S[-2])!=9:
#cnt = min(int(S[-1])+int(S[-2]), ((int(S[-2])+1)%10+10-int(S[-1])))
    cnt = min(1+(9-int(S[-2]))+(10-int(S[-1])%10), 1+(10-int(S[-2])%10), int(S[-2])+1+(10-int(S[-1])%10), 1+int(S[-2])+1+int(S[-1]), int(S[-2])+int(S[-1]))
else:
    cnt = min(1+(10-int(S[-1])), 1+1+(10-int(S[-2])%10), int(S[-2])+int(S[-1]))
#print(cnt)
#cnt = 4
print(cnt)

for i in range(len(S)-2)[::-1]:
    cnt += min(10-int(S[i]), int(S[i]))

print(cnt)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n = list(str(input()))
lis = []
n.insert(0,"#")
s = ""
print(n)
ans = 0
for i in range(len(n)-1):
    if int(n[-(i+1)]) > 5 or (int(n[-(i+1)]) == 5 and (s != "" or int(n[-(i+2)]) >= 5)):
        s = n[-(i+1)]+s
    else:
        s = n[-(i+1)]+s
        if len(s) == 1:
            ans += min(int(s),11-int(s))
        else:
            num1 = str(10 ** len(s)-int(s))
            num2 = str((int(s[0])+1) * 10 ** (len(s)-1)-int(s))
            ans += min(1+sum([int(num1[i]) for i in range(len(num1))]),1+int(s[0])+sum([int(num2[i]) for i in range(len(num2))]))
          #print(s,ans)
        s = ""
if s != "":
    if len(s) == 1:
        ans += min(int(s), 11 - int(s))
    else:
        num1 = str(10 ** len(s) - int(s))
        num2 = str((int(s[0]) + 1) * 10 ** (len(s) - 1) - int(s))
        ans += min(1 + sum([int(num1[i]) for i in range(len(num1))]),1 + int(s[0]) + sum([int(num2[i]) for i in range(len(num2))]))
      #print(s,ans)
print(ans)

Submission status: Runtime Error

Solution code:
n = input()
n = int(n)
pay = [0 for _ in range(101)]
ret = [0 for __ in range(101)]

i = 0
while n > 0:
    num = n%10
    if num <6:
        pay[i] += num
    else:
        if i == 100:
            pay[i]+=num
        else:
            n = n +10
            ret[i] +=10-num
    i = i+1
    n = n//10
sums =0
#print(pay)
#print(ret)
for j in range(101):
    temp = min(pay[j],ret[j])
    if temp>0:
        pay[j] -= temp
        ret[j] -= temp
    sums += pay[j] + ret[j]
print(sums)

Submission status: Runtime Error

Solution code:
n = input()

dp = [[0] * 2 for _ in range(100000)]

digit = int(n[-1])

dp[0][0] = 10 - digit
dp[0][1] = digit

for i in range(1, len(n) + 1):
    if i == len(n):
        digit = 0
    else:
        digit = int(n[-i-1])
    dp[i][0] = min(dp[i-1][0] + 10 - digit - 1, dp[i-1][1] + 10 - digit)
    dp[i][1] = min(dp[i-1][0] + digit + 1, dp[i-1][1] + digit)


print(min(dp[len(n)][1], dp[len(n)-1][1]))

Submission status: Runtime Error

Solution code:
N = input()
c = 0
for i in range(len(N)):
    s = N[i]
    i = int(s)
    if i <= 5:
        c += i
    else:
        c += 1 + (10 - i)
        if i > 0:
            if N[i-1] > '5':
                c -= 2
print (c)

Submission status: Runtime Error

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 = inputInt()
	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: Runtime Error

Solution code:
 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) == 5) 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: Runtime Error

Solution code:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<string>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>

#define REP(i,n) for(int i=0;i<(n);i++)
#define EPS (1e-7)
#define INF (3e9)

using namespace std;
typedef long long int ll;

string S;
int main(void){
    cin >> S;

    bool kuriage = false;
    ll count = 0;
    for (int i = S.length()-1; i >= 0;i--){
        int current_num = S[i] - '0';

        if(kuriage) current_num++;

        if(current_num <= 5){
            count += current_num;
            kuriage = false;
        }else{
            count += 10 - current_num;
            kuriage = true;
        }
        // cout << S[i] - '0' << " " << count << endl;
    }
    if (kuriage) count++;

    cout << count << endl;
    return 0;
}

Submission status: Runtime Error

Solution code:
n = input()

dp = [[0] * 2 for _ in range(101)]

digit = int(n[-1])

dp[0][0] = 10 - digit
dp[0][1] = digit

for i in range(1, len(n) + 1):
    if i == len(n):
        digit = 0
    else:
        digit = int(n[-i-1])
    dp[i][0] = min(dp[i-1][0] + 10 - digit - 1, dp[i-1][1] + 10 - digit)
    dp[i][1] = min(dp[i-1][0] + digit + 1, dp[i-1][1] + digit)


print(min(dp[len(n)][1], dp[len(n)-1][1]))

Submission status: Runtime Error

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 l[-2] <= 5 and l[-1] >= 6:
    dd -= 1

ans -= dd

print(ans)

Submission status: Runtime Error

Solution code:
n=list(map(int,input()))
#print(n)
#print(len(n))
ans=0
for i in range(len(n)-1,-1,-1):
    #print(n[i])
    if n[i]<=5:
        ans+=n[i]
    else:
        if i==0:
            #print("a")
            ans+=1
        ans+=10-n[i]#帰ってくる枚数
        j=1
        while n[i-j]==9 and (i-j)!=0:
            #print("sdfghj")
            n[i-j]=0
            j+=1
        n[i-j]=n[i-j]+1
    #print(n[i],ans)
print(ans)

Submission status: Runtime Error

Solution code:
 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: Runtime Error

Solution code:
N = input()
c = 0
for i in range(len(N)):
    s = N[i]
    i = int(s)
    if i <= 5:
        c += i
    else:
        c += 1 + (10 - i)
        if i > 0 and N[i-1] > '5':
            c -= 2
print (c)

Submission status: Runtime Error

Solution code:
import math
import numpy as np
n=int(input())
count=0
while n!=0:
    m=n%10
    if m>=6:
        n=(n-m+10)/10
        count+=10-m
    else:
        n=(n-m)/10
        count+=m
    n=int(n)
    print(n)

print(count)

Submission status: Runtime Error

Solution code:
N = input()
n = int(N)

c = 0
for i in range(len(N)):
    s = N[i]
    i = int(s)
    if i <= 5:
        c += i
    else:
        c += 1 + (10 - i)
        if i > 0 and N[i-1] > '5':
            c -= 2
print (c)

Submission status: Runtime Error

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: Runtime Error

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]
        if index4[i] == 0:
            f = False
        if index4[i] == 2:
            f = True
    #print(index4)
    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 (i<N-1 and int(S[i]+S[i+1])>=56) or int(S[i])>=6:
            flag = True
            ans += 11-int(S[i])
        else:
            ans += int(S[i])
    print(ans)
    return

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

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

if __name__ == '__main__':
    examE()

"""
4396817
"""

Submission status: Runtime Error

Solution code:
n = input()
A = ["0"]
t = 0
for i in n:
    A.append(i)
i = 0
for _ in range(len(A)-1):
    if i == len(A):
        break
    if int(A[i]) < 5:
        t += int(A[i])
    elif int(A[i]) < 9:
        t += (10 - int(A[i])) + 1
    else:
        num = int(A[i] + A[i+1])
        if num <= 94:
            t += num - 90 + 2
        else:
            t += 100 - num + 1
        t -= min(10 - int(A[i-1])+1, int(A[i-1]))
        i += 1
    i += 1


print(t)

Submission status: Runtime Error

Solution code:
def main():
    N = list(input()[::-1])
    if len(N) > 101:
        tmp = N[:101]
        over = N[101:]
        for i in range(len(over)):
            tmp[100] += over[i]*10**(i+1)
        tmp = list(map(int,tmp))
    else:
        tmp = list(map(int,N))
    ans = 0
    for i in range(len(tmp)):
        ans += min(tmp[i],10-tmp[i])
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from functools import lru_cache
N=int(input())

@lru_cache()
def _pay(N):
    if N<5:
        return N
    n=N%10
    if n==5:
        return min(n+pay(N//10),10-n+pay(1+N//10))
    elif n<5:
        return n+pay(N//10)
    else:
        return 10-n+pay(1+N//10)

@lru_cache()
def pay(N):
    if N<=5:
        return N
    elif N<=10:
        return 11-N
    retval=0
    while(N%10!=5):
        n=N%10
        if n<5:
            retval+=n
            N=N//10
        else:
            retval+=10-n
            N=1+N//10
        if N<100:
            return retval+_pay(N)
    return retval+5+min(pay(N//10),pay(1+N//10))

print(pay(N))

Submission status: Runtime Error

Solution code:
n = input()
l = len(n)
n = int(n)
ans = 0


def c(l, n, ans):

    a = n // (10 ** (l - 1))
    if l == 1:
        if a <= 5:
            return ans + a
        else:
            return ans + 1 + (10 - a)
    elif a == 5 and l > 1:
        if (n // (10 ** (l - 2))) % 10 <= 4:
            return c(l - 1, n % (10 ** (l - 1)), ans + a)
        else:
            return c(l, 10 ** l - n, ans + 1)
    elif a <= 4 and l > 1:
        return c(l - 1, n % (10 ** (l - 1)), ans + a)
    else:
        return c(l, 10 ** l - n, ans + 1)


print(c(l, n, ans))

Submission status: Runtime Error

Solution code:
n = input()
l = len(n)
n = int(n)
ans = 0


def c(l, n, ans):
    print(ans)
    a = n // (10 ** (l - 1))
    if l == 1:
        if a <= 5:
            return ans + a
        else:
            return ans + 1 + (10 - a)
    elif a == 5 and l > 1:
        if (n // (10 ** (l - 2))) % 10 <= 4:
            return c(l - 1, n % (10 ** (l - 1)), ans + a)
        else:
            return c(l, 10 ** l - n, ans + 1)
    elif a <= 4 and l > 1:
        return c(l - 1, n % (10 ** (l - 1)), ans + a)
    else:
        return c(l, 10 ** l - n, ans + 1)


print(c(l, n, ans))

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using PII = std::pair<int, int>;
using PLL = std::pair<ll, ll>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)


const int mod = 1e9 + 7;

int main()
{

#ifdef DEBUG
    cout << "DEBUG MODE" << endl;
    ifstream in("input.txt"); //for debug
    cin.rdbuf(in.rdbuf());    //for debug
#endif

    string n;
    ll ans = 0;
    cin >> n;
    vector<int> v;

    int a[10] = {0,
                 1,
                 2,
                 3,
                 4,
                 5,
                 5,
                 4,
                 3,
                 2};

    rep(i, n.length())
    {
        v.push_back(n[i] - '0');
    }

    reverse(v.begin(), v.end());
    v.push_back(0);

    ans = 0;

    rep(i, v.size())
    {
        int j = 0;

        while (v[i + j] > 5 || (v[i + j] == 5 && v[i + j + 1] > 0))
        {

            ans += 10 - v[i + j];
            v[i + j] = 0;
            j++;
            v[i + j]++;
        }


        ans += v[i];

    }

    cout << ans << endl;
    return 0;
}

Submission status: Runtime Error

Solution code:
n = input()
A = ["0"]
t = 0
for i in n:
    A.append(i)
i = 0
for _ in range(len(A)):
    if i == len(A):
        break
    if int(A[i]) < 5:
        t += int(A[i])
    elif int(A[i]) < 9:
        t += (10 - int(A[i])) + 1
    else:
        num = int(A[i] + A[i+1])
        if num <= 94:
            t += num - 90 + 2
        else:
            t += 100 - num + 1
        t -= min(10 - int(A[i-1])+1, int(A[i-1]))
        i += 1
    i += 1


print(t)

Submission status: Runtime Error

Solution code:
from sys import setrecursionlimit as srl; srl(10**7)
rr = raw_input
rrm = lambda: map(int, raw_input().split())

def memo(f):
    class memodict(dict):
        def __init__(self, f):
            self.f = f
        def __call__(self, *args):
            return self[args]
        def __missing__(self, key):
            ret = self[key] = self.f(*key)
            return ret
    return memodict(f)


def solve(A):
    A = A[::-1]
    N = len(A)
    @memo
    def dp(i, last):
        if i == N:
            return last
        v = A[i+1] if i+1 < N else 0
        v2 = (A[i+1] +1 )if i+1 < N else 0
        ans = dp(i+1, v) + last
        if last <= 10:
            ans = min(ans, 10 - last + dp(i+1, v2))
        else:
            ans = min(ans, last - 10 + dp(i+1, v2))
        return ans
    return dp(0, A[0])

s = rr()
print solve(map(int, s))

Submission status: Runtime Error

Solution code:
S = input()
ans = 0
for s in S:
    i = int(s)
    ans += min(i, 10-i)

s0, s1 = int(S[0]), int(S[1])
if s1 >= 6:
    ans += 1
elif s1 <=4 and s0 >= 5:
    ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
import os
import sys
input = sys.stdin.buffer.readline
S = input().rstrip().decode()
ans = 0
for s in S:
    i = int(s)
    ans += min(i, 10-i)

s0, s1 = int(S[0]), int(S[1])
if s1 >= 6:
    ans += 1
elif s1 <=4 and s0 >= 5:
    ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
n = input()

a = n[::-1]

prev_a = int(a[0])
prev_b = 10 - int(a[0])

# dp[下の桁から][繰り上げるかどうか]
for i in range(1, len(a)):
    x = int(a[i])
    y = x + 1
    aa = min([x + prev_a, y + prev_b])
    bb = min([(10 - x) + prev_a, (10 - y) + prev_b])
    prev_a = aa
    prev_b = bb

print(min(aa, bb))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
from functools import lru_cache
N=int(input())

@lru_cache()
def pay(N):
    if N<5:
        return N
    n=N%10
    if n==5:
        return min(n+pay(N//10),10-n+pay(1+N//10))
    elif n<5:
        return n+pay(N//10)
    else:
        return 10-n+pay(1+N//10)

print(pay(N))

Submission status: Runtime Error

Solution code:
N = int(input())

dp = {}
def recurse(n):
    if n in dp:
        return dp[n]
    if n < 10:
        return min(n, 1 + 10 - n)

    ans = float('inf')
    ans = min(ans, n % 10 + recurse(n // 10))
    ans = min(ans, 10 - (n % 10) + recurse(n // 10 + 1))

    dp[n] = ans
    return ans
 
print(recurse(N))

Submission status: Runtime Error

Solution code:
from functools import lru_cache
N=int(input())

@lru_cache()
def pay(N):
    if N<5:
        return N
    n=N%10
    return min(n+pay(N//10),10-n+pay(1+N//10))

print(pay(N))

Submission status: Runtime Error

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

N = input()

tmp = 0
for i in range(len(N)):
    num = int(N[i])
    if num < 5:
        tmp += num
    else:
        tmp += 10 - num

if int(N[0]) > 5 or int(N[1]) > 5:
    tmp += 1

print(tmp)

Submission status: Runtime Error

Solution code:
from itertools import combinations
n, k = map(int, input().split())
*A, = map(int, input().split())
print(sorted(x*y for x, y in combinations(A, 2))[k])

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import sys
from functools import lru_cache
sys.setrecursionlimit(10 ** 9)
def input(): return sys.stdin.readline().rstrip()

def main():
    n = input()
    n_len = len(n)
    
    def min(m, n):
        if m > n:
            return n
        else:
            return m

    @lru_cache(maxsize=None)
    def dfs(i, isPlus):
        if i == n_len:
            return 0
        v = int(n[i])
        if isPlus:
            v = 10 - v
        return min(v + 1 + dfs(i+1, True), v + dfs(i+1, False))

    print(min(dfs(0, False), 1 + dfs(0, True)))

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

Submission status: Runtime Error

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