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:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#solve
def solve():
    n = S()
    dp1 = [inf] * (len(n))
    dp2 = [inf] * (len(n))
    dp1[0] = int(n[0])
    dp2[0] = 10-int(n[0])
    for i in range(len(n)-1):
        x = int(n[i+1])
        if dp1[i] + x < dp1[i + 1]:
            dp1[i + 1] = dp1[i] + x
        if x != 0:
            if dp1[i] + (10 - x) < dp2[i + 1]:
                dp2[i + 1] = dp1[i] + (10 - x)
        x += 1
        if x != 10:
            if dp2[i] + x < dp1[i + 1]:
                dp1[i + 1] = dp2[i] + x
        if dp2[i] + (10 - x) < dp2[i + 1]:
            dp2[i + 1] = dp2[i] + (10 - x)

    print(min(dp1[-1],dp2[-1]))
    return


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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#solve
def solve():
    n = S()
    n = n[::-1] + ["0"]
    ans = 0
    b = 0
    f = 0
    x = 0
    for ni in n:
        if b:
            if x and "0" <= ni <= "4":
                b = 0
                ans += int(ni) - (ni != "4")
                x = 0
            elif x:
                ans += 10 - int(ni)
                b = 1
                f = ni == "5"
                x = 0
            elif f and ni == "0":
                x = 0
                pass
            elif "0" <= ni <= "3":
                ans += int(ni) + b
                b = 0
                x = 0
            else:
                ans += 9 - int(ni)
                x = ni == "4"
            f = 0
        else:
            if "0" <= ni <= "4":
                ans += int(ni)
            else:
                ans += 10 - int(ni)
                b = 1
                f = ni == "5"
        print(ans,ni)
    
    return


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

Submission status: Wrong Answer

Solution code:
def solve():
    N = input()
    N = N[::-1] + '0'
    
    INF = 10 ** 32
    
    import numpy as np
    dp = np.full((len(N), 2), INF)
    dp[0][0] = 0
    
    for i, x in enumerate(int(n) for n in N[:-1]):
        ni = i + 1
        dp[ni][0] = min(dp[i] + x)
        dp[ni][1] = min(dp[i][0] + 11 - x, dp[i][1] + 9 - x)
    
    ans = min(dp[-1])
    
    print(ans)

solve()

Submission status: Wrong Answer

Solution code:
def solve():
    from sys import stdin
    f_i = stdin
    
    N = f_i.readline().rstrip()
    N = N[::-1] + '0'
    
    INF = 10 ** 32
    
    import numpy as np
    dp = np.full((len(N), 2), INF)
    dp[0][0] = 0
    
    for i, x in enumerate(int(n) for n in N[:-1]):
        ni = i + 1
        dp[ni][0] = min(dp[i] + x)
        dp[ni][1] = min(dp[i][0] + 11 - x, dp[i][1] + 9 - x)
    
    ans = min(dp[-1])
    
    print(ans)

solve()

Submission status: Wrong Answer

Solution code:
N = input()
x = 0
n = list(N)
n = [int(s) for s in n]
keta = len(n)
if n[keta-1] <= 5:
    x += n[keta-1]
else:
    x += 10 - n[keta-1]
    n[keta -2] += 1
for i in range(keta -2):
    if n[keta - i -2] == 10:
        n[keta - i -2] = 0
        n[keta - i - 3] += 1
    if n[keta - i -2] <= 5:
        x += n[keta - i -2]
    else:
        x += 10 - n[keta - i -2]
        n[keta - i - 3] += 1
if n[0] >= 6:
    x += 1 + 10 - n[0]
else:
    x += n[0]
print(x)

Submission status: Wrong Answer

Solution code:
N = int(input())

keta = len(str(N))
result = 0
a = False

for i in range(1, keta+1):
    tmp = N%10
    N //= 10
    if a:
        tmp += 1
    if tmp == 10:
        a = True
    elif tmp <= 5:
        result += tmp
        a = False
    else:
        result += 10 - tmp
        a = True
if tmp >5 :
    result += 1
print(result)

Submission status: Wrong Answer

Solution code:
def change(a):
    r = 0
    a = str(a)
    for i in range(len(a)):
        r += int(a[i])
    return r
def num(s:str):
    if not s:
        return 0
    r = 0
    s = s[::-1]
    for i in range(len(s)):
        r += int(s[i]) * (10 ** i)
    return r
        
def solve(s:str):
    if not s:
        return 0
    t = num(s)
    return min(1 + change((10 ** (len(s))) -  t),  int(s[0]) + solve(s[1:]))

n = int(input())
print(solve("0" + str(n)))

Submission status: Wrong Answer

Solution code:
n = int(input())
n1 = str(n)
length = len(n1)
ans = 0

for i in range(length):
    k = int(n1[-(i+1)])
    if k >= 6:
        n += (10 - k)*(10**i)
        ans += 10 - k
        str(n)

n1 = str(n)
length = len(n1)
for i in range(length):
    ans += int(n1[-(i+1)])

print(ans)

Submission status: Wrong Answer

Solution code:
def solve():
    N = input()
    N = N[::-1]
    N += '0'
    
    INF = 10 ** 7
    
    import numpy as np
    dp = np.full((len(N) + 1, 2), INF)
    dp[0][0] = 0
    
    for i, x in enumerate(int(n) for n in N):
        ni = i + 1
        for j in range(2):
            x += j
            for a in range(10):
                nj = 0
                b = a - x
                if b < 0:
                    nj = 1
                    b += 10
                dp[ni][nj] = min(dp[ni][nj], dp[i][j] + a + b)
    
    ans = dp[-1][0]
    
    print(ans)

solve()

Submission status: Wrong Answer

Solution code:
def solve():
    from sys import stdin
    f_i = stdin
    
    N = f_i.readline().rstrip()
    N = N[::-1]
    N += '0'
    
    INF = 10 ** 7
    
    import numpy as np
    dp = np.full((len(N) + 1, 2), INF)
    dp[0][0] = 0
    
    for i, x in enumerate(int(n) for n in N):
        ni = i + 1
        for j in range(2):
            x += j
            for a in range(10):
                nj = 0
                b = a - x
                if b < 0:
                    nj = 1
                    b += 10
                dp[ni][nj] = min(dp[ni][nj], dp[i][j] + a + b)
    
    ans = dp[-1][0]
    
    print(ans)

solve()

Submission status: Wrong Answer

Solution code:
N = [int(_) for _ in list(input())]
a, b = 0, 1
for n in N:
    a, b = min(a+n, b+10-n), min(a+n+1, b+9-n)
print(min(a, b))

Submission status: Wrong Answer

Solution code:
# coding: utf-8

n = input().rstrip()
count = 0
c = 0
for i in range(len(n)):
  num = int(n[-i-1])
  if c + num <= 5:
    count += c + num
    c = 0
  else:
    count += 10 - num - c
    c = 1
print(count + c)

Submission status: Wrong Answer

Solution code:
# e 数位dp 还可以从右到左
# 参考 https://maspypy.com/atcoder-%e5%8f%82%e5%8a%a0%e6%84%9f%e6%83%b3-2019-02-16abc-155
# https://atcoder.jp/contests/abc155/submissions/10155415
# in your code
import numpy as np
DBG=0
def p(*args, **kargs):
    if DBG:print(*args, **kargs)
read = input 
rn = lambda :list(map(int, read().split()))

def sol(s):
    dp = 0, 1 # n, n + 1
    for x in map(int, s):
        c, d = dp
    #                     为什么这里x = 0的时候不受影响 c+0 << d+10，因为abs(c-d) < 1
        
        #    前一位没借位  前一位借位
        a = min(c + x,    d + 10 - x)# for ...n
        #    这里x=9为什么不受影响, c+10 >> d，因为abs(c-d) < 1
        b = min(c + x + 1,d + 10 - 1 - x )# for ...(n+1)
        
        # 因为a,b相差小于1。f(n), f(n+1)相差总是小于1，多给1元即可
        dp = a,b
        p(dp)
    return dp[0]
if DBG:
    pass
#     sol("100")
#     assert 8 == sol("36")
#     assert sol("91") == 3
#     assert 243 == sol("314159265358979323846264338327950288419716939937551058209749445923078164062862089986280348253421170")
sol(read())

Submission status: Wrong Answer

Solution code:
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

# 引かれる数の左からi桁目に+10 & 引く数のi-1桁目に+1 を適宜行う

def calc(x):
    if 0 <= x <= 5:
        return x
    else:
        return 10-x+1

N = I()
S = str(N)
"""
L = len(S)

dp0 = [0]*L
dp1 = [0]*L
dp0[0] = calc(int(S[0]))
dp1[0] = calc(int(S[0])+1)

for i in range(1,L):
    n = int(S[i])
    if n == 0:
        dp0[i] = dp0[i-1]
    else:
        dp0[i] = min(dp0[i-1]+n, dp1[i-1]+10-n)
    if n == 9:
        dp1[i] = dp1[i-1]
    else:
        dp1[i] = min(dp0[i-1]+n+1, dp1[i-1]+9-n)

print(dp0[-1])
"""

Submission status: Wrong Answer

Solution code:
from collections import Counter
N = str(input())
d = Counter(N)
sum = d['1']*1+d['2']*2+d['3']*3+d['4']*4+d['5']*5+d['6']*5+d['7']*4+d['8']*3+d['9']*2
n = '0' + N + '0'

count = 0
for ind, i in enumerate(N):
    if int(i) >= 6:
        if int(n[ind]) >= 5:
            sum -= 1
        if int(n[ind+2]) >= 5:
            sum -= 1
    elif i == '5' and int(n[ind]) >= 5 and int(n[ind+2]) >= 5:
        sum -= 1
    
    if count > 0:
        count -= 1
    elif i == '4' and int(n[ind]) >= 5 and int(n[ind+2]) >= 5:
        a = 0
        b = 0
        j = 0
        k = 0
        while 1:
            if int(n[ind-j]) >= 6:
                a = 1
                break
            elif int(n[ind-j]) == 5:
                j += 1
            else:
                break
        while 1:
            if int(n[ind+2+k]) >= 6:
                b = 1
                break
            elif int(n[ind+2+k]) == 5:
                k += 1
            elif int(n[ind+2+k]) == 4:
                count = k+1
                k += 1
            else:
                break
        if a*b == 1:
            sum -= 1
                    
print(sum)

Submission status: Wrong Answer

Solution code:
from collections import Counter
N = str(input())
d = Counter(N)
sum = d['1']*1+d['2']*2+d['3']*3+d['4']*4+d['5']*5+d['6']*5+d['7']*4+d['8']*3+d['9']*2
b = '00' + N + '0'

for ind, i in enumerate(N):
    if int(i) >= 6:
        if int(b[ind+1]) >= 5:
            sum -= 1
        if int(b[ind+3]) >= 5:
            sum -= 1
    elif i == '5' and int(b[ind+1]) >= 5 and int(b[ind+3]) >= 5:
        sum -= 1
    elif i == '4' and int(b[ind+3]) >= 5:
        if int(b[ind+1]) >= 6 or (int(b[ind+1]) >= 5 and int(b[ind]) >= 5):
            sum -= 1
            
print(sum)

Submission status: Wrong Answer

Solution code:
from collections import Counter
N = str(input())
d = Counter(N)
sum = d['1']*1+d['2']*2+d['3']*3+d['4']*4+d['5']*5+d['6']*5+d['7']*4+d['8']*3+d['9']*2
b = '0' + N + '0'
for ind, i in enumerate(N):
    if i == '5' and int(b[ind]) >= 5 and int(b[ind+2]) >= 5:
        sum -= 1
    elif int(i) >= 6:
        if int(b[ind]) >= 5:
            sum -= 1
        if int(b[ind+2]) >= 5:
            sum -= 1
print(sum)

Submission status: Wrong Answer

Solution code:
a = input()
 
cnt = 0
sakki5 = False
sakki6 = False
for s in a:
    if int(s) < 6:
        cnt += int(s)
        sakki6 = False
        if int(s) == 5:
            sakki5 = True
    else:
        cnt += 11 - int(s)
        if sakki6:
            cnt -= 2
        if sakki5:
            cnt -= 1
        sakki6 = True
        sakki5 = False
print(cnt)

Submission status: Wrong Answer

Solution code:
s = 0
for c in input():
  x = int(c)
  s += min(x, 10-x+1)
  
print(s)

Submission status: Wrong Answer

Solution code:
N = int(input())

result = 0
while 'true':
    if N == 0:
        break
    elif N % 10 < 5:
        result += N % 10
        N //= 10
    else:
        result += 10 - (N % 10)
        N = (N + 10) // 10
        
print(result)

Submission status: Wrong Answer

Solution code:
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))
inf = 10 **9 + 5
def main():
    s = str(input())
    s = s[::-1]
    dp1 = [inf] *(10 ** 6 +3)
    dp2 = [inf] *(10 ** 6 +3)
    dp1[0] = int(s[0])
    dp2[0] =  10 -int(s[0])
    for i in range(1,len(s)):
        dp1[i] = min(dp2[i-1] + int(s[i]) + 1,dp1[i-1] + int(s[i]))
        dp2[i] = min(dp2[i-1] + 9 - int(s[i]),dp1[i-1] + 10 - int(s[i]))
    if s[len(s) -1] == "9":
        dp2[len(s) -1] += 1
    print(min(dp1[len(s)-1],dp2[len(s)-1]))

main()

Submission status: Wrong Answer

Solution code:
N = reversed("0" + input())
digit = next(N)
total = 0
carry = 0
for nextdigit in N:
    digit = int(digit) + carry
    if digit < 1:
        carry = 0
    elif digit < 5:
        total += digit
        carry = 0
    elif digit == 5:
        if int(nextdigit) < 5:
            total += digit
            carry = 0
        else:
            total += 10 - digit
            carry = 1
    elif digit < 10:
        total += 10 - digit
        carry = 1
    else:
        carry = 1
    digit = nextdigit

print(total)

Submission status: Wrong Answer

Solution code:

import numpy as np
from functools import *
import sys
sys.setrecursionlimit(100000)



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


def II():
    return int(input())


#N=list(map(int,list(input())[::-1]))
N = list(map(int, list(input())))

dp=[[-1,-1] for i in range(len(N)+1)]
dp[0]=[0,1]
b=0
N.insert(0,0)

for i in range(1,len(N)):
    up = 10-N[i]
    dp[i][0]=min(dp[i-1][0]+N[i],dp[i-1][1]+up)
    dp[i][1] = min(dp[i-1][0]+N[i]+1, dp[i-1][1]+up-1)
   

print(min(dp[-1]))

Submission status: Wrong Answer

Solution code:

import numpy as np
from functools import *
import sys
sys.setrecursionlimit(100000)



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


def II():
    return int(input())


#N=list(map(int,list(input())[::-1]))
N = list(map(int, list(input())))

dp=[[-1,-1] for i in range(len(N)+1)]
dp[0]=[0,1]
b=0
N.insert(0,0)
print(N)
for i in range(1,len(N)):
    up = 10-N[i]
    dp[i][0]=min(dp[i-1][0]+N[i],dp[i-1][1]+up)
    dp[i][1] = min(dp[i-1][0]+N[i]+1, dp[i-1][1]+up-1)
   

print(min(dp[-1]))

Submission status: Wrong Answer

Solution code:

import numpy as np
from functools import *
import sys
sys.setrecursionlimit(100000)



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


def II():
    return int(input())


#N=list(map(int,list(input())[::-1]))
N = list(map(int, list(input())))

dp=[[-1,-1] for i in range(len(N)+1)]
dp[0]=[0,1]
b=0
N.insert(0,0)
print(N)
for i in range(1,len(N)):
    print(i)
    up = 10-N[i]
    dp[i][0]=min(dp[i-1][0]+N[i],dp[i-1][1]+up)
    dp[i][1] = min(dp[i-1][0]+N[i]+1, dp[i-1][1]+up-1)
   

print(min(dp[-1]))

Submission status: Wrong Answer

Solution code:
def main():
    N = [int(x) for x in input()]
    dp0 = 0
    dp1 = 1e100
    for n in N:
        # そのまま払う
        a = min(dp0 + n, dp1 + 10 - n)
        # 1多めに払う
        b = min(dp0 + n + 1, dp1 + 10 - (n+1))
        dp0, dp1 = a, b

    print(dp0)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
def main():
    input=sys.stdin.readline
    S=input().strip()
    print(S)
    dp=[[0,0] for i in range(len(S)+1)]
    dp[0][1]=1
    for i in range(1,len(S)+1):
        for j in (0,1):
            if j==0:
                dp[i][0]=min(dp[i-1][0]+int(S[i-1]),dp[i-1][1]+10-int(S[i-1]))
            elif j==1:
                dp[i][1]=min(dp[i-1][0]+int(S[i-1])+1,dp[i-1][1]+10-int(S[i-1])-1)

    print(dp[len(S)][0])

if __name__=="__main__":
    main()

Submission status: Wrong Answer

Solution code:
N=input()
a=[]
s=len(N)

for i in range(s-1,-1,-1):
    a.append(int(N[i]))
a.append(0)
#print(a)

p=sum(a)
t=0
f=0
c=0
for i in range(len(a)-1):
    if a[i] <= 5:
        t+=a[i]
        if a[i] == 5 and a[i+1]>=5:a[i+1]+=1
        f=0
        
    else:
        c+=10-a[i]
        a[i+1] += 1
        f=1
        
    print(t+f+c)

Submission status: Wrong Answer

Solution code:
N = input().strip()
cmin = 0
for i in range(len(N)):
    cmin += int(N[i])
M = list(N)
i = len(N)-1
while i>-1:
    m = int(M[i])
    if m<5:
        i -= 1
    elif m>5:
        cmin += 10-m
        M[i] = "0"
        cmin -= m
        i -= 1
        while i>-1 and M[i]=="9":
            M[i]=="0"
            cmin -= 9
            i -= 1
        if i>-1:
            M[i] = str(int(M[i])+1)
            cmin += 1
        else:
            cmin += 1
    else:
        if i>0 and int(M[i-1])>5:
            cmin += 10-m
            M[i] = "0"
            cmin -= m
            i -= 1
            while i>-1 and M[i]=="9":
                M[i]=="0"
                cmin -= 9
                i -= 1
            if i>-1:
                M[i] = str(int(M[i])+1)
                cmin += 1
            else:
                cmin += 1
        else:
            i -= 1
print(cmin)

Submission status: Wrong Answer

Solution code:
ans = 0
for s in reversed(input()):
    n = int(s)
    if n < 6:
        ans += n
    else:
        ans += (10 - n) + 1

print(ans)

Submission status: Wrong Answer

Solution code:
ans = 0
for s in reversed(input()):
    n = int(s)
    if n < 6:
        ans += n
        ans += n % 10
        nxt = False
    else:
        ans += (10 - n) + 1

print(ans)

Submission status: Wrong Answer

Solution code:
# print('input >>')
N = input()
N = reversed(N)
ans = 0
nxt = False
for n in N:
    i_n = int(n) + nxt
    if i_n % 10 >= 6:
        ans += 10 - i_n % 10
        nxt = True
    else:
        ans += i_n % 10
        nxt = False
    # print(N, ans)
 
# print('-----output-----')
print(ans)

Submission status: Wrong Answer

Solution code:
# print('input >>')
N = input()
N = reversed(N)
ans = 0
nxt = False
for n in N:
    i_n = int(n) - nxt
    if i_n % 10 >= 6:
        ans += 10 - i_n % 10
        nxt = True
    else:
        ans += i_n % 10
        nxt = False
    # print(N, ans)

# print('-----output-----')
print(ans)

Submission status: Wrong Answer

Solution code:
# print('input >>')
N = input()
N = reversed(N)
ans = 0
nxt = False
for n in N:
    i_n = int(n)
    if i_n % 10 >= 6:
        ans += 10 - i_n % 10
        nxt = True
    else:
        ans += i_n % 10
        nxt = False
    # print(N, ans)

Submission status: Wrong Answer

Solution code:
# print('input >>')
N = input()
N = reversed(N)
ans = 0
nxt = False
for n in N:
    i_n = int(n) + nxt
    if i_n % 10 >= 6:
        ans += 10 - i_n % 10
        nxt = True
    else:
        ans += i_n % 10
        nxt = False
    # print(N, ans)

Submission status: Wrong Answer

Solution code:
def solve():
    N = int(input())
    s = str(N)
    length = len(s)
    precise = 0
    greedy = 0
    
    for i in range(length):
        a = int(s[i])
        precise += a # ぴったりの額
        
        if 0 <= a < 6:
            greedy += a
        else:
            greedy += 10-a+1

    return min(precise, greedy)

print(solve())

Submission status: Wrong Answer

Solution code:
N=input()
L=[int(N[i]) for i in range(len(N))]

P=0
OT=0

for i in range(len(L)-1):
    if 0 <= L[i] <= 4:
        P += L[i] + OT
        OT = 0

    elif L[i] == 5:
        if OT == 1:
            P += 4

        else:
            if L[i+1] >= 5:
                P += 1 + 4
                OT = 1
            else:
                P += 5

    else:
        if OT == 1:
            P += 9-L[i]
            OT = 1
        else:
            P += 10 - L[i]
            OT = 1

Pone = 0
if OT==0:
    Pone = min(L[-1],11-L[-1])
elif OT==1 and L[-1]<=4:
    Pone = 1+L[-1]
else:
    Pone = 10-L[-1]

print(P+Pone)

Submission status: Wrong Answer

Solution code:
tmp = input()
n_int = int(tmp)
n_list = [int(i) for i in list(tmp)]


ans = 0
pay = n_int

for i in range(len(n_list)):
    keta = int(str(pay)[-(i+1)])
    if 6 <= keta:
        pay += (10-keta)*10**i

# print(pay)

pay_maisuu = 0

for p in str(pay):
    pay_maisuu += int(p)

oturi = pay - n_int

oturi_maisuu = 0

for o in str(oturi):
    oturi_maisuu += int(o)

ans = pay_maisuu + oturi_maisuu

print(ans)

Submission status: Wrong Answer

Solution code:
N=input()
L=[int(N[i]) for i in range(len(N))]

P=0
U=0

for i in range(len(L)-1):
    if L[i] == 0:
        P += U
        U = 0

    elif 1 <= L[i] <= 4:
        P += L[i]+U
        U = 0

    elif L[i] == 5:
        if U == 1:
            P += U + 4
            U = 1
        else:
            if L[i+1] >= 5:
                P += 4
                U = 1
            else:
                P += 5

    elif L[i] >= 6:
        if U == 1:
            P += U + (9-L[i])
            U = 1
        else:
            P += 9-L[i]
            U = 1

P += U + min(L[-1],10-L[-1])

print(P)

Submission status: Wrong Answer

Solution code:
def s_in():
    return input()

def n_in():
    return int(input())

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

class Interval():
    def __init__(self, li):
        self.li = li
        self.n = len(li)
        self.sum_li = [li[0]]
        for i in range(1, self.n):
            self.sum_li.append(self.sum_li[i-1] + li[i])

    def sum(self, a, b=None):
        if b is None:
            return self.sum(0, a)

        res = self.sum_li[min(self.n-1, b-1)]
        if a > 0:
            res -= self.sum_li[a-1]
        return res

N = s_in()[::-1]
n = len(N)

dp1 = [0 for _ in range(n)]
dp1[0] = int(N[0])
# dp1[i] は i桁目を同じ値だけ使ったとしたときの最小値
dp2 = [0 for _ in range(n)]
dp2[0] = 10 - int(N[0])
# dp2[i] は i桁目を繰り上げたとしたときの最小値

for i, s in enumerate(N[1:]):
    i += 1

    m = int(s)
    dp1[i] = min(dp1[i-1] + m,      dp2[i-1] + (m+1))
    dp2[i] = min(dp1[i-1] + (10-m), dp2[i-1] + (10-m-1))

print(min(dp1[n-1], dp2[n-1]))

Submission status: Wrong Answer

Solution code:
n=input()
a=len(n)
A=[]
for i in range(a):
  A.append(int(n[i]))
print(A)
ans=0
ans1=0
ans2=10
for i in range(a):
  ans1=ans+A[i]
  ans2=ans+10-A[i]
  ans=min(ans1,ans2)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

Submission status: Wrong Answer

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


def solve(N, INF=1000000):
    L = len(N)
    N = list(reversed("0" + N))
    dp0 = [0 for i in range(L + 1)]
    dp1 = [0 for i in range(L + 1)]
    dp1[0] = INF
    for i in range(L):
        x = int(N[i])
        dp0[i + 1] = min(dp0[i] + x, dp1[i] + x)
        if x == 0:
            dp1[i + 1] = INF
        else:
            dp1[i + 1] = min(dp0[i] + 1 + (10 - x), dp1[i] + 1 + (10 - x) - 2)
    return min(dp0[-1], dp1[-1])


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

Submission status: Wrong Answer

Solution code:
n = input()

ans = 0

for i in n[::-1]:
    i = int(i)
    ans += min(10 - i, i)

if 6 <= int(n[0]):
    ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
s=input()[::-1]
l=[0]
l=[int(i) for i in s]
l.append(0)
ans=0
s+="0"
for i in range(len(l)):
    if int(l[i])>5:
        l[i]=0
        l[i+1]+=1
ans=sum(l)
#print(l)
for i in range(len(l)-1):
    x=l[i]-int(s[i])
    #print(x)
    if x<0:
        x+=10
        l[i+1]-=1
        #l[i+1]-=1
    #print(x)
    ans+=x
print(ans)

Submission status: Wrong Answer

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


def solve(N, INF=1000000):
    L = len(N)
    N = list(reversed("0" + N))
    dp0 = [0 for i in range(L + 1)]
    dp1 = [0 for i in range(L + 1)]
    dp1[0] = INF
    for i in range(L):
        x = int(N[i])
        dp0[i + 1] = min(dp0[i] + x, dp1[i] + x)
        if x == 0:
            dp1[i + 1] = INF
        else:
            dp1[i + 1] = min(dp0[i] + 1 + (10 - x), dp1[i] - 1 + (10 - x))
    return min(dp0[-1], dp1[-1])


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

Submission status: Wrong Answer

Solution code:
n=str(input())
count=0
l=[["0","1","2","3","4","5","6","7","8","9"],[0,1,2,3,4,5,5,4,3,2]]
for i in range(len(n)-1):
  for j in range(10):
    if n[i]==l[0][j]:
      count+=l[1][j]
      break
print(count)

Submission status: Wrong Answer

Solution code:
s=input()
l=[0]
for i in s:
    l.append(int(i))
#l=[int(i) for i in s[::-1]]
#l.append(0)
#print(l)
ans=0
for i in range(1,len(l)):
    if l[i]>5:
        l[i]=0
        l[i-1]+=1
s="0"+s
#print(l,s)
for i in range(len(s)):
    ans+=l[i]
    ans+=[l[i]-int(s[i]),9+l[i]-int(s[i])][l[i]<int(s[i])]
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = input()

keta = len(n)
ans = 0


for i in range(keta):
    x = int(n[(i+1)*(-1)])
    m = 100
    for j in range(10):
        if j >= x:
            m = min(m, j + j-x)
        else:
            m = min(m, j + 10+j-x)
    ans += m
    
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    N = [int(x) for x in input()]
    dp0 = 0
    dp1 = 1
    for n in N:
        # そのまま払う
        a = min(dp0 + n, dp1 + 10 - n)
        # 1多めに払う
        b = min(dp0 + n + 1, dp1 + 10 - (n+1))
        dp0, dp1 = a, b
        print(n, dp0, dp1)
    print(dp0)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N = [int(x) for x in input()]
    dp0 = 0
    dp1 = 1
    for n in N:
        # そのまま払う
        a = min(dp0 + n, dp1 + 10 - n)
        # 1多めに払う
        b = min(dp0 + n + 1, dp1 + 10 - (n+1))
        dp0, dp1 = a, b
    print(min(dp0, dp1))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n=list(input())
lis=[]
for k in range(len(n)):
  lis.append(int(n[len(n)-1-k]))
             
t=0
print(lis)
for k in range(len(lis)):
  if lis[k]==10:
    if k==len(lis)-1:
      t+=1
      break
    else:
      lis[k]=0
      lis[k+1]+=1
  if lis[k]<5:
    t+=lis[k]
  elif lis[k]>5:
    t=t+10-lis[k]
    if k==len(lis)-1:
        t+=1
        break
    else:
        
      lis[k+1]+=1
  elif lis[k]==5:
    if k==len(lis)-1:
      t+=5
    else:
      t+=5
      if lis[k+1]<5:
        pass
      else:
        lis[k+1]+=1
print(t)

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
h=0
l=0
for i in range(a):
  if a!=0 and a!=a-1 and s[i]=="4":
    j=i
    while j>=1:
      j=j-1
      if int(s[j])==5:
        continue
      elif int(s[j])<=4:
        h=0
        break
      else:
        h=1
        break
    k=i
    while k<=a-2:
      k=k+1
      if int(s[k])==5:
        continue
      elif int(s[k])<=4:
        l=0
        break
      else:
        l=1
        break
    if h*l==1:
      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])>=6 and int(s[i+1])>=6:
      g=g+1
print(ans-g)