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

Solution (Python):
n = list(map(int, list("0" + input())))
ans = 0
for i in range(len(n)):
    check = int(n[-i-1])
    if 0 <= check <= 4:
        ans += check
    elif 6 <= check <= 9:
        ans += 10 - check
        temp = 2
        while n[-i-temp] == 9:
            n[-i-temp] = (int(n[-i-temp]) + 1) % 10
            temp += 1
        else:
            n[-i-temp] = (int(n[-i-temp]) + 1) % 10
    else:
        ans += 5
        check2 = n[-i-2]
        if check2 >= 5:
            temp2 = 2
            while n[-i-temp2] == 9:
                n[-i-temp2] = (int(n[-i-temp2]) + 1) % 10
                temp2 += 1
            else:
                n[-i-temp2] = (int(n[-i-temp2]) + 1) % 10
print(ans)

Solution (Python):
N = list('0'+input())
l = len(N)
ans = 0
i = l-1
while i > 0:
    if int(N[i])>=6 or (int(N[i]) == 5 and int(N[i-1])>=5):
        ans+=10-int(N[i])
        i-=1
        while N[i] == '9':
            i-=1
        N[i] = str(int(N[i])+1)
    else:
        ans+=int(N[i])
        i-=1
ans+=int(N[0])
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10000000)


n = input()






























    























a = [0 for i in range(len(n) + 1)]
for i in range(1, len(n) + 1):
    
    m = int(n[-i]) + a[i - 1]
    
    if m < 5:
        a[i - 1] = m
    elif m > 5:
        a[i] += 1
        a[i - 1] = - (10 - m)
    else: 
        if i != len(n) and int(n[-i-1]) >= 5:
            a[i] += 1
            a[i - 1] = - m
        else:
            a[i - 1] = m


print(sum(map(abs, a)))

Solution (Python):
n = input()
value = 0
answer = 0
over = 0
five = 0

for i in range(len(n)+1):
    if i == len(n):
        if over == 1:
            answer += 1
            break
        else:
            break
    value = int(n[len(n)-1-i]) + over
    if five == 1:
        if value >= 5:
            answer += (10 - value - 1)
            five = 0
            over = 1
        else:
            answer += value
            five = 0
            over = 0
    else:
        if value > 5:
            answer += (10 - value)
            over = 1
        elif value == 5:
            answer += 5
            five = 1
            over = 0
        else:
            answer += value
            over = 0

print(answer)

Solution (Python):
def main():
    a = list(map(int, list(input())))
    a.reverse()
    n = len(a)
    a += [0]
    b = [i for i in a]
    cnt = 0
    for i in range(len(a)):
        if b[i] > 5 or (b[i] == 5 and b[i+1] >= 5):
            b[i] = 0
            b[i+1] += 1
    cnt = sum(b)
    for i in range(n):
        if a[i] <= b[i]:
            cnt += b[i]-a[i]
        else:
            b[i] += 10
            b[i+1] -= 1
            cnt += b[i]-a[i]
    print(cnt)


main()

Solution (Python):
"""
N,K = list(map(int,input().split()))
A = list(map(int,input().split()))

A.sort()


def bisect(i,V):
    l = i+1
    r = N-1
    if A[i]*A[r]<V:
        return r-i
    if [i]*A[l]>=V:
        return 0
    while r-l>1:
        if A[i]*A[(l+r)//2]<V:
            l = (l+r)//2
        else:
            r = (l+r)//2
    return r

"""


N = input()
N = N[::-1]

count = 0
flag = False
for i in range(len(N)):
    s = int(N[i])
    if not flag:

        if s>5:
            count += 10-s
            flag = True
        elif s==5:
            count += 5
            if i<len(N)-1:
                t = int(N[i+1])
                if t>=5:
                    flag=True
        else:
            count += s
            flag=False
    else:

        if s+1>=10:
            
            
            flag = True
        elif s+1>5:
            count += 10-s-1
            
            
            flag = True
        elif s+1==5:
            count += 5
            if i<len(N)-1:
                t = int(N[i+1])
                if t>=5:
                    flag=True
                else:
                    flag=False
            else:
                flag=False

        else:
            count += s+1

            flag=False

if flag:
    count+=1

print(count)

Solution (Python):
n = list(map(int, list(input())))
n.reverse()
n.append(0)
l = len(n)
cnt = 0
for i in range(l):
    if n[i] <= 4:
        cnt += n[i]
    elif n[i] == 5:
        if n[i+1] >=5:
            cnt += 10-n[i]
            n[i+1] += 1
        else:
            cnt += n[i]
    else:
        cnt += 10-n[i]
        n[i+1] += 1
print(cnt)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


def main():
    S = input().strip()

    ans = 0
    S_rev = S[::-1]
    kuriage = 0
    for i, s in enumerate(S_rev):
        if i == len(S) - 1:
            if int(s) == 9 and kuriage == 1:
                ans += 1
            elif int(s) + kuriage >= 6:
                ans += 10 - int(s) - kuriage + 1
            else:
                ans += int(s) + kuriage
        else:
            if int(s) == 9 and kuriage == 1:
                continue
            if int(s) + kuriage >= 6:
                ans += 10 - int(s) - kuriage
                kuriage = 1
            elif int(s) + kuriage == 5 and int(S_rev[i + 1]) >= 5:
                ans += 10 - int(s) - kuriage
                kuriage = 1
            else:
                ans += int(s) + kuriage
                kuriage = 0
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
N=input()
a=0
k=0
for i in range(len(N)-1,-1,-1):
    n=int(N[i])+k
    if n<6:
        a+=n
        if n==5 and i!=0 and int(N[i-1])>4:
            k=1
        else:
            k=0
    elif n!=10:
        a+=10-n
        k=1
    else:
        a+=0
        k=1
a+=k
print(a)

Solution (Python):
ans=0
s=[int(i) for i in input()[::-1]]+[0]
i=0
while i<len(s):
    t=s[i]
    if t<=4:
        ans+=t
        i+=1
    elif t==5:
        if s[i+1]>=5:
            ans+=6
            i+=1
            t=s[i]
            while t>=4:
                if t==4:
                    if s[i+1]<5:
                        ans+=4
                        i+=1
                        break
                ans+=9-t
                i+=1
                t=s[i]
        else:
            ans+=5
            i+=1
    elif t>5:
        ans+=11-t
        i+=1
        t=s[i]
        while t>=4:
            if t==4:
                if s[i+1]<5:
                        ans+=4
                        i+=1
                        break
            ans+=9-t
            i+=1
            t=s[i]
print(ans)

Solution (Python):
S = input()[::-1]
lenS = len(S)
S = list(map(int, list(S)))

cnt = 0
mod = 0

for i in range(lenS - 1):
    v = int(S[i]) + mod
    if v < 5:
        cnt += v
        mod = 0
    elif v == 5:
        cnt += 5
        if S[i + 1] >= 5:
            mod = 1
        else:
            mod = 0
    elif v > 5:
        cnt += 10 - v
        mod = 1

ss = S[-1] + mod
if ss <= 5:
    cnt += ss
    mod = 0
else:
    cnt += 10 - ss
    mod = 1

print(cnt if mod == 0 else cnt + 1)

Solution (Python):
M = [int(i) for i in input()][::-1] + [0]
ans = 0
flag = 0
for i in range(len(M)):
    if M[i] > 5:
        if M[i-1] == 5:
            ans += 10-M[i]+4
            M[i-1] = 0
            M[i] = 0
            M[i+1] += 1
        else:
            ans += 10-M[i]
            M[i+1] += 1
            M[i] = 0
    elif M[i] == 5 and M[i-1] == 5:
        M[i-1] = 0
        M[i] = 0
        M[i+1] += 1
        ans += 9
print(sum(M)+ans)

Solution (Python):
def main():
    N = input()
    r = 0
    x = 0
    n = len(N)
    p = 0
    o = 0
    for i in N:
        j = int(i)
        np = min(p + j, o + j)
        if o != 0:
            no = min(o + 9 - j, p + 11 - j)
        else:
            no = p + 11 - j
        p = np
        o = no
    print(min(p,o))
main()

Solution (Python):
l = list(input())
l2 = []
for i in l[::-1]:
    l2.append(int(i))

l2.append(0)
count = 0

for i in range(len(l2)):
    if l2[i] <= 4:
        count += l2[i]
    elif l2[i] == 5:
        if l2[i+1] <=4:
            count += 5
        else:
            count += 5
            l2[i+1] += 1
    else:
        count += 10-l2[i]
        l2[i+1] += 1
print(count)

Solution (Python):
n = "0"+input()
a = [0, 0]
flag = True

for i in reversed(n):
    b = int(i)
    c=[0,0]
    if flag:
        flag = False
        c[0] = b
        c[1] = 10 - b
    else:
        c[0] = min(a[0] + b, a[1] + b + 1)
        c[1] = min(a[0] + 10 - b, a[1] + 10 - b - 1)
    a=c[:]
print(min(a))

Solution (Python):
nl = list(input())
n = len(nl)

ans = 0
dp = [[0, 0] for i in range(n+1)]
for idx, i in enumerate(nl[::-1]):
    i = int(i)
    if idx == 0:
        dp[1][0] = dp[idx][0] + i
        dp[1][1] = dp[idx][0] + 10 - i
    else:
        dp[idx+1][0] = min(dp[idx][0] + i, dp[idx][1] + i + 1)
        dp[idx+1][1] = min(dp[idx][0] + 10 - i, dp[idx][1] + 10 - i - 1)
print(min(dp[-1][0], dp[-1][1]+1))

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

daikin = 0
otsuri = 0

cnt = 0
for i in range(len(n)-1, 0, -1):
  if n[i] == 10:
    n[i-1] += 1
    continue

  if n[i] < 5:
    cnt += n[i]
    daikin += n[i]
  elif n[i] == 5:
    if n[i-1] < 5:
      cnt += 5
    else:
      cnt += 5
      n[i-1] += 1
  else:
    cnt += 10 - n[i]
    otsuri
    if i != 0:
      n[i-1] += 1

if n[0] <= 5:
  cnt += n[0]
else:
  cnt += 11 - n[0]

print(cnt)

Solution (Python):
A = input()
S = list(map(int,list(A)))
N = len(S)
output = 0
for i in range(N):
    t = N-1-i
    M = S[t]
    if M>5:
        output += 10-M
        S[t-1]+=1
    elif M==5:
        if S[t-1]>=5:
            output += 10-M
            S[t-1]+=1
        else:
            output+=M
    else:
        output += M
if S[0]>5:
    output+=1
if A=="5":
    output = 5
print(output)

Solution (Python):
N = ['0'] + list(input()) + ['0']

n = len(N)
ans = 0
flag = False
count = 0
for i in range(n - 1, 0, -1):
    s = int(N[i])
    
    if flag:
        s += 1
        N[i] = str(s)
    else:
        pass

    if s <= 5:
        next_ = int(N[i - 1])
        if s == 5 and  next_ >= 5:
            
            ans += s
            flag = True

        else:
            ans += s
            flag = False
    else:
        ans += 10 - s
        flag = True


if flag:
    ans += 1

count = 0
for s in N:
    if s == '5':
        count += 1
    else:
        ans -= max(count -2, 0)
        count = 0


print (ans)

Solution (Python):
a = list(map(int,list(input())))[::-1]
a.append(0)
ans = 0
for i in range(len(a)):
  if a[i] >= 6 or (a[i] == 5 and a[i+1] >= 5):
    a[i] = 10-a[i]
    a[i+1] += 1
print(sum(a))

Solution (Python):
def nasu(N):
  ans = 0
  i = 0
  while i < len(N):
    t = N[i]
    if t < 5 or (t == 5 and i == len(N) - 1) or (t == 5 and N[i + 1] <= 4):
      ans += t
      i += 1
    else:
      ans += 1
      ans += 9 - t
      i += 1
      while i < len(N):
        t = N[i]
        if t < 4 or (t == 4 and i == len(N) - 1) or (t == 4 and N[i + 1] <= 4):
          ans += 1
          break
        else:
          ans += 9 - t
          i += 1
      else:
        ans += 1

  return ans


N = list(map(int, list(input())))

print(nasu(N))

exit()
for i in range(500, 600):
  N = list(map(int, list(str(i))))
  print(i, nasu(N))

Solution (Python):
num = list(input())
num = list(map(int, num))
length = len(num)

ans = 0














for i in reversed(range(1, length)):
    digit = num[i]
    if digit < 5:
        ans += digit
    elif i > 0 and digit == 5 and num[i-1] < 5:
        ans += digit
    else:
        ans += (10 - digit)
        if i > 0:
            num[i-1] += 1
        else:
            ans += 1

digit = num[0]
if digit <= 5:
    ans += digit
else:
    ans += (11 - digit)

print(ans)

Solution (Python):
def f(i):
    while True:
        a[i]=(a[i]+1)%10
        if a[i]==0: i+=1
        else: break

s=input()
a=[]
for i in range(len(s)-1,-1,-1):
    a.append(int(s[i]))
a.append(0)

ans=0
for i in range(len(a)-1):
    if a[i]<5: ans+=a[i]
    elif a[i]==5:
        if a[i+1]<5: ans+=5
        else:
            ans+=5
            f(i+1)

    else:
        ans+=10-a[i]
        f(i+1)

if a[-1]!=0: ans+=1

print(ans)

Solution (Python):
import bisect

S=input()
dig=len(S)

ans=0

b=0
for i in range(dig):
    a=int(S[dig-i-1])+b
    if a<=4:
        ans+=a
        b=0
    elif a>=6:
        ans+=(10-a)
        b=1
    else:
        if i==dig-1:
            ans+=5
            b=0
        else:
            if int(S[dig-i-2])>=5:
                b=1
            else:
                b=0
            ans+=5


        

if b==1:
    ans+=1

    
print(ans)

Solution (Python):
def main():
    N = input()
    ans = 0
    carry = 0
    prev_5 = False
    for c in N[::-1]:
        if prev_5 and int(c) >= 5:
            carry = 1
        if int(c) + carry <= 4:
            ans += int(c) + carry
            carry = 0
            prev_5 = False
        elif int(c) + carry == 5:
            ans += 10 - (int(c) + carry)
            carry = 0
            prev_5 = True
        else:
            ans += 10 - (int(c) + carry)
            carry = 1
            prev_5 = False
    if carry == 1:
        ans += 1
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
a=input()
N=len(a)
count=0
bonus=0
for i in range(N-1,-1,-1):
    if int(a[i])+bonus>=6 or (int(a[i])+bonus==5 and (i!=0 and int(a[i-1])>=5)):
        count+=(10-int(a[i])-bonus)
        if i!=0:
            bonus=1
        else:
            count+=1
    else:
        count+=int(a[i])+bonus
        bonus=0
print(count)

Solution (Python):
import sys
MAX_INT = int(10e15)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

s = S()
t = s[::-1]
N = len(s)

dp = [[MAX_INT]*2 for i in range(N+1)]
dp[0][0] = 0
for i in range(N):
  num = int(t[i])
  dp[i+1][0] = min(dp[i][0] + num, dp[i][1] + num +1)
  dp[i+1][1] = min(dp[i][0] + 10-num, dp[i][1] + 10-num-1)
print(min(dp[-1][0], dp[-1][1]+1))

Solution (Python):

def solve():
    N = input()[::-1]+'00'
    res = 0
    c = 0
    for i in range(len(N)-1):
        r = int(N[i])+c
        c = r//10
        r %= 10
        r_ = (int(N[i+1])+c)%10
        if r < 5 or (r==5 and r_ < 5):
            res += r
        else:
            res += 10-r
            c += 1
    return str(res)

if __name__ == '__main__':
    print(solve())

Solution (Python):
N = list(map(int,input()))
L = len(N)
ans0 = 0
ans1 = 100
for i in range(L):
    k = N[-i-1]
    ans0, ans1 = (min(ans0+k, ans1 + k+1),
                  min(ans0 + 10-k, ans1 + 9-k))
ans = min(ans0, ans1+1)
print(ans)

Solution (Python):

N = input()

ans = 0

dp = [0,2]

for i in range(len(N)):

    ndp = [float("inf"),float("inf")]

    num = int(N[-1-i])

    ndp[0] = min( dp[0] + num , dp[1] + num )
    ndp[1] = min( dp[0] + 2 + 9 - num , dp[1] + 9 - num)

    dp = ndp

    

print (min(dp))

Solution (Python):
n = input()
l = len(n)
now = 0
m = 1
p = 0
for i in range(l):
  d = int(n[i])
  M = [m,p]
  P = [m,p]
  M[0] += 9-d
  P[0] += 10-d
  M[1] += 1+d
  P[1] += d
  m = min(M)
  p = min(P)
  
m += 1
print(min(m,p))

Solution (Python):
from sys import stdout
printn = lambda x: stdout.write(str(x))
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True 
BIG = 999999999
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

n = ins()
nrev = n[::-1]
m = len(n)
l = [0]*(m+1)
r = [0]*(m+1)
r[m] = 1
for k in range(m,0,-1):
    p = int(nrev[k-1])
    l[k-1] = min(l[k]+p, r[k]+10-p)
    if k>0:
        r[k-1] = min(l[k]+p+1, r[k]+10-p-1)
print(l[0])

Solution (Python):
n = input()

a = n[::-1] + "0"

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


for i in range(1, len(a)):
    x = int(a[i])
    y = x + 1
    aa = min([x + prev_a, y + prev_b])
    bb = min([(10 - x) + prev_a, (10 - y) + prev_b])
    prev_a = aa
    prev_b = bb

print(min(aa, bb))

Solution (Python):
n = str(input())
l = len(n)
c = 0
n = "00"+ n
dnow = int(n[l+1])
for i in range(l+1):
  dnext = int(n[l-i])
  if  dnow <= 4:
    c += dnow
  elif dnow == 5:
    if dnext <= 4:
      c += 5
    else:
      dnext += 1
      c += 5
  elif dnow == 10:
    dnext += 1
  else:
    c += 10 - dnow
    dnext += 1
  dnow = dnext
print(c)

Solution (Python):


stdin = open(0)
N = [int(x) for x in stdin.read().rstrip()]


def solve(N):
    L = len(N)
    
    dp = 0, 1
    for n in N:
        a = min(dp[0] + n, dp[1] + 10 - n)
        b = min(dp[0] + n + 1, dp[1] + 10 - (n + 1))
        dp = a, b
    return dp[0]


print(solve(N))

Solution (Python):
n = input()[::-1]
cnt = 0
flag = 0
for i in range(len(n)-1):
    if int(n[i])+flag < 5:
        cnt += int(n[i])+flag
        flag = 0
    elif int(n[i])+flag > 5:
        cnt += (10-int(n[i])-flag)
        flag = 1
    else:
        if int(n[i+1]) >= 5:
            cnt += (10 - int(n[i])-flag)
            flag = 1
        else:
            cnt += (int(n[i]) + flag)
            flag = 0
if int(n[-1])+flag > 5:
    cnt += (11 - int(n[-1])-flag)
else:
    cnt += int(n[-1])+flag
print(cnt)

Solution (Python):
n=input()
keta=[0,0]
for i in range(len(n)):
	keta.append(int(n[i]))
ans=0
for i in range(len(keta)-1):
	if keta[-i-2]>=5:
		if keta[-i-1]>=5:
			ans+=10-keta[-i-1]
			keta[-i-2]+=1
		else:
			ans+=keta[-i-1]
	else:
		if keta[-i-1]>=6:
			ans+=10-keta[-i-1]
			keta[-i-2]+=1
		else:
			ans+=keta[-i-1]
print(ans)

Solution (Python):
s = input().strip()
s = s[::-1]
r = 0
b = 0
N = len(s)
dp1 = [0] * (N+1)
dp2 = [0] * (N+1)
dp1[1] = int(s[0])
dp2[1] = 11 - int(s[0])
for i, c in enumerate(s[1:]):
    i += 1
    c = int(c)
    dp1[i+1] = min(dp1[i] + c, dp2[i] + c)
    dp2[i+1] = min(dp1[i] + 11 - c, dp2[i] + 9 - c)



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

Solution (Python):
n=input()[::-1]
ans=0
flag=0
for i in range(len(n)):
    a=int(n[i])
    a+=flag
    if a==5 and i+1<len(n) and int(n[i+1])>=5:
        ans+=10-a
        flag=1
    elif a<=5:
        ans+=a
        flag=0
    else:
        ans+=10-a
        flag=1
ans+=flag
print(ans)

Solution (Python):
Z = [0,1,2,3,4,5,6,7,8,9,10]
C = [0,1,2,3,4,5,6,7,8,9]
X = [0,1,2,3,4,5,6,7,8,9]

def solve_dp(s):
    n = len(s)
    s = [int(c) for c in s]
    inf = 10**9

    dp = [[inf] * 2 for _ in range(n)]

    d0 = s[0]
    dp[0][0] = Z[d0]
    dp[0][1] = Z[d0+1]

    for i in range(1, n):
        for j in range(2):
            d = s[i] + j
            for k in range(10):
                if d + C[k] >= 10:
                    dp[i][j] = min(dp[i][j], dp[i-1][1] + X[k] + Z[d + C[k] - 10])
                else:
                    dp[i][j] = min(dp[i][j], dp[i-1][0] + X[k] + Z[d + C[k]])

    return dp[n-1][0]

if __name__ == '__main__':
    s = input()
    s = "0"+ s
    print(solve_dp(s))

Solution (Python):
s=input()
state=0
mai=0
for i in range(len(s)):
  k=int(s[len(s)-1-i])
  if state==1:
    k=k+1
    if k<0:
      k=10+k
  elif state==2:
    if k>=5:
      k=k+1
      if k<0:
        k=10+k
  if k>5:
    mai+=10-k
    state=1
  elif k<5:
    mai+=k
    state=0
  elif k==5:
    state=2
    mai+=k
if k>=6:
  mai+=1
print(mai)

Solution (Python):


import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
read = sys.stdin.read


n = "".join(["0",input()])

ov = 10**9
eq = 0

L = len(n)-1
for i,x in enumerate(n):
    x = int(x)
    ov,eq = min(ov+9-x,eq+x+1), min(ov+10-x, eq+x)


print(eq)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): 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 LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    s=SI()
    n=len(s)
    ans=0
    nxt=0
    for i in range(n-1,-1,-1):
        now=int(s[i])+nxt
        if now>5 or (i>0 and now==5 and int(s[i-1])>4):
            nxt=1
            ans+=10-now
        else:
            ans += now
            nxt = 0
    ans+=nxt
    print(ans)

main()

Solution (Python):
n_s = input()
n_s = n_s[::-1]


pay = 0
back = 0
flag = 0

dp = [[0,0] for _ in range(len(n_s) + 1)]

dp[0][0] = int(n_s[0])
dp[0][1] = 10 - int(n_s[0])

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

print(min(dp[-1]))

Solution (Python):
N=list(map(int,list("0"+input())))

ans = 0
while N:
    n = N.pop()
    if n>=10:
        N[-1] += 1
        n -= 10
    if n>=6:
        ans += 10-n
        N[-1] += 1
    elif n==5:
        if N[-1] >= 5:
            N[-1] += 1
            ans += 10-n
        else:
            ans += n
    else:
        ans += n
print(ans)

Solution (Python):
S = input()

ans=0
num = [int(i) for i in S]
num.reverse()

def judge(keta):
    if keta >= len(S):
        return true
    tmp = num[keta]
    if tmp < 5:
        return false
    elif tmp > 5:
        num[keta] = 10 - num[keta]
        num[keta+1] += 1
        return true
    else:
        if judge(keta):
            pass
        
for i in range(len(S)-1):
    tmp = num[i]
    if tmp < 5:
        pass
    elif tmp > 5:
        num[i] = 10-num[i]
        num[i+1] +=1
    else:
        if num[i+1] >= 5:
            num[i] = 10-num[i]
            num[i+1] +=1
        else:
            pass

last = len(S)-1
ans = 0
if num[last] > 5:
    num[last] = 10 - num[last]
    ans =1
print(sum(num)+ans)

Solution (Python):
import sys
readline = sys.stdin.readline

N = readline().rstrip()
N = list(map(int, N))
L = len(N)

total = 0

a = [0] * L

for i in range(L - 1, -1, -1):
    k = N[i]
    if k <= 4:
        a[i] = 0 
        total += k
    elif k >= 6:
        a[i] = 1 
        total += 10 - k
        if i == 0:
            total += 1
        else:
            N[i - 1] += 1
    else:
        if i == 0:
            a[i] = 0
            total += k
        else:
            if N[i - 1] <= 4:
                a[i] = 0
                total += k
            else:
                a[i] = 1
                total += 10 - k
                N[i - 1] += 1

print(total)

Solution (Python):
n = input()
digit = [int(n[-(i + 1)]) for i in range(len(n))]
digit.append(0)

ans = 0
for i in range(len(digit)):
  if digit[i] > 5 or (digit[i] == 5 and digit[i + 1] >= 5):
    ans += 10 - digit[i]
    digit[i] = 0
    digit[i + 1] += 1
  else:
    ans += digit[i]
    digit[i] = 0

print(ans)

Solution (Python):
N = list(map(int, list(input())))[::-1]
kuriage, up5, ans = False, False, 0

for i in range(len(N)):
    if up5:
        if N[i] < 5:
            ans += N[i] + 5
        else:
            ans += 14 - N[i]
            kuriage = True
        up5 = False
    else:
        if kuriage:
            if N[i] == 4:
                up5, kuriage = True, False
            elif N[i] + 1 < 9 - N[i]:
                ans += N[i] + 1
                kuriage = False
            else:
                ans += 9 - N[i]
        else:
            if N[i] == 5:
                up5 = True
            elif N[i] < 10 - N[i]:
                ans += N[i]
            else:
                ans += 10 - N[i]
                kuriage = True

if up5:
    ans += 5
if kuriage:
    ans += 1
print(ans)

Solution (Python):
import sys





from collections import defaultdict





def main():
	li=[0]+list(map(int,input()))
	n=len(li)
	
	ans=0
	
	for i in range(n):
		a=li[n-1-i]
		if 6<=a:
			ans+=10-a
			li[n-2-i]+=1
		elif a==5:
			if li[n-2-i]<=4:
				ans+=a
			else:
				ans += 10 - a
				li[n - 2 - i] += 1
		else:
			ans+=a
	print(ans)
	
	
if __name__ == "__main__":
	main()

Solution (Python):
N = input()
L = len(N)
ans = 0
t = 0
for i in range(L-1,-1,-1):
    n = int(N[i])+t
    if n == 5:
        if i > 0 and int(N[i-1]) >= 5:
            ans += 10-n
            t = 1
            continue
    if n <= 5:
        ans += n
        t = 0
    else:
        t = 1
        ans += 10-n
if t==1:
    ans += 1
print(ans)

Solution (Python):
n = input()
k = 0
pay = 0
n_list = []
for i in n[::-1]:
    n_list.append(int(i))
n_list.append(0)
ans = 0
for i in range(len(n_list) - 1):
    num = n_list[i]
    if num < 5:
        ans += num
    elif (num <= 5 and n_list[i + 1] < 5):
        ans += num
    else:
        n_list[i + 1] += 1
        ans += 10 - num

ans += n_list[-1]
print(ans)

Solution (Python):
def main():
    N = [0] + [int(i) for i in input()]
    r = 0
    c = 0
    f = 0
    for i in reversed(N):
        if f:
            if i + c >= 5:
                c += 1
                r += 5
            else:
                r += 5
            f = 0
        i += c
        c = 0
        if i < 5:
            r += i
        elif i == 5:
            f += 1
        else:
            c += 1
            r += 10 - i
    print(r)



main()

Solution (Python):
n = input()
nl = [0]
for i in range(len(n)):
    nl.append(int(n[i]))
y = 0
for i in range(1, len(nl)+1):
    if nl[-i] <= 4 or (nl[-i] == 5 and nl[-(i+1)] <= 4):
        y += nl[-i]
    else:
        y += (10-nl[-i])
        nl[-(i+1)] += 1
print(y)

Solution (Python):
S = input()
L = len(S)

INF = float('inf')
dp = [[INF]*2 for _ in range(L)]
dp[0][0] = int(S[-1])
dp[0][1] = 10-int(S[-1])
S = S[::-1]
for i,c in enumerate(S[1:]):
    c = int(c)
    d = 10-c
    dp[i+1][0] = min(dp[i+1][0], dp[i][0]+c)
    dp[i+1][1] = min(dp[i+1][1], dp[i][0]+d)
    c += 1
    d -= 1
    if c==10:
        dp[i+1][1] = min(dp[i+1][1], dp[i][1])
    else:
        dp[i+1][0] = min(dp[i+1][0], dp[i][1]+c)
        dp[i+1][1] = min(dp[i+1][1], dp[i][1]+d)
print(min(dp[-1][0], dp[-1][1]+1))

Solution (Python):
N=input()
n=len(N)
x=0
flag=0

M=[0]*(n+1)
for i in range(n):
  M[i]=int(N[n-1-i])

for i in range(n+1):
  
  m=M[i]
  if m==5:
    x+=5
    flag=1
  elif m<5:
    x+=m
    flag=0
  else:
    x+=(10-m)
    flag=2
  if flag==2:
    M[i+1]+=1
  elif flag==1:
    if M[i+1]>=5:
      M[i+1]+=1
print(x)

Solution (Python):
def main():
    N = input()
    reversed_N = ("0" + N)[::-1]

    kuriage = 0
    ans = 0
    for i in range(len(reversed_N)):
        n = int(reversed_N[i]) + kuriage

        if n < 5:
            ans += n
            kuriage = 0
        elif n == 5 and i < len(reversed_N) - 1:
            next_n = int(reversed_N[i + 1])
            if next_n < 5:
                ans += n
                kuriage = 0
            else:
                ans += 10 - n
                kuriage = 1
        else:
            ans += 10 - n
            kuriage = 1

    print(ans)


main()

Solution (Python):
S = "000"+input()
S = S[::-1]
N = len(S)

dp = [[0] * 2 for _ in range(N + 5)]

ans = 0
up = 0
for i, s in enumerate(S):
    x = int(s) + up
    if x == 10:
        continue
    y = 10 - x
    if x < y:
        ans += x
        up = 0
    elif x > y:
        ans += y
        up = 1
    else:
        ans += x
        if int(S[i + 1]) >= 5:
            up = 1
        else:
            up = 0

print(ans)

Solution (Python):
N = list(map(int, input()))
ret = 0
N.reverse()
N.append(0)
N.append(0)
N.append(0)
L = len(N)
for i in range(L-1):
    if N[i] == 5:
        if N[i+1] >= 5:
            ret += 1
            N[i] += 1
    if N[i] > 5:
        ret += 10 - N[i]
        N[i+1] += 1
        if N[i+1] > 9:
            N[i+1] -= 10
            N[i+2] += 1            
    else:
        ret += N[i]
print(ret)

Solution (Python):
n = list(map(int, reversed(list(input()))))

a = 0
b = 9999
for x in n:
  temp_a = min(a + x, b + x)
  b = min(a + 10 - x + 1, b + 10 - x - 1)
  a = temp_a

print(min(a, b))

Solution (Python):
import collections

def solution():
    n = [int(x) for x in input().strip()]
    res = 0
    carry = 0
    for i in range(len(n)-1, -1, -1):
        cur = n[i] + carry
        if cur == 10:
            carry = 1
        elif 6 <= cur <= 9:
            res += 10-cur
            carry = 1
        elif cur == 5:
            if i > 0 and n[i-1] >= 5:
                carry = 1
                res += 5
            else:
                carry = 0
                res += 5
        else:
            carry = 0
            res += cur
    print(res+carry)

def main():    
    
    for _ in range(1):
        solution()
    

main()

Solution (Python):
N = list(map(int,list(input())))
give = [""]
oturi = [""]
N.reverse()
N.append(0)
for i in range(len(N)):
    if N[i] > 5 or (N[i] == 5 and N[i+1] >= 5):
        N[i+1] += 1
        oturi .append(str(10-N[i]))
        give.append("0")
    else:
        give.append(str(N[i]))
        oturi.append("0")
give.reverse()
oturi.reverse()
give = ("".join(give))
oturi = ("".join(oturi))

ans = 0
for i in give:
    ans += int(i)
for i in oturi:
    ans += int(i)

print(ans)

Solution (Python):
Z = [0,1,2,3,4,5,5,4,3,2,1,1]
C = [0,1,2,5,6,7]
X = [0,1,2,1,2,3]
C=[0,1,2,3,4,5,6,7,8,9]
X=[0,1,2,3,4,5,6,7,8,9]
def solve_dp(s):
    n = len(s)
    s = [int(c) for c in s]
    inf = 10**9

    dp = [[inf] * 2 for _ in range(n)]

    d0 = s[0]
    dp[0][0] = Z[d0]
    dp[0][1] = Z[d0+1]

    for i in range(1, n):
        for j in range(2):
            d = s[i] + j
            for k in range(10):
                if d + C[k] >= 10:
                    dp[i][j] = min(dp[i][j], dp[i-1][1] + X[k] + Z[d + C[k] - 10])
                else:
                    dp[i][j] = min(dp[i][j], dp[i-1][0] + X[k] + Z[d + C[k]])

    return dp[n-1][0]

if __name__ == '__main__':
    s = input()
    print(solve_dp(s))

Solution (Python):
N = input().strip()


d = int(N[-1])
count_p = d
count_m = 10 - d

for i in range(len(N) - 2, -1, -1):
	d = int(N[i])
	
	new_count_p = min(count_p + d, count_m + (d + 1))
	new_count_m = min(count_p + (10 - d), count_m + (10 - (d + 1)))
	
	count_p = new_count_p
	count_m = new_count_m

count_m += 1

print(min(count_p, count_m))

Solution (Python):
n = input()
precise = 0
ok_over = 1
ok_prec = 0
for d in n:
    d = int(d)
    new_over = min(precise + d + 1, ok_over + d + 2, ok_over + 9 - d, ok_prec + d + 1)
    new_prec = min(ok_over + d + 1, ok_prec + d)
    precise += d
    ok_over = new_over
    ok_prec = new_prec
ok_over += 1
print(min(precise, ok_over, ok_prec))