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:
s=input()
a=len(s)
b=0
ans=0
i=0
c=0
d=0
e=0
f=0
while i<=a-1:
  if int(s[i])<=4:
    if e==1:
      ans=ans+int(s[i])+c
    elif e+d>=1:
      ans=ans+int(s[i])+c-d+2
    else:
      ans=ans+int(s[i])
    c=0
    d=0
    e=0
    f=0
    i=i+1
  elif d==0 and s[i]=="5":
    while i<=a-1 and s[i]=="5":
      if s[i]=="5":
        c=c+5
        d=d+1
        e=e+1
        i=i+1
      
  else:
    c=c+10-int(s[i])
    d=d+1
    i=i+1
    e=0
if d==0 :
  ans=ans
elif e==1:
  ans=ans+5
elif d==1:
  ans=ans+10-int(s[-1])+1
else:
  ans=ans+c-d+2
g=0
for i in range(a):
  if a!=0 and a!=a-1 and s[i]=="4":
    if int(s[i-1])>=5 and int(s[i+1])>=6:
      g=g+1
print(ans-g)

Submission status: Wrong Answer

Solution code:
s=input()
a=len(s)
b=0
ans=0
i=0
c=0
d=0
e=0
f=0
while i<=a-1:
  if int(s[i])<=4:
    if e==1:
      ans=ans+int(s[i])+c
    elif e+d>=1:
      ans=ans+int(s[i])+c-d+2
    else:
      ans=ans+int(s[i])
    c=0
    d=0
    e=0
    f=0
    i=i+1
  elif d==0 and s[i]=="5":
    while i<=a-1 and s[i]=="5":
      if s[i]=="5":
        c=c+5
        d=d+1
        e=e+1
        i=i+1
      
  else:
    c=c+10-int(s[i])
    d=d+1
    i=i+1
    e=0
if d==0 :
  ans=ans
elif e==1:
  ans=ans+5
elif d==1:
  ans=ans+10-int(s[-1])+1
else:
  ans=ans+c-d+2
g=0
for i in range(a):
  if a!=0 and a!=a-1 and s[i]=="4":
    if int(s[i-1])>=5 and int(s[i+1])>=5:
      g=g+1
print(ans-g)

Submission status: Wrong Answer

Solution code:
n=int(input())

ans=0
while True:
    num=n%10
    if num<=5:  #お釣りがないように払う
        ans+=num
    else:
        ans+=1  #一つ上の桁の紙幣１枚
        ans+=(10-num)  #お釣り
    if n<10:
        break
    n=n//10
print(ans)

Submission status: Wrong Answer

Solution code:
N = list(input())
Y = [0]
for i in N:
    Y.append(int(i))
dp = [0 for i in range(len(N)+1)]
dp2 = [10 for i in range(len(N)+1)]
for i in range(1,len(N)+1):
    dp[i] = min(dp[i-1] + Y[i],dp2[i-1] + Y[i])
    dp2[i] = min(dp[i-1] + 1 + 10 - Y[i],dp2[i-1] - 1 + 10 - Y[i])
    print(str(dp[i])+":" +str(dp2[i]))
ans = min(dp[-1],dp2[-1])
#print(dp,dp2)
print(ans)

Submission status: Wrong Answer

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

 
dp = [[0] * 2 for _ in range(len(N) + 1)]
dp[0][1] = 1
 
for i, n in enumerate(N):
    dp[i + 1][0] = min(dp[i][0] + n, dp[i][1] + (10 - n))
    dp[i + 1][1] = min(dp[i][0] + (n + 1), dp[i][1] + (10 - (n + 1)))
    print(dp)
    
print(dp[-1][0])

Submission status: Wrong Answer

Solution code:
N = [int(n) for n in input()]
print(N)
 
dp = [[0] * 2 for _ in range(len(N) + 1)]
dp[0][1] = 1
 
for i, n in enumerate(N):
    dp[i + 1][0] = min(dp[i][0] + n, dp[i][1] + (10 - n))
    dp[i + 1][1] = min(dp[i][0] + (n + 1), dp[i][1] + (10 - (n + 1)))
    print(dp)
    
print(dp[-1][0])

Submission status: Wrong Answer

Solution code:
N = input().replace("\n", "")
L = len(N)
res = 0
f = 0
for i in range(L):
    n = int(N[i])

    if f == 1:
        if n >= 5:
            res += 9-n
            f = 1
        else:
            res += n
            f = 0
    else:
        if n >= 6:
            res += 11-n
            f = 1
        else:
            res += n

print(res)

Submission status: Wrong Answer

Solution code:
#E 復習
N = list(input())
N = [int(n) for n in N]
len_N = len(N)
ans = 0

for i in reversed(range(len_N)):
    num = int(N[i])
    if num == 10:
        N[i-1] += 1
    elif num >5:
        ans += (10-num)
        N[i-1] += 1
    else:
        ans += num
#     print(N)
#     print(num, ans)

if N[0] == 10:
    ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
N = input()

a = 0  # not carried
b = 0  # carried
for i in range(len(N) - 1, -1, -1):
    t = int(N[i])
    a, b = min(a + t, b + (t + 1)), min(a + (10 - t), b + (10 - (t + 1)))
print(min(a, b + 1))

Submission status: Wrong Answer

Solution code:
n = [0] + list(map(int, input())) + [0]
n = n[::-1]
#print(n)

b0 = [0]*len(n)
b1 = [0]*len(n)

for i in range(1, len(n)):
  num = n[i]
  b0[i] = min(b0[i-1]+num, b1[i-1]+num+1)
  b1[i] = min(b0[i-1]+10-num, b1[i-1]+10-(num+1))

print(min(b0[-1], b1[-1]))

Submission status: Wrong Answer

Solution code:
n = list(map(int, input()))
n = n[::-1]
#print(n)

b0 = [0]*(len(n)+1)
b1 = [0]*(len(n)+1)

for i in range(0, len(n)):
  num = n[i]
  b0[i+1] = min(b0[i]+num, b1[i]+num+1)
  b1[i+1] = min(b0[i]+10-num, b1[i]+10-(num+1))

print(min(b0[-1], b1[-1]+1))

Submission status: Wrong Answer

Solution code:
def main():
    from builtins import min
    N = int(input())

    a = 0  # not carried
    b = 0  # carried
    while N != 0:
        t = N % 10
        a, b = min(a + t, b + (t + 1)), min(a + (10 - t), b + (10 - (t + 1)))
        N //= 10
    print(min(a, b + 1))


main()

Submission status: Wrong Answer

Solution code:
n = list(map(int, input())) + [0]
n = n[::-1]
#print(n)

b0 = [0]*len(n)
b1 = [0]*len(n)

for i in range(1, len(n)):
  num = n[i]
  #print(num)
  b0[i] = min(b0[i-1]+num, b1[i-1]+num+1)
  b1[i] = min(b0[i-1]+10-num, b1[i-1]+10-(num+1))

  
print(min(b0[-1], b1[-1]))

Submission status: Wrong Answer

Solution code:
N = int(input())

a = 0  # not carried
b = 0  # carried
while N != 0:
    t = N % 10
    a, b = min(a + t, b + (t + 1)), min(a + (10 - t), b + (10 - (t + 1)))
    N //= 10
print(min(a, b + 1))

Submission status: Wrong Answer

Solution code:
N = int(input())

a = 0  # not carried
b = 0  # carried
while N != 0:
    t = N % 10
    a, b = min(a + t, b + t + 1), min(a + (10 - t), b + (10 - t) + 1)
    N //= 10
print(min(a, b + 1))

Submission status: Wrong Answer

Solution code:
N = '0' + input()
INF = float('inf')
dp = [[INF] * 2 for _ in range(len(N) + 1)]
dp[0][0] = 0

for i in range(len(N)):
    n = int(N[len(N) - 1 - i])
    for j in range(2):
        for a in range(10):
            ni = i + 1
            nj = 0
            b = a - n - j
            if b < 0:
                b += 10
                nj = 1
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + a + b)
            a == b

print(dp[len(N)][0], dp[len(N)][1])

Submission status: Wrong Answer

Solution code:
###間違っとる！！！！
#tmp="9"*(10**6)
tmp=input()
l=len(tmp)
#print(l)
rev=tmp[::-1]+"0"
seq_flag=False
ans=0
debug=[]
ans_arr=[]
next_bit=0
kuri_cal=0
for i in range(l-1):
    num=int(rev[i])+next_bit
    next_num=int(rev[i+1])
    if not seq_flag:
        if (num<5) or (num==5 and next_num<5):
            ans+=num
            ans_arr.append(ans)
            next_bit=0
            kuri_cal=0
            continue
        seq_s=i
        seq_flag=True
        kuri_cal=10-num
        next_bit+=1
        continue
    #print(kuri_cal)
    if num<5:
        ans+=(kuri_cal+num)
        next_bit=0
        seq_flag=False
    elif num>5:
        kuri_cal+=10-num
    else:
        if next_num<5:
            ans+=(kuri_cal+num)
            next_bit=0
            seq_flag=False
        else:
            kuri_cal+=10-num
        
"""    
        
    if next_num+next_bit<5:
        ans+=(kuri_cal+2)
        ans_arr.append(ans)
        debug.append([i,ans-1])
        seq_flag=False"""
#print(ans)
last=int(tmp[0])+next_bit
#print(last)
#print(kuri_cal)
ans+=kuri_cal+min(last,11-last)
ans_arr.append(ans)
print(ans)

Submission status: Wrong Answer

Solution code:
###間違っとる！！！！
tmp="9"*(10**6)
#tmp=input()
#tmp="78164062862089986280348253421170"
#tmp="314159265358979323846264338327950288419716939937551058209749445923078164062862089986280348253421170"
#tmp="846"
#tmp="253421170"
l=len(tmp)
#print(l)
rev=tmp[::-1]+"0"
seq_flag=False
ans=0
debug=[]
ans_arr=[]
next_bit=0
for i in range(l-1):
    num=int(rev[i])+next_bit
    next_num=int(rev[i+1])
    if not seq_flag:
        if (num<5) or (num==5 and next_num<5):
            ans+=num
            ans_arr.append(ans)
            next_bit=0
            kuri_cal=0
            continue
        seq_s=i
        seq_flag=True
        kuri_cal=10-num
        next_bit+=1
        continue
    #print(kuri_cal)
    if num<5:
        ans+=(kuri_cal+num)
        next_bit=0
        seq_flag=False
    elif num>5:
        kuri_cal+=10-num
    else:
        if next_num<5:
            ans+=(kuri_cal+num)
            next_bit=0
            seq_flag=False
        else:
            kuri_cal+=10-num
        
"""    
        
    if next_num+next_bit<5:
        ans+=(kuri_cal+2)
        ans_arr.append(ans)
        debug.append([i,ans-1])
        seq_flag=False"""
#print(ans)
last=int(tmp[0])+next_bit
#print(last)
#print(kuri_cal)
ans+=kuri_cal+min(last,11-last)
ans_arr.append(ans)
print(ans)

Submission status: Wrong Answer

Solution code:
N = list(input())
ans = 0
c = 0
judge = [0]*len(N)

for i in range(len(N)-1,0,-1):
    if int(N[i]) >= 5:
        judge[i-1] = 1

for i in range(len(N)):
    if judge[i] == 1:
        if int(N[i]) >= 5:
            ans += 9-int(N[i])
            c = 1
        else:
            ans += int(N[i])
    elif c == 1:
        ans += 11 - int(N[i])
        c = 0
    else:
        if int(N[i]) >= 6:
            ans += 11 - int(N[i])
        else:
            ans += int(N[i])

print(ans)

Submission status: Wrong Answer

Solution code:
s=input()
ans=0
y=z=0
t=0
for i in s[::-1]:
    x=int(i)
    if y==0 and x<5:
        ans+=x
    elif x>5:
        y+=x*(pow(10,z))
        z+=1
    else:
        ans+=x
        p=pow(10,z)-y
        y,z=0,1
        while p>0:
            ans+=p%10
            p//=10
        ans+=1
    t=x
else:
    if y>0:
        p=pow(10,z)-y
        while p>0:
            ans+=p%10
            p//=10
        ans+=1
print(ans)

Submission status: Wrong Answer

Solution code:
s=input()
ans=0
y=z=0
for i in s[::-1]:
    x=int(i)
    if y==0 and x<5:
        ans+=x
    elif x>=5:
        y+=x*(10**z)
        z+=1
    else:
        ans+=x
        p=(10**z)-y
        y=z=0
        while p>0:
            ans+=p%10
            p//=10
        ans+=1
else:
    if y>0:
        p=10**z-y
        y=z=0
        while p>0:
            ans+=p%10
            p//=10
        ans+=1
print(ans)

Submission status: Wrong Answer

Solution code:
s=input()
ans=0
y=z=0
for i in s[::-1]:
    x=int(i)
    if y==0 and x<6:
        ans+=x
    elif x>5:
        y+=x*(10**z)
        z+=1
    else:
        ans+=x
        p=(10**z)-y
        y=z=0
        while p>0:
            ans+=p%10
            p//=10
        ans+=1
else:
    if y>0:
        p=10**z-y
        y=z=0
        while p>0:
            ans+=p%10
            p//=10
        ans+=1
print(ans)

Submission status: Wrong Answer

Solution code:
s=input()
ans=0
y=z=0
for i in s[::-1]:
    x=int(i)
    if y==0 and x<6:
        ans+=x
    elif x>5:
        y+=x*(10**z)
        z+=1
    else:
        p=10**z-y
        y=z=0
        while p>0:
            ans+=p%10
            p//=10
else:
    p=10**z-y
    y=z=0
    while p>0:
        ans+=p%10
        p//=10
    #ans+=min(x,1+(10-x))
print(ans)

Submission status: Wrong Answer

Solution code:
s=input()
ans=0
y=0
for i in s[::-1]:
    x=int(i)+y
    y=[0,1][x>1+(10-x)]
    ans+=min(x,1+(10-x))
print(ans)

Submission status: Wrong Answer

Solution code:
#tmp="9"*(10**6)
tmp=input()
#tmp="314159265358979323846264338327950288419716939937551058209749445923078164062862089986280348253421170"
#tmp="253421170"
l=len(tmp)
#print(l)
rev=tmp[::-1]
seq_flag=False
ans=0

for i in range(l-1):
    num=int(rev[i])
    next_num=int(rev[i+1])
    if not seq_flag:
        if (num<5) or (num==5 and next_num<5):
            ans+=num
            continue
        seq_s=i
        seq_flag=True
        kuri_cal=9-num
        #print("ans:",ans)
    else:
        kuri_cal+=(9-num)
    #print(kuri_cal)
    if next_num<5:
        #print(rev[seq_s:i+1][::-1])
        #print(i,":",kuri_cal+2)
        #print()
        ans+=(kuri_cal+2)
        seq_flag=False
if not seq_flag:
    if int(tmp[0])<=5:
        ans+=int(tmp[0])
    else:
        ans+=11-int(tmp[0])
else:
    kuri_cal+=(9-int(tmp[0]))
    ans+=(kuri_cal+2)
print(ans)

Submission status: Wrong Answer

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

    for i,j in enumerate(s[::-1]):
        jk=int(j)
        if jk+x<=5:
            y+=jk*(10**i)
            x=0
        else:
            y+=10*(10**i)
            x=1
    z=y-chk
    #print(y)
    #print(y)
    #print(chk)
    #print(z)
    ans=0
    while 1:
        if z==0 and y==0:
            break
        ans+=y%10
        ans+=z%10
        y//=10
        z//=10
    print(ans)

if __name__=="__main__":
    sol(0)

Submission status: Wrong Answer

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


# 下の桁から貪欲に見ていくと解ける
# 1の位が4以下ならそのまま紙幣で支払うのが得→自身をそのまま答えに採用 (枚数として加算)
# 6以上ならばお釣り狙いのほうが得→自身は0にセットして、上位桁を+1する (枚数として10-自身を加算)
# 5ちょうどのときは、お釣りもそのまま払っても枚数は変わらない。だけど555みたいに、上位桁に+1すると得になる可能性がある。
# 555→15枚, 1000-445→15枚
# つまり5が出たときは更に上位の桁を見て、4以下ならそのまま払うほうが得,5以上なら桁上りのほうが得

# 5ちょうどのときは？ #お釣りもそのまま払っても同じ。だけど上位 お釣り狙いだと上の桁が多くなって支払う金額が増える可能性がある？
# 5ピッタリのときに上位桁に+1すると悪くなる場合が存在する 455 とは 15にってしまう
# 5ピッタリのときはどうするのがよいのか？

N = '0' + read()[:-1]

N = N[::-1]
ans = 0
i = 0

lo = int(N[i])
while i < len(N) - 1:
    hi = int(N[i + 1])
    if lo == 5:  # 5のときは上の桁の大きさによってお釣りが得になるかそのまま払うのが良くなるか変化する
        ans += lo
        if hi < 5:
            pass
        else:
            hi += 1
    elif lo < 5:
        # loが5以外のとき (お釣りが良いか、そのまま払うのが良いか考えるだけ)
        ans += lo
    else:
        ans += 10 - lo
        hi += 1  # 上の桁が繰り上がる
    lo = hi  # 桁を一つずらす
    i += 1


print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

# Nの価値の物を購入する際に必要な最小紙幣数とおつりの合計枚数
# 入力はN
# 通貨は10のn乗の紙幣のみ

def gen(n):
    digit_value = []
    
    while(n != 0):
        digit_value.append(n % 10)
        n //= 10
    
    digit_value.append(0)
    digit_value.reverse()
    
    return digit_value

# 支払いに必要な最小の紙幣数
def num_payment(money):
    count = 0
    digit_value = gen(money) 
    num_digit = len(digit_value) - 1
  
    print(digit_value)
    
    for i in range(1, num_digit + 1):
        if(digit_value[-i] <= 4):
             count += digit_value[-i]
        elif(digit_value[-i] == 5):
            if(digit_value[-(i+1)] < 5):
                count += digit_value[-i]
            else:
                count += (10 - digit_value[-i])
                digit_value[-(i+1)] += 1
        else:
            count += (10 - digit_value[-i])
            digit_value[-(i+1)] += 1
    
    return count + digit_value[0]

if __name__ == '__main__':
    value = int(input())
    print(num_payment(value))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
S=input()
total=0
pay=0
get=0
flg=0
for a in S:
  b=int(a)
  if(b>5):
    total=total+11-b-flg-flg
    flg=1
  else:
    total=total+b
    flg=0
print(total)

Submission status: Wrong Answer

Solution code:
def main():
    a = list(map(int, list(input())))
    n = len(a)
    dp = [[10**8, 10**8] for _ in [0]*(n+1)]
    dp[0] = [0, 0]
    for i in range(n):
        dp[i+1][0] = min(dp[i][0]+a[i], dp[i][1]+10-a[i])
        dp[i+1][1] = min(dp[i][0]+a[i]+1, dp[i][1]+9-a[i])
    print(dp[n][0])


main()

Submission status: Wrong Answer

Solution code:
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
import itertools
from functools import *
from itertools import permutations,combinations,groupby
import sys
import bisect
import string
import math
import time
import random
def Golf():
    *a,=map(int,open(0))
def S_():
    return input()
def IS():
    return input().split()
def LS():
    return [i for i in input().split()]
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def NI(n):
    return [int(input()) for i in range(n)]
def NI_(n):
    return [int(input())-1 for i in range(n)]
def StoI():
    return [ord(i)-97 for i in input()]
def ItoS(nn):
    return chr(nn+97)
def LtoS(ls):
    return ''.join([chr(i+97) for i in ls])
def GI(V,E,Directed=False,index=0):
    org_inp=[]
    g=[[] for i in range(n)]
    for i in range(E):
        inp=LI()
        org_inp.append(inp)
        if index==0:
            inp[0]-=1
            inp[1]-=1
        if len(inp)==2:
            a,b=inp
            g[a].append(b)
            if not Directed:
                g[b].append(a)
        elif len(inp)==3:
            a,b,c=inp
            aa=(inp[0],inp[2])
            bb=(inp[1],inp[2])
            g[a].append(bb)
            if not Directed:
                g[b].append(aa)
    return g,org_inp
def GGI(h,w,search=None,replacement_of_found='.',mp_def={'#':1,'.':0}):
#h,w,g,sg=GGI(h,w,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0}) # sample usage
    mp=[1]*(w+2)
    found={}
    for i in range(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[1]+[mp_def[j] for j in s]+[1]
    mp+=[1]*(w+2)
    return h+2,w+2,mp,found
def bit_combination(k,n=2):
    rt=[]
    for tb in range(n**k):
        s=[tb//(n**bt)%n for bt in range(k)]
        rt+=[s]
    return rt
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['YES','NO']

mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase
u_alp=string.ascii_uppercase
ts=time.time()
#sys.setrecursionlimit(10**7)
input=lambda: sys.stdin.readline().rstrip()
 
def ran_input():
    import random
    n=random.randint(4,16)
    rmin,rmax=1,10
    a=[random.randint(rmin,rmax) for _ in range(n)]
    return n,a

show_flg=False
show_flg=True

ans=0
inp=input()
#inp='36'
s=[int(i) for i in inp]
n=len(s)

dp=[[0]*2 for i in range(n+1)]
dp[0][0]=0
x,y=[],[]

for i in range(n):
    c=s[i]
    # x1 same_same, x2 up_same, y1 same_up, y2 up_up
    x1,x2,y1,y2=[],[],[],[]

    for j in range(i):
        x1=[t for t in x]
        x2=[t for t in y]
        y1=[t for t in x]
        y2=[t for t in y]
    x1+=[c]
    x2+=[c]
    y1+=[c+1]
    y2+=[0] if y2 else [c+1]

    nx1=dp[i][0]+c
    nx2=dp[i][1]+c
    if y1[-1]==10:
        dy1=2
    elif y1[-1]==0:
        dy1=9-c
    else:
        dy1=c+1 +1
    
    ny1=dp[i][0]+dy1
    
    if y2[-1]==10:
        dy2=2
    elif y2[-1]==0:
        dy2=10-c
        if len(y2)>=2:
            dy2-=1
    else:
        dy2=c+1+1
    ny2=dp[i][1]+dy2
    

    if nx1<nx2:
        x=[k for k in x1]
        dp[i+1][0]=nx1
    else:
        x=[k for k in x2]
        dp[i+1][0]=nx2

    if ny1<ny2:
        y=[i for i in y1]
        dp[i+1][1]=ny1
    else:
        y=[i for i in y2]
        dp[i+1][1]=ny2

print(min(dp[-1]))

Submission status: Wrong Answer

Solution code:
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
import itertools
from functools import *
from itertools import permutations,combinations,groupby
import sys
import bisect
import string
import math
import time
import random
def Golf():
    *a,=map(int,open(0))
def S_():
    return input()
def IS():
    return input().split()
def LS():
    return [i for i in input().split()]
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def NI(n):
    return [int(input()) for i in range(n)]
def NI_(n):
    return [int(input())-1 for i in range(n)]
def StoI():
    return [ord(i)-97 for i in input()]
def ItoS(nn):
    return chr(nn+97)
def LtoS(ls):
    return ''.join([chr(i+97) for i in ls])
def GI(V,E,Directed=False,index=0):
    org_inp=[]
    g=[[] for i in range(n)]
    for i in range(E):
        inp=LI()
        org_inp.append(inp)
        if index==0:
            inp[0]-=1
            inp[1]-=1
        if len(inp)==2:
            a,b=inp
            g[a].append(b)
            if not Directed:
                g[b].append(a)
        elif len(inp)==3:
            a,b,c=inp
            aa=(inp[0],inp[2])
            bb=(inp[1],inp[2])
            g[a].append(bb)
            if not Directed:
                g[b].append(aa)
    return g,org_inp
def GGI(h,w,search=None,replacement_of_found='.',mp_def={'#':1,'.':0}):
#h,w,g,sg=GGI(h,w,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0}) # sample usage
    mp=[1]*(w+2)
    found={}
    for i in range(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[1]+[mp_def[j] for j in s]+[1]
    mp+=[1]*(w+2)
    return h+2,w+2,mp,found
def bit_combination(k,n=2):
    rt=[]
    for tb in range(n**k):
        s=[tb//(n**bt)%n for bt in range(k)]
        rt+=[s]
    return rt
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['YES','NO']

mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase
u_alp=string.ascii_uppercase
ts=time.time()
#sys.setrecursionlimit(10**7)
input=lambda: sys.stdin.readline().rstrip()
 
def ran_input():
    import random
    n=random.randint(4,16)
    rmin,rmax=1,10
    a=[random.randint(rmin,rmax) for _ in range(n)]
    return n,a

show_flg=False
#show_flg=True

ans=0
inp=input()
#inp='36'
s=[int(i) for i in inp]
n=len(s)

dp=[[0]*2 for i in range(n+1)]
dp[0][0]=0
x,y=[],[]

for i in range(n):
    c=s[i]
    # x1 same_same, x2 up_same, y1 same_up, y2 up_up
    x1,x2,y1,y2=[],[],[],[]
    #show(x,y,z,x1,x2,y1,y2,z1,z2)
    for j in range(i):
        x1=[t for t in x]
        x2=[t for t in y]
        y1=[t for t in x]
        y2=[t for t in y]
    x1+=[c]
    x2+=[c]
    y1+=[c+1]
    y2+=[0] if y2 else [c+1]
    show(x1,x2,y1,y2)
    nx1=dp[i][0]+c
    nx2=dp[i][1]+c
    if y1[-1]==10:
        dy1=2
    elif y1[-1]==0:
        dy1=9-c
    else:
        dy1=c+1 +1
    
    ny1=dp[i][0]+dy1
    
    if y2[-1]==10:
        dy2=2
    elif y2[-1]==0:
        dy2=10-c
        if len(y2)>=2:
            dy2-=1
    else:
        dy2=c+1+1
    ny2=dp[i][1]+dy2
    
    show(nx1,nx2,ny1,ny2)
    if nx1<nx2:
        x=[k for k in x1]
        dp[i+1][0]=nx1
    else:
        x=[k for k in x2]
        dp[i+1][0]=nx2

    if ny1<ny2:
        y=[i for i in y1]
        dp[i+1][1]=ny1
    else:
        y=[i for i in y2]
        dp[i+1][1]=ny2
    show((x,y))#,'  ',x1,x2,y1,y2,z1,z2)
    #x,y,z,w=X,Y,Z,W

print(min(dp[-1]))

Submission status: Wrong Answer

Solution code:
S = input()[::-1]
l = len(S)
dp = [[0]*2 for _ in range(l+1)]
dp[1][0]=dp[0][0]+int(S[0])
dp[1][1]=dp[0][1]+10-int(S[0])
for i in range(1,l):
  dp[i+1][0]=min(dp[i][0]+int(S[i]),dp[i][1]+int(S[i])+1)
  dp[i+1][1]=min(dp[i][0]+10-int(S[i]),dp[i][1]+10-int(S[i])-1)
  
print(min(dp[-1]))

Submission status: Wrong Answer

Solution code:
S = input()
l = len(S)
dp = [[0]*2 for _ in range(l+1)]
for i in range(l):
  dp[i+1][0]=min(dp[i][1]+10-int(S[i]),dp[i][0]+int(S[i]))
  dp[i+1][1]=min(dp[i][1]+10-int(S[i])-1,dp[i][0]+int(S[i])+1)
print(min(dp[-1][0]+1,dp[-1][1]))

Submission status: Wrong Answer

Solution code:
S = input()
l = len(S)
dp = [[0]*2 for _ in range(l+1)]
for i in range(l):
  dp[i+1][0]=min(dp[i][1]+10-int(S[i]),dp[i][0]+int(S[i]))
  dp[i+1][1]=min(dp[i][1]+10-int(S[i])-1,dp[i][0]+int(S[i])+1)
if int(S[0])>=4:
  print(dp[-1][0]+1)
else:
  print(dp[-1][0])

Submission status: Wrong Answer

Solution code:
S = input()
l = len(S)
dp = [[0]*2 for _ in range(l+1)]
for i in range(l):
  dp[i+1][0]=min(dp[i][1]+10-int(S[i]),dp[i][0]+int(S[i]))
  dp[i+1][1]=min(dp[i][1]+10-int(S[i])-1,dp[i][0]+int(S[i])+1)
if int(S[0])>=5:
  print(dp[-1][0]+1)
else:
  print(dp[-1][0])

Submission status: Wrong Answer

Solution code:
S = input()
l = len(S)
dp = [[0]*2 for _ in range(l+1)]
for i in range(l):
  dp[i+1][0]=min(dp[i][1]+10-int(S[i]),dp[i][0]+int(S[i]))
  dp[i+1][1]=min(dp[i][1]+10-int(S[i])-1,dp[i][0]+int(S[i])+1)
if int(S[0])>=6:
  print(dp[-1][0]+1)
else:
  print(dp[-1][0])

Submission status: Wrong Answer

Solution code:
S = input()
l = len(S)
dp = [[0]*2 for _ in range(l+1)]
for i in range(l):
  dp[i+1][0]=min(dp[i][1]+10-int(S[i]),dp[i][0]+int(S[i]))
  dp[i+1][1]=min(dp[i][1]+10-int(S[i])-1,dp[i][0]+int(S[i])+1)
print(dp[-1][0]+1)

Submission status: Wrong Answer

Solution code:
tmp=input()
n=int(tmp)
l=len(tmp)
rev=tmp[::-1]
pay_rev=""
next_bit=0
for i in range(l):
    val=int(rev[i])+next_bit
    if val>5:
        val=0
        next_bit=1
    else:
        next_bit=0
    pay_rev+=str(val)
if pay_rev[-1]=="0":
    pay_rev+="1"
pay=pay_rev[::-1]

print(pay)
zankin=int(pay)-n
print(zankin)
ans=0
for i in pay:
    ans+=int(i)
for i in str(zankin):
    ans+=int(i)

print(ans)

Submission status: Wrong Answer

Solution code:
S = input()
l = len(S)
dp = [[0]*2 for _ in range(l+1)]
for i in range(l):
  dp[i+1][0]=min(dp[i][1]+10-int(S[i]),dp[i][0]+int(S[i]))
  dp[i+1][1]=min(dp[i][1]+10-int(S[i])-1,dp[i][0]+int(S[i])+1)
if int(S[0])!=9:
  print(dp[-1][0])
else:
  print(dp[-1][0]+1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
S = input()
l = len(S)
dp = [[0]*2 for _ in range(l+1)]
for i in range(l):
  dp[i+1][0]=min(dp[i][1]+10-int(S[i]),dp[i][0]+int(S[i]))
  dp[i+1][1]=min(dp[i][1]+10-int(S[i])-1,dp[i][0]+int(S[i])+1)
print(dp[-1][0])
print(dp)

Submission status: Wrong Answer

Solution code:
N = input()

dp = [[0 for _ in range(2)] for _ in range(len(N) + 1)]
for i in range(len(N)):
    dp[i+1][0] = min(dp[i][0]+int(N[len(N) - i - 1]),
                  dp[i][1]+int(N[len(N) - i - 1]))
    dp[i+1][1] = min(dp[i][0]+int(11-int(N[len(N) - i - 1])),
                dp[i][1]+int(11-int(N[len(N) - i - 1])))
print(min(dp[len(N)][0],dp[len(N)][1]))

Submission status: Wrong Answer

Solution code:
S = input()
l = len(S)
dp = [[0]*2 for _ in range(l+1)]
for i in range(l):
  dp[i+1][0]=min(dp[i][1]+10-int(S[i])+1,dp[i][0]+int(S[i]))
  dp[i+1][1]=min(dp[i][1]+10-int(S[i]),dp[i][0]+int(S[i])+1)
print(min(dp[-1][0],dp[-1][1]+1))

Submission status: Wrong Answer

Solution code:
S = input()
l = len(S)
dp = [[0]*2 for _ in range(l+1)]
for i in range(l):
  dp[i+1][0]=min(dp[i][1]+10-int(S[i]),dp[i][0]+int(S[i]))
  dp[i+1][1]=min(dp[i][1]+10-int(S[i])-1,dp[i][0]+int(S[i])+1)
print(min(dp[-1][0],dp[-1][1]+1))

Submission status: Wrong Answer

Solution code:
S = input()[::-1]
l = len(S)
dp = [[0]*2 for _ in range(l+1)]
for i in range(l):
  dp[i+1][0]=min(dp[i][1]+10-int(S[i]),dp[i][0]+int(S[i]))
  dp[i+1][1]=min(dp[i][1]+10-int(S[i])-1,dp[i][0]+int(S[i])+1)
print(min(dp[-1][0],dp[-1][1]+1))

Submission status: Wrong Answer

Solution code:
n = list(map(int, input()))
dp = (0, 1)
for i in n:
  dp = (min(dp[0] + i, dp[1] + 10 - i), min(dp[0] + i + 1, dp[1] + 9 - i))
  print(dp)
print(dp[0])

Submission status: Wrong Answer

Solution code:
d,e=0,0
for x in input():
	d,e=int(x)+min(e,d),9-int(x)+min(e,d+2)
print(min(d,e))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

L=list(map(int,input())); n=len(L)
L.reverse();L.append(0)
for i in range(n):

  if L[i] >= 6:
    count += 10-L[i]
    L[i] = 0; L[i+1] += 1

  elif L[i] == 5 and L[i+1] == 5:
    count += 10-L[i]
    L[i] = 0; L[i+1] += 1

print(count + sum(L))
# print(L)

Submission status: Wrong Answer

Solution code:
def main():
    n = input()[::-1] + "0"
    s = 0
    cnt = 0
    for i, ni in enumerate(n[:-1]):
        k = int(ni) + s
        if k < 5 or k == 5 and int(n[i + 1]) <= 5:
            cnt += k
            s = 0
        else:
            cnt += 10 - k
            s = 1
    cnt += s
    print(cnt)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = str(input())
keta = len(N)

ans = 0
is_up = 0
for i in range(keta):
  kazu = int(N[keta-1-i]) + is_up
  if kazu <= 4 :
    is_up = 0
    ans += kazu
  elif kazu == 5:
    if i > 0:
      if int(N[keta-i-2]) <= 4:
        is_up = 0
        ans += kazu
      else:
        is_up = 1
        ans += kazu  
    else:
      is_up = 0
      ans += kazu
  else :
    is_up = 1
    ans += (10 - kazu)
    
print(ans + is_up)

Submission status: Wrong Answer

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

ans = 0
carry = 0
for i in range(len(N)-1):
    n = int(N[i])
    nextn = int(N[i+1])
    if carry == 1:
        carry = 0
        n += 1

    if n <=4:
        ans += n
    elif n >= 6:
        ans += 10-n
        carry = 1
    else: # n==5
        if nextn <= 6:
            ans += n
        else:
            ans += 10-n
            carry = 1

print(ans)

Submission status: Wrong Answer

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

ans = 0
carry = 0
for i in range(len(N)-1):
    n = int(N[i])
    nextn = int(N[i+1])
    if carry == 1:
        carry = 0
        n += 1

    if n <=4:
        ans += n
    elif n >= 6:
        ans += 10-n
        carry = 1
    else: # n==5
        if nextn <= 4:
            ans += n
        else:
            ans += 10-n
            carry = 1

print(ans)

Submission status: Wrong Answer

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

while n:
    s = n.pop()

    if 0 <= s <= 4:
        ans += s + c_in
        c_in = 0
    elif s == 5:
        if c_in:
            ans += 4
        else:
            ans += s
    elif 6 <= s <= 9:
        ans += 10 - s - c_in
        c_in = 1


ans += c_in
print(ans)