Buggy python submission (verdict: Runtime Error):
#!/usr/bin/env python3

import sys
import math
import itertools

def main():
    ns = list(map(int, sys.stdin.readline().strip()))
    ns.append(0)
    ns.reverse()

    ans = 0
    lastpay = 0

    for i in range(len(ns)):
        if lastpay == 0:
            if ns[i] <= 4 or (ns[i] == 5 and ns[i + 1] <= 4):
                ans += ns[i]
                lastpay = 0
            else:
                ans += 1 + (10 - ns[i])
                lastpay = 1
        else:
            if lastpay + ns[i] <= 4 or (lastpay + ns[i] == 5 and ns[i + 1] <= 4):
                ans += ns[i]
                lastpay = 0
            else:
                ans += 1 - 1 + (9 - ns[i])
                lastpay = 1
    #print(''.join(map(str, reversed(pay))))
    #print(''.join(map(str, reversed(charge))))
    #assert (sum(pay) + sum(charge)) == ans
    print(ans)

if __name__ == '__main__':
    main()

Fixed submission:
#!/usr/bin/env python3

import sys
import math
import itertools

def main():
    ns = list(map(int, sys.stdin.readline().strip()))
    ns.reverse()
    ns.append(0)

    ans = 0
    lastpay = 0

    for i in range(len(ns)):
        if lastpay == 0:
            if ns[i] <= 4 or (ns[i] == 5 and ns[i + 1] <= 4):
                ans += ns[i]
                lastpay = 0
            else:
                ans += 1 + (10 - ns[i])
                lastpay = 1
        else:
            if lastpay + ns[i] <= 4 or (lastpay + ns[i] == 5 and ns[i + 1] <= 4):
                ans += ns[i]
                lastpay = 0
            else:
                ans += 1 - 1 + (9 - ns[i])
                lastpay = 1
    print(ans)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
#!/usr/bin/env python3

import sys
import math
import itertools

def main():
    ns = list(map(int, sys.stdin.readline().strip()))
    ns.append(0)
    ns.reverse()

    pay = [0] * (len(ns) + 1)
    charge = [0] * (len(ns) + 1)
    for i in range(len(ns)):
        if pay[i] == 0:
            if ns[i] <= 4 or (ns[i] == 5 and ns[i + 1] <= 4):
                pay[i] = ns[i]
                charge[i] = 0
            else:
                pay[i] = 0
                pay[i + 1] = 1
                charge[i] = 10 - ns[i]
        else:
            if pay[i] + ns[i] <= 4 or (pay[i] + ns[i] == 5 and ns[i + 1] <= 4):
                pay[i] += ns[i]
                charge[i] = 0
            else:
                pay[i] = 0
                pay[i + 1] = 1
                charge[i] = 9 - ns[i]
    #print(''.join(map(str, reversed(pay))))
    #print(''.join(map(str, reversed(charge))))
    print(sum(pay) + sum(charge))

if __name__ == '__main__':
    main()

Fixed submission:
#!/usr/bin/env python3

import sys
import math
import itertools

def main():
    ns = list(map(int, sys.stdin.readline().strip()))
    ns.reverse()
    ns.append(0)

    ans = 0
    lastpay = 0

    for i in range(len(ns)):
        if lastpay == 0:
            if ns[i] <= 4 or (ns[i] == 5 and ns[i + 1] <= 4):
                ans += ns[i]
                lastpay = 0
            else:
                ans += 1 + (10 - ns[i])
                lastpay = 1
        else:
            if lastpay + ns[i] <= 4 or (lastpay + ns[i] == 5 and ns[i + 1] <= 4):
                ans += ns[i]
                lastpay = 0
            else:
                ans += 1 - 1 + (9 - ns[i])
                lastpay = 1
    print(ans)

if __name__ == '__main__':
    main()