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:
from collections import defaultdict as dd
n = input()
def num_bill(n):
    n = str(n)
    ans = sum([int(num) for num in n])
    return ans

buyer = num_bill(n)
buyer_bill = dd(int)
for i, s in enumerate(n):
    buyer_bill[i] = int(s)
#print(buyer_bill)
seller = 0
for i in range(len(n) - 1, -1, -1):
    if buyer_bill[i] > 5:
        buyer -= buyer_bill[i] - 1
        buyer_bill[i-1] += 1
        seller += 10 - buyer_bill[i]
        buyer_bill[i] = 0

    #print(buyer_bill, buyer, seller)
#print(buyer_bill)
print(buyer + seller)

Submission status: Wrong Answer

Solution code:
N=input()
inf=float("inf")
dp=[[inf]*(len(N)+1) for i in range(2)]
dp[0][0]=0
for i in range(len(N)):
   tmp=int(N[-1-i])
   dp[0][i+1]=min(dp[0][i],dp[1][i])+tmp
   dp[1][i+1]=min(dp[0][i]+11,dp[1][i]+9)-tmp
   print(dp[0][i+1],dp[1][i+1])
print(min(dp[0][-1],dp[1][-1]))

Submission status: Wrong Answer

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

l = n[::-1]
m = len(l)

cnt = 0
for i in range(m-1):
  if l[i] < 6:
    cnt += l[i]
  else:
    cnt += 10-l[i]
    l[i+1] += 1
    
if l[m-1] < 6:
  cnt += l[m-1]
else:
  cnt += 10-l[m-1]+1

print(cnt)

Submission status: Wrong Answer

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

l = n[::-1]
m = len(l)

print(l)

cnt = 0
for i in range(m-1):
  if l[i] < 6:
    cnt += l[i]
  else:
    cnt += 10-l[i]
    l[i+1] += 1
  print(cnt)
    
if l[m-1] < 6:
  cnt += l[m-1]
else:
  cnt += 10-l[m-1]+1

print(cnt)

Submission status: Wrong Answer

Solution code:
N = input() + "0"

dp = 0
k = 0

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

Submission status: Wrong Answer

Solution code:
N = "0" + input()

returnDP = 0
payDP = 0
j = 1
keta = 0
for i in N[::-1]:
    i = int(i) + keta
    if i == 10:
        returnDP = returnDP
        payDP = payDP
        keta = 1
    elif i > 5:
        payDP = payDP
        returnDP = returnDP + 10 - i
        keta = 1
    else:
        payDP = payDP + i
        returnDP = returnDP
        keta = 0
    j += 1

print(payDP + returnDP)

Submission status: Wrong Answer

Solution code:
N = "0" + input()

returnDP = [0]*(len(N)+1)
payDP = [0]*(len(N)+1)
j = 1
keta = 0
for i in N[::-1]:
    i = int(i) + keta
    if i == 10:
        returnDP[j] = returnDP[j-1]
        payDP[j] = payDP[j-1]
        keta = 1
    elif i > 5:
        payDP[j] = payDP[j-1]
        returnDP[j] = returnDP[j-1] + 10 - i
        keta = 1
    else:
        payDP[j] = payDP[j-1] + i
        returnDP[j] = returnDP[j-1]
        keta = 0
    j += 1

print(payDP[-1] + returnDP[-1])

Submission status: Wrong Answer

Solution code:
N = "0" + input()

returnDP = [0]*(len(N)+1)
payDP = [0]*(len(N)+1)
j = 1
keta = 0
for i in N[::-1]:
    i = int(i) + keta
    if i == 10:
        returnDP[j] = returnDP[j-1]
        payDP[j] = payDP[j-1]
        keta = 1
    elif i > 5:
        payDP[j] = payDP[j-1]
        returnDP[j] = returnDP[j-1] + 10 - i
        keta = 1
    else:
        payDP[j] = payDP[j-1] + i
        returnDP[j] = returnDP[j-1]
        keta = 0
    j += 1
print(payDP)
print(returnDP)
print(payDP[-1] + returnDP[-1])

Submission status: Wrong Answer

Solution code:
n = int(input())

# ループしてないかテスト

ans = 0
while n > 0:
    t = n
    pre_t = t
    keta = 0
    while t >= 10:
        pre_t = t
        t //= 10
        keta += 1
    #if t < 5 or (t == 5 and pre_t <= 54):  # 555 の時とか1000円だすと14枚で行けてしまう。。
    if t <= 5:  # test
        ans += t
        n = n - t * (10 ** keta)
        #print(t * (10 ** keta),'払った,残り', n, ans)
    else:
        ans += 1
        n = (10 ** (keta + 1)) - n  # どちらがお金を払うかが入れ替わる
        #print((10 ** (keta + 1)),'払った,お釣り', n, ans)
print(ans)

Submission status: Wrong Answer

Solution code:
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

from collections import deque
from collections import defaultdict
def main():
    N = S()
    N =  N[::-1]
    cnt = 0
    next = 0
    for i in range(len(N)):
        num = int(N[i])
        num += next
        if num <= 5:
            cnt += num
            next = 0
        else:
            cnt += 10 - num
            next = 1
    if next == 1:
        cnt += 1
    print(cnt)
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
S = input()
ans = 0
flag = False #繰り上がりありならTrue
flag2 = False #前が5ならTrue
for k in range(len(S)):
  n = int(S[-k-1])
  if flag2:
    if n < 5:
      flag = False
    else:
      flag = True
  if flag:
    n += 1
  if n == 5:
    ans += 5
    flag2 = True
  elif n < 5:
    ans += n
    flag2 = False
    flag = False
  else:
    ans += 10-n
    flag2 = False
    flag = True
if flag:
  ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

ans = 0
while N > 0:

    Q, R = N // 10, N % 10

    if R <= 5:
        ans += R

    else:
        ans += 10 - R
        Q += 1

    N = Q

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
A = [0] + list(map(int, list(input())))
while A[-1] == 0:
  A.pop()
ans = 0
c = 0
n = len(A)
for i in range(n-1):
  a, b = A[i], A[i+1]
  if c:
    if i != n-2:
      a, b = 9-a, 9-b
    else:
      a, b = 9-a, 10-b
  if b <= 4:
    ans += a
  else:
    ans += a+1
    c ^= 1
if c:
  ans += 10 - A[-1]
else:
  ans += A[-1]
print(ans)

Submission status: Wrong Answer

Solution code:
def solve(N):
    N = [0] + N
    k = 0  # 繰り上がり
    ans = 0
    for d, d_next in zip(N[::-1], N[-2::-1]):
        d += k
        if d <= 4 or (d==5 and d_next<5):
            k = 0
            ans += d
        else:
            k = 1
            ans += 10 - d

    return ans

def test(N, ans):
    for i in range(N, 10000):
        rem = i - N
        if sum(map(int, str(i))) + sum(map(int, str(rem))) < ans:
            print(f"!!! N={N}, ans={ans}, trueans={i, rem}")
def main():
    N = list(map(int, input()))
    print(solve(N))
    # for i in range(1, 10000):
    #     ans = solve(list(map(int, str(i))))
    #     test(i, ans)
main()

Submission status: Wrong Answer

Solution code:
N = input()

ans = 0
flg = False
for i, s in enumerate(N):
    d = int(s)
    if d <= 4:
        ans += d
        if flg:
            flg = False
    elif d == 5:
        if not flg and i < len(N)-1 and N[i+1] >= '5':
            ans += 5
            flg = True
        elif flg:
            ans += 4
            if i == len(N)-1 or N[i+1] <= '4':
                ans += 1
        else:
            ans += 5
    else:
        if flg:
            ans += 9-d
        else:
            ans += 10-d
            flg = True
        if i == len(N)-1 or N[i+1] <= '4':
            ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
n=['0']+list(input())
a=0
k=0
for i in range(len(n)-1,-1,-1):
  if (int(n[i])+k>5):
    a+=10-int(n[i])-k
    k=1
  else:
    a+=int(n[i])+k
    k=0
print(a)

Submission status: Wrong Answer

Solution code:
n=list(input())
a=0
k=0
for i in range(len(n)-1,-1,-1):
  if (int(n[i])+k>5):
    a+=10-int(n[i])
    k=1
  else:
    a+=int(n[i])
    k-0
print(a)

Submission status: Wrong Answer

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
import math
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul, itemgetter
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7

#############
# Main Code #
#############

N = input()

ans = 0
n_l = []
m_l = []
opt = ''
# m_l へは数字を逆順に収納している
for i in range(len(N))[::-1]:
    if int(N[i]) == 4 and len(opt) > 0:
        opt += N[i]
    elif int(N[i]) <= 4:
        if len(opt) > 0:
            m_l.append(opt)
        opt = ""
        n_l.append(int(N[i]))
    else:
        opt += N[i]

if len(opt) > 0:
    m_l.append(opt)

ans += sum(n_l)

for o in m_l:
    # 5単体が紛れ込むので個別で処理
    if o == '5':
        ans += 5
        continue
    # お釣りが来るようでかい紙幣を１枚出す
    ans += 1
    # 一桁目のお釣りは10 - int(o[j])
    ans += 1
    for j in range(len(o)):
        # 4でoptを繋げないとでかい紙幣の１枚　＋　一桁目のお釣りで１増える分で２枚増えるため
        # 上で4で繋がるようにしているが、4が先頭にあるとこの２つの効果が消えるので
        # 9 - int(o[j])をやめて4枚紙幣を出すよう修正
        if j == len(o) - 1 and o[j] == '4':
            ans += 4
            continue
        ans += 9 - int(o[j])
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter
from operator import mul
import copy
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
import heapq

n = int(input())

l = len(str(n))
nn = 10**l - n
mm = str(nn)

m = [str(n),str(nn)]
a = len(m[0]) - len(m[1])
m[1] = "0"*a + m[1]

c = 0
i = 0
while len(m[i]) > 0:

    if len(m[i]) >= 2:
        if int(m[i][0]) <= 4 or (int(m[i][0]) == 5 and int(m[i][1]) <= 5):
            c = c + int(m[i][0])
            ll = len(m[i])
            m[0] = m[0][1:]
            m[1] = m[1][1:]
        else:
            c = c + 1
            i = 1 - i
    else:
        if int(m[i][0]) <= 5:
            c = c + int(m[i][0])
            break
        else:
            c = c + 1
            i = 1 - i

print(c)

Submission status: Wrong Answer

Solution code:
INF=10**30
N=list(map(int,list(input())))[::-1]
l=len(N)
dp=[[INF,INF] for i in range(l+1)]
dp[0][0]=0
for i in range(l):
    dp[i+1][0]=min(dp[i+1][0],dp[i][0]+N[i])
    if N[i]!=9:
        dp[i+1][0]=min(dp[i+1][0],dp[i][1]+N[i]+1)
    dp[i+1][1]=min(dp[i+1][1],dp[i][0]+(10-N[i]))
    if N[i]!=0:
        dp[i+1][1]=min(dp[i+1][1],dp[i][1]+(10-N[i]-1))
    else:
        dp[i+1][1]=min(dp[i+1][1],dp[i][1]+9)
ans=dp[l][0] if N[-1]!=9 else min(dp[l][0],dp[l][1]+1)
print(ans)

Submission status: Wrong Answer

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

def solve(N):
    res = 0
    flg = 0
    for n in reversed(N):
        if flg == 2:
            if n >= 5:
                flg = 1
            else:
                flg = 0
        m = flg + n
        if m == 5:
            res += 5
            flg = 2
        elif m < 5:
            res += m
            flg = 0
        else:
            res += (10 - m)
            flg = 1
    print(res + min(flg,1))


N = list(map(int, input().strip()))
solve(N)

# for N in range(44,47):
#     print(N, "OK")
#     solve(list(map(int, str(N))))

Submission status: Wrong Answer

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

def solve(N):
    res = 0
    flg = 0
    for n in reversed(N):
        if flg == 2:
            if n >= 5:
                flg = 1
            else:
                flg = 0
        m = flg + n
        if m == 5:
            res += 5
            flg = 2
        elif m < 5:
            res += m
            flg = 0
        else:
            res += (10 - m)
            flg = 1
    print(res + flg)


N = list(map(int, input().strip()))
solve(N)

# for N in range(554,557):
#     print(N, "OK")
#     solve(list(map(int, str(N))))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6+1)
n=int(input())
def f(x):
    p=x//10
    q=x%10
    if x==0:
        return 0
    else:
        return f(p+q//6)+min(q,10-q)
print(f(n))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6+1)
import math
n=int(input())
def f(x):
    if x==0:
        return 0
    else:
        return f(x//10+math.floor((x%10)/6))+min(x%10,10-x%10)
print(f(n))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6+1)
import math
n=int(input())
def f(x):
    if x==1:
        return 1
    elif x==0:
        return 0
    else:
        return f(x//10+math.floor((x%10)/6))+min(x%10,10-x%10)
print(f(n))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=int(input())
print(243)

Submission status: Wrong Answer

Solution code:

N = input()
dp = [[0] * 2 for _ in range(len(N) + 1)]

for i in range(len(N)):
    n = int(N[i])
    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[-1][0])

Submission status: Wrong Answer

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

dp = [[0 for i in range(len(N))] for j in range(2)]

dp[0][0] = N[-1]
dp[1][0] = 1+10-N[-1]

lenN = len(N)
for i in range(1,lenN):
  if N[-i-1]==9:
    dp[0][i] = dp[0][i-1]+N[-i-1]
    dp[1][i] = min(dp[0][i-1]+2, dp[1][i-1]+1)
  else:
    dp[0][i] = min(dp[0][i-1]+N[-i-1], dp[1][i-1]+N[-i-1])
    dp[1][i] = min(dp[0][i-1]+10-N[-i-1], dp[1][i-1]+10-N[-i-1])

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

Submission status: Wrong Answer

Solution code:
#

import sys
input=sys.stdin.readline

def main():
    NS=input().strip("\n")
    dp=[[0]*2 for i in range(len(NS))]
    # 0:over確定 1:わからん
    dp[0][0]=11-int(NS[0])
    dp[0][1]=int(NS[0])
    for i in range(1,len(NS)):
        d=int(NS[i])
        dp[i][0]=min(dp[i-1][0]+min(d,10-d),dp[i-1][1]+min(d,11-d))
        dp[i][1]=dp[i-1][1]+d
    print(min(dp[-1]))
        
    
if __name__=="__main__":
    main()

Submission status: Wrong Answer

Solution code:
#

import sys
input=sys.stdin.readline

def main():
    N=input().strip("\n")
    ans=0
    for i in range(len(N)):
        d=int(N[i])
        if d<=5:
            ans+=d
        else:
            ans+=10-d
            if i==0:
                ans+=1
    print(ans)
    
if __name__=="__main__":
    main()

Submission status: Wrong Answer

Solution code:
N = input()[::-1]
ans = 0
flag = False
for i, n in enumerate(N):
    x = int(n)
    if flag:
        x += 1
        flag = False
    if x <= 5:
        ans += x
    else:
        ans += 10 - x
        flag = True
if flag:
    ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
# Template 1.0
import sys, re
from collections import deque, defaultdict, Counter, OrderedDict
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from heapq import heappush, heappop, heapify, nlargest, nsmallest
def STR(): return list(input())
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def list2d(a, b, c): return [[c] * b for i in range(a)]
def sortListWithIndex(listOfTuples, idx):   return (sorted(listOfTuples, key=lambda x: x[idx]))
def sortDictWithVal(passedDic):
    temp = sorted(passedDic.items(), key=lambda kv: (kv[1], kv[0]))
    toret = {}
    for tup in temp:
        toret[tup[0]] = tup[1]
    return toret
def sortDictWithKey(passedDic):
    return dict(OrderedDict(sorted(passedDic.items())))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

a = input()
ans = 0
for el in a:
    if(int(el)>5):
        ans+=11-int(el)
    else:
        ans+=int(el)
print(ans)

Submission status: Wrong Answer

Solution code:
n=input()
change=0
for i in range(1,len(n)+1):
  sn=list(n)
  x=sn[len(sn)-i]
  if int(x)>5:
    change+=(10-int(x))
    n=str(int(n)+(10-int(x))*10**(i-1))
pay=0
snf=list(n)
for j in range(len(snf)):
  pay+=int(snf[j])
print(pay+change)

Submission status: Wrong Answer

Solution code:
n = input()
ans = 0
next_i = 0
for i in range(len(n)):
    n_i = int(n[i])
    ans_1 = 1 + (10 - n_i) - next_i
    ans_2 = n_i
    if ans_1 < ans_2:
        ans += ans_1
        if next_i > 0:
            ans -= 1
        if 10 > n_i:
            next_i = 1
            
    else:
        ans += ans_2
        next_i = 0
print(ans)

Submission status: Wrong Answer

Solution code:
n = input()
ans = 0
for i in range(len(n)):
    n_i = int(n[i])
    ans_1 = 1 + (10 - n_i)
    ans_2 = n_i
    ans += min(ans_1, ans_2)
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    S = list( map( int, list( input())))
    N = len(S)
    dp = [[0,0] for _ in range(N+1)]
    dp[0][1] = 1
    for i in range(N):
        s = S[i]
        dp[i+1][0] = min(dp[i][0] + s, dp[i][1]+s)
        dp[i+1][1] = min(dp[i][0] + 11-s, dp[i][1]+9-s)
        # 11-sは、追加で支払うものと、お釣り
        # 9-sは、追加はすでに支払われていて、繰り下がりが起こっている
    print( min(dp[N][0], dp[N][1]))
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

ans = 0
carry = 0   #0 or 1 or 2. when 2, it means that it's optional between 0 and 1.
for i in range(len(N) - 1, -1, -1):
    if carry == 2:
        if N[i] >= 5:
            digit = N[i] + 1
            carry = 1
            ans += (10 - digit)
        else:
            digit = N[i]
            carry = 0
            ans += digit
    else:
        digit = N[i] + carry
        if digit >= 6:
            carry = 1
            ans += (10 - digit)
        elif digit <= 4:
            carry = 0
            ans += digit
        else:
            carry = 2
            ans += digit
        


ans += carry

print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
n = len(s)

ans = 0

sub = 0
judge = False
for i in range(n-1, -1, -1):
  key = int(s[i])+sub
  if judge:
    if key <= 4:
      ans += key
      sub = 0
    elif key == 5:
      ans += 9-key
    else:
      ans += 9-key
      sub = 1
  else:
    if key <= 5:
      ans += key
      sub = 0
    else:
      ans += 10-key
      sub = 1
  if key == 5:
    judge = True
  else:
    judge = False

ans += sub

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
INF = float("inf")


def solve(N: str):
    # 下の桁から貪欲？
    N = "0" + N
    ans = 0
    kuriagari = 0
    for i in reversed(range(len(N))):
        n = int(N[i])
        if kuriagari == 1:
            kuriagari = 0
            n += 1
        if 0 <= n <= 4:
            ans += n
        elif n == 5:
            # print("上の桁を見て決める")
            k = i-1
            while N[k] == "5":
                k -= 1
            if 0 <= int(N[k]) <= 4:
                # print("桁上がらない")
                ans += 5
            else:
                # print("桁上がる")
                ans += 5
                kuriagari = 1
        else:
            ans += 10 - n
            kuriagari = 1
        # print("現状", ans, kuriagari)
    print(ans)

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    a = next(tokens)
    N = str(a)  # type: str
    solve(N)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N=list(input()[::-1])+["0"]
ans=0
for i in range(len(N)):
  if N[i] in ["6","7","8","9"]:
    ans+=10-int(N[i])
    N[i]="0"
    N[i+1]=str(int(N[i+1])+1)
  elif N[i]=="10":
    N[i]="0"
    N[i+1]=str(int(N[i+1])+1)
for i in range(len(N)):
  ans+=int(N[i])
print(ans)

Submission status: Wrong Answer

Solution code:
s = input()[::-1]
size = len(s)
s += "9"

ans = 0
bef = 0

for i in range(size):
    v1 = int(s[i])
    v2 = int(s[i+1])
    if v1+bef>=6 or (v1+bef>=5 and v2>=5):
        ans += 10-(v1+bef)
        bef = 1
    else:
        ans += (v1+bef)
        bef = 0

ans += bef
print(ans)

Submission status: Wrong Answer

Solution code:
s = input()[::-1]
size = len(s)
s += "9"

ans = 0
bef = 0

for i in range(size):
    v1 = int(s[i])
    v2 = int(s[i+1])
    if v1+bef>=6 or (v1+bef>=5 and v2>=5):
        ans += 10-(v1+bef)
        bef = 1
    else:
        ans += (v1+bef)
        bef = 0

ans += (v1+bef)//10
print(ans)

Submission status: Wrong Answer

Solution code:
s = input()

ans = 0
bef = 0

for i in s[::-1]:
    v = int(i)
    if v+bef>=5:
        ans += 10-(v+bef)
        bef = 1
    else:
        ans += (v+bef)
        bef = 0

ans += (v+bef)//10
print(ans)

Submission status: Wrong Answer

Solution code:
n=list(map(int,list(input())))
cnt=0
for nn in n:
    if nn==9:
        cnt+=2
    elif nn==8:
        cnt+=3
    elif nn==7:
        cnt+=4
    elif nn==6:
        cnt+=5
    elif nn==5:
        cnt+=5
    elif nn==4:
        cnt+=4
    elif nn==3:
        cnt+=3
    elif nn==2:
        cnt+=2
    elif nn==1:
        cnt+=1

print(cnt)

Submission status: Wrong Answer

Solution code:
N = input()
ans = [0,1]
for s in N:
    s = int(s)
    ans[0], ans[1] = min(ans[0]+s, ans[1]+10-s), min(ans[0]+s+1,ans[1]+9-s)


print(min(ans))

Submission status: Wrong Answer

Solution code:
N= int(input())
n = N
ans = 0
i = 0
while n:
    j = i
    i = n%10
    if i > 11-i:
        ans += 10-i
    else:
        ans += i
    n//= 10
if (j > 5 and i <= 5) or (j <= 5 and i > 5):
    ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import math
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
from collections import deque
from bisect import bisect_left
from itertools import product
def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
#文字列を一文字ずつ数字に変換、'5678'を[5,6,7,8]とできる
def LSI(): return list(map(int, list(sys.stdin.readline().rstrip())))
def LSI2(N): return [list(map(int, list(sys.stdin.readline().rstrip()))) for i in range(N)]
#文字列として取得
def ST(): return sys.stdin.readline().rstrip()
def LST(): return sys.stdin.readline().rstrip().split()
def LST2(N): return [sys.stdin.readline().rstrip().split() for i in range(N)]
def FILL(i,h): return [i for j in range(h)]
def FILL2(i,h,w): return [FILL(i,w) for j in range(h)]
def FILL3(i,h,w,d): return [FILL2(i,w,d) for j in range(h)]
def FILL4(i,h,w,d,d2): return [FILL3(i,w,d,d2) for j in range(h)]
def sisha(num,digit): return Decimal(str(num)).quantize(Decimal(digit),rounding=ROUND_HALF_UP)
#'0.01'や'1E1'などで指定、整数に戻すならintをかます
MOD = 1000000007
INF = float("inf")
sys.setrecursionlimit(10**6+10)

N = LSI()
dp = [[INF]*2 for _ in range(len(N)+1)] #dp[i][0]が下からi桁めをぴったり払う最低枚数
dp[0][0] = 0
for i in range(len(N)):
    d = N[-(i+1)]
    dp[i+1][0] = min(dp[i][0] + d, dp[i][1] + d + 1)
    dp[i+1][1] = min(dp[i][0] + (10-d), dp[i][1] + (10-d) - 1)
print(min(dp[-1][0], dp[-1][1]))

Submission status: Wrong Answer

Solution code:
s = input()
keta = len(s)
infi = 10 ** 10
dp = [[infi] * (2) for _ in range(keta + 1)]
dp[0][0] = dp[0][1] = 0
s = s[::-1]
for i in range(keta):
    for j in range(2):
        num = int(s[i])
        if j == 1:
            num += 1
        ni = i + 1
        if num == 10:
            nj = 1
            num = 0
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + num)
        elif num < 5:
            nj = 0
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + num)
        elif num > 5:
            nj = 1
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + 10 - num)
        else:
            nj = 0
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + num)
            nj = 1
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + 10 - num)
print(min(dp[keta][0], dp[keta][1] + 1))

Submission status: Wrong Answer

Solution code:
s = input()
keta = len(s)
n = int(s)
infi = 10 ** 10
dp = [[infi] * (2) for _ in range(keta + 1)]
dp[0][0] = dp[0][1] = 0
s = s[::-1]
for i in range(keta):
    for j in range(2):
        num = int(s[i])
        if j == 1:
            num += 1
        ni = i + 1
        if num == 10:
            nj = 1
            num = 0
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + num)
        elif num < 5:
            nj = 0
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + num)
        elif num > 5:
            nj = 1
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + 10 - num)
        else:
            nj = 0
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + num)
            nj = 1
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + 10 - num)
print(min(dp[keta][0], dp[keta][1] + 1))

Submission status: Wrong Answer

Solution code:
import sys
import math
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
from collections import deque
from bisect import bisect_left
from itertools import product
def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
#文字列を一文字ずつ数字に変換、'5678'を[5,6,7,8]とできる
def LSI(): return list(map(int, list(sys.stdin.readline().rstrip())))
def LSI2(N): return [list(map(int, list(sys.stdin.readline().rstrip()))) for i in range(N)]
#文字列として取得
def ST(): return sys.stdin.readline().rstrip()
def LST(): return sys.stdin.readline().rstrip().split()
def LST2(N): return [sys.stdin.readline().rstrip().split() for i in range(N)]
def FILL(i,h): return [i for j in range(h)]
def FILL2(i,h,w): return [FILL(i,w) for j in range(h)]
def FILL3(i,h,w,d): return [FILL2(i,w,d) for j in range(h)]
def FILL4(i,h,w,d,d2): return [FILL3(i,w,d,d2) for j in range(h)]
def sisha(num,digit): return Decimal(str(num)).quantize(Decimal(digit),rounding=ROUND_HALF_UP)
#'0.01'や'1E1'などで指定、整数に戻すならintをかます
MOD = 1000000007
INF = float("inf")
sys.setrecursionlimit(10**6+10)

N = LSI()
plus = 0
ans = 0
temp = 0
before_five = 0
for j in range(len(N)+1):
    if j>len(N)-1:
        i = plus
    else:
        i = N[-(j+1)] + plus

    if i+before_five <= 4:
        before_five = 0
        ans += i
        plus = 0
    elif i + before_five >= 6:
        if before_five == 1:
            i += 1
        before_five = 0
        ans += 10-i
        plus = 1
    else:#i+before_five==5
        before_five = 1
        ans += 5
        plus = 0


print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import math
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
from collections import deque
from bisect import bisect_left
from itertools import product
def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
#文字列を一文字ずつ数字に変換、'5678'を[5,6,7,8]とできる
def LSI(): return list(map(int, list(sys.stdin.readline().rstrip())))
def LSI2(N): return [list(map(int, list(sys.stdin.readline().rstrip()))) for i in range(N)]
#文字列として取得
def ST(): return sys.stdin.readline().rstrip()
def LST(): return sys.stdin.readline().rstrip().split()
def LST2(N): return [sys.stdin.readline().rstrip().split() for i in range(N)]
def FILL(i,h): return [i for j in range(h)]
def FILL2(i,h,w): return [FILL(i,w) for j in range(h)]
def FILL3(i,h,w,d): return [FILL2(i,w,d) for j in range(h)]
def FILL4(i,h,w,d,d2): return [FILL3(i,w,d,d2) for j in range(h)]
def sisha(num,digit): return Decimal(str(num)).quantize(Decimal(digit),rounding=ROUND_HALF_UP)
#'0.01'や'1E1'などで指定、整数に戻すならintをかます
MOD = 1000000007
INF = float("inf")
sys.setrecursionlimit(10**6+10)

N = I()
plus = 0
ans = 0
temp = 0
before_five = 0
while True:
    i = N%10 + plus

    if i+before_five <= 4:
        before_five = 0
        ans += i
        plus = 0
    elif i + before_five >= 6:
        if before_five == 1:
            i += 1
        before_five = 0
        ans += 10-i
        plus = 1
    else:#i+before_five==5
        before_five = 1
        ans += 5
        plus = 0

    N = N//10
    if N==0 and plus==0:
        break
print(ans)

Submission status: Wrong Answer

Solution code:
N = input()
dp = [0] * (len(N) + 1)
kuriage = 0

for i in range(len(N)):
    p = int(N[-i - 1])
    d = 10 - p + 1 - kuriage
    if d < p:
        kuriage = 2
    else:
        kuriage = 0
    dp[i + 1] += min(p, d) + dp[i]

print(dp[len(N)])

Submission status: Wrong Answer

Solution code:
N = input()
dp = [0] * (len(N) + 1)
for i in range(len(N)):
    p = int(N[-i - 1])
    d = 10 - p + 1
    dp[i + 1] += min(p, d) + dp[i]

print(dp[len(N)])

Submission status: Wrong Answer

Solution code:
N = input()
dp = [0] * (len(N) + 1)
for i in range(len(N)):
    p = int(N[-i - 1])
    d = 10 - p + 1
    dp[i + 1] += min(p, d) + dp[i]

print(dp[len(N) - 1])