Buggy python submission (verdict: Runtime Error):
import functools
import sys

sys.setrecursionlimit(10000000)
n = input()
memo = {}
lenn = len(n)
@functools.lru_cache(None)
def dp(index, carry):
    if index >= lenn:
        return 99999999999999999999 if carry else 0
    v = int(n[index])
    if carry:
        # ぴったり払う
        temp = 10 - v + dp(index+1, 0)
        # お釣りありで払う
        temp = min(temp, 9 - v + dp(index+1, 1))
    else:
        # ぴったり払う
        temp = v + dp(index+1, 0)
        # お釣りありで払う
        temp = min(temp, 1 + v + dp(index+1, 1))
    return temp

print(min(dp(0, 0), 1 + dp(0, 1)))

Fixed submission:
n = input()
lenn = len(n)

dp = [0, 10000]
for d in reversed(n):
    d = int(d)
    dp = [min(d + dp[0], d + 1 + dp[1]), min(10 - d + dp[0], 9 - d + dp[1])]

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

Buggy python submission (verdict: Runtime Error):
import functools
import sys

sys.setrecursionlimit(1000000000)
n = input()
memo = {}

@functools.lru_cache(None)
def dp(index, carry):
    if index >= len(n):
        return 99999999999999999999 if carry else 0
    v = int(n[index])
    if carry:
        # ぴったり払う
        temp = 10 - v + dp(index+1, 0)
        # お釣りありで払う
        temp = min(temp, 9 - v + dp(index+1, 1))
    else:
        # ぴったり払う
        temp = v + dp(index+1, 0)
        # お釣りありで払う
        temp = min(temp, 1 + v + dp(index+1, 1))
    return temp

print(min(dp(0, 0), 1 + dp(0, 1)))

Fixed submission:
n = input()
lenn = len(n)

dp = [0, 10000]
for d in reversed(n):
    d = int(d)
    dp = [min(d + dp[0], d + 1 + dp[1]), min(10 - d + dp[0], 9 - d + dp[1])]

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

Buggy python submission (verdict: Runtime Error):
import functools
import sys

sys.setrecursionlimit(10000000)
n = input()
memo = {}

@functools.lru_cache(None)
def dp(index, carry):
    if index >= len(n):
        return 99999999999999999999 if carry else 0
    v = int(n[index])
    if carry:
        # ぴったり払う
        temp = 10 - v + dp(index+1, 0)
        # お釣りありで払う
        temp = min(temp, 9 - v + dp(index+1, 1))
    else:
        # ぴったり払う
        temp = v + dp(index+1, 0)
        # お釣りありで払う
        temp = min(temp, 1 + v + dp(index+1, 1))
    return temp

print(min(dp(0, 0), 1 + dp(0, 1)))

Fixed submission:
n = input()
lenn = len(n)

dp = [0, 10000]
for d in reversed(n):
    d = int(d)
    dp = [min(d + dp[0], d + 1 + dp[1]), min(10 - d + dp[0], 9 - d + dp[1])]

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

Buggy python submission (verdict: Runtime Error):
import functools
n = input()
memo = {}

@functools.lru_cache(None)
def dp(index, carry):
    if index >= len(n):
        return 99999999999999999999 if carry else 0
    v = int(n[index])
    if carry:
        # ぴったり払う
        temp = 10 - v + dp(index+1, 0)
        # お釣りありで払う
        temp = min(temp, 9 - v + dp(index+1, 1))
    else:
        # ぴったり払う
        temp = v + dp(index+1, 0)
        # お釣りありで払う
        temp = min(temp, 1 + v + dp(index+1, 1))
    return temp

print(min(dp(0, 0), 1 + dp(0, 1)))

Fixed submission:
n = input()
lenn = len(n)

dp = [0, 10000]
for d in reversed(n):
    d = int(d)
    dp = [min(d + dp[0], d + 1 + dp[1]), min(10 - d + dp[0], 9 - d + dp[1])]

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

Buggy python submission (verdict: Runtime Error):
s = input()
ans = 0
index = 0
rev = {'0': '9',
       '1': '8',
       '2': '7',
       '3': '6',
       '4': '5',
       '5': '4',
       '6': '3',
       '7': '2',
       '8': '1',
       '9': '0'}
def f(n):
    global ans, index

    lenn = len(n)
    while index < lenn and (n[index] < '5' or (n[index] == '5' and (index == lenn - 1 or n[index+1] <= '4'))):
        ans += int(n[index])
        index += 1
    if index < lenn:
        ans += 1
        f(''.join([rev[t] for t in n if t]))
f(s)
print(ans)

Fixed submission:
n = input()
lenn = len(n)

dp = [0, 10000]
for d in reversed(n):
    d = int(d)
    dp = [min(d + dp[0], d + 1 + dp[1]), min(10 - d + dp[0], 9 - d + dp[1])]

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

Buggy python submission (verdict: Time Limit Exceeded):
s = input()
ans = 0
index = 0
def f(n):
    global ans, index
    lenn = len(n)
    while index < lenn and (n[index] < '5' or (n[index] == '5' and (index == lenn - 1 or n[index+1] <= '4'))):
        ans += int(n[index])
        index += 1
    if index < lenn:
        ans += 1
        f(str(10**len(n)-int(n)))
f(s)
print(ans)


Fixed submission:
n = input()
lenn = len(n)

dp = [0, 10000]
for d in reversed(n):
    d = int(d)
    dp = [min(d + dp[0], d + 1 + dp[1]), min(10 - d + dp[0], 9 - d + dp[1])]

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

Buggy python submission (verdict: Time Limit Exceeded):
s = input()
ans = 0
index = 0
def f(n):
    global ans, index
    lenn = len(n)
    while index < lenn and (n[index] < '5' or (n[index] == '5' and (index == lenn - 1 or n[index+1] <= '4'))):
        ans += int(n[index])
        index += 1
    if index < lenn:
        ans += 1
        f(str(10**len(n)-int(n)))
f(s)
print(ans)

Fixed submission:
n = input()
lenn = len(n)

dp = [0, 10000]
for d in reversed(n):
    d = int(d)
    dp = [min(d + dp[0], d + 1 + dp[1]), min(10 - d + dp[0], 9 - d + dp[1])]

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

Buggy python submission (verdict: Time Limit Exceeded):
s = input()
ans = 0
def f(n):
    global ans
    while n and (n[0] < '5' or (n[0] == '5' and (len(n) == 1 or n[1] <= '4'))):
        ans += int(n[0])
        n = n[1:]
    if n:
        ans += 1
        f(str(10**len(n)-int(n)))
f(s)
print(ans)

Fixed submission:
n = input()
lenn = len(n)

dp = [0, 10000]
for d in reversed(n):
    d = int(d)
    dp = [min(d + dp[0], d + 1 + dp[1]), min(10 - d + dp[0], 9 - d + dp[1])]

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

Buggy python submission (verdict: Time Limit Exceeded):
s = input()
keta = len(s)
n = int(s)

ans = 0
while n > 0:
    hoge = 10 ** (keta-1)
    t = n // (hoge)
    if t == 0:
        keta -= 1
        continue

    if t < 5 or (t == 5 and ((n // (hoge//10) if hoge >= 10 else 0) <= 54)):  # 555 の時とか1000円だすと14枚で行けてしまう。。
        ans += t
        n = n - t * hoge
        keta -= 1

    else:
        ans += 1
        n = hoge*10 - n  # どちらがお金を払うかが入れ替わる

print(ans)


Fixed submission:
n = input()
lenn = len(n)

dp = [0, 10000]
for d in reversed(n):
    d = int(d)
    dp = [min(d + dp[0], d + 1 + dp[1]), min(10 - d + dp[0], 9 - d + dp[1])]

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

Buggy python submission (verdict: Time Limit Exceeded):
s = input()
keta = len(s) + 1
n = int(s)

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


Fixed submission:
n = input()
lenn = len(n)

dp = [0, 10000]
for d in reversed(n):
    d = int(d)
    dp = [min(d + dp[0], d + 1 + dp[1]), min(10 - d + dp[0], 9 - d + dp[1])]

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

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())

ans = 0
while n > 0:
    t = n
    pre_t = t
    keta = 0
    while t >= 1000000000:
        pre_t = t
        t //= 1000000000
        keta += 9
    while t >= 10:
        pre_t = t
        t //= 10
        keta += 1
    if t < 5 or (t == 5 and pre_t <= 54):  # 555 の時とか1000円だすと14枚で行けてしまう。。
        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)


Fixed submission:
n = input()
lenn = len(n)

dp = [0, 10000]
for d in reversed(n):
    d = int(d)
    dp = [min(d + dp[0], d + 1 + dp[1]), min(10 - d + dp[0], 9 - d + dp[1])]

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

Buggy python submission (verdict: Time Limit Exceeded):
n = input()

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

Fixed submission:
n = input()
lenn = len(n)

dp = [0, 10000]
for d in reversed(n):
    d = int(d)
    dp = [min(d + dp[0], d + 1 + dp[1]), min(10 - d + dp[0], 9 - d + dp[1])]

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

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())

# 0, 0+0
# 1, 1+0
# 2, 2+0
# 3, 3+0
# 4, 4+0
# 5, 5+0
# 6, 1+4
# 7, 1+3
# 8, 1+2
# 9, 1+1
# 99, 2枚
# 99999, 2枚...
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枚で行けてしまう。。
        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)


Fixed submission:
n = input()
lenn = len(n)

dp = [0, 10000]
for d in reversed(n):
    d = int(d)
    dp = [min(d + dp[0], d + 1 + dp[1]), min(10 - d + dp[0], 9 - d + dp[1])]

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

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())

# 0, 0+0
# 1, 1+0
# 2, 2+0
# 3, 3+0
# 4, 4+0
# 5, 5+0
# 6, 1+4
# 7, 1+3
# 8, 1+2
# 9, 1+1
# 99, 2枚
# 99999, 2枚...
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枚で行けてしまう。。
        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)

Fixed submission:
n = input()
lenn = len(n)

dp = [0, 10000]
for d in reversed(n):
    d = int(d)
    dp = [min(d + dp[0], d + 1 + dp[1]), min(10 - d + dp[0], 9 - d + dp[1])]

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