Buggy python submission (verdict: Time Limit Exceeded):
def main():
    s = input().rstrip()
    t = "0" + s
    n = len(t)
    dp = [[float("inf")]*2 for _ in range(n+1)]
    dp[0][0] = 0
    for i in range(n):
        for j in range(2):
            x = int(t[n-i-1]) + j
            if x < 10:
                dp[i+1][0] = min(dp[i+1][0], dp[i][j] + x + 0)
            if 0 < x:
                dp[i+1][1] = min(dp[i+1][1], dp[i][j] + 10 - x)
    print(dp[n][0])

if __name__ == "__main__":
    main()


Fixed submission:
def main():
    s = input().rstrip()
    s = "0" + s
    dp = [0, float("inf")]
    for c in s:
        x = int(c)
        dp = [min(dp[0] + x, dp[1] + (10-x)), min(dp[0] + x+1, dp[1] + (10-x) - 1)]
    print(dp[0])

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    s = input().rstrip()
    s = s[::-1] + "0"
    n = len(s)
    dp = [[float("inf")]*2 for _ in range(n+1)]
    dp[0][0] = 0
    for i, c in enumerate(s):
        for j in range(2):
            x = int(c) + j
            for num in range(10):
                ni = i+1
                nj = 0
                rem = num - x
                if rem < 0:
                    nj = 1
                    rem += 10
                dp[ni][nj] = min(dp[ni][nj], dp[i][j] + num + rem)
    print(dp[n][0])

if __name__ == "__main__":
    main()


Fixed submission:
def main():
    s = input().rstrip()
    s = "0" + s
    dp = [0, float("inf")]
    for c in s:
        x = int(c)
        dp = [min(dp[0] + x, dp[1] + (10-x)), min(dp[0] + x+1, dp[1] + (10-x) - 1)]
    print(dp[0])

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    s = input().rstrip()
    s = s[::-1] + "0"
    n = len(s)
    dp = [[10**18]*2 for _ in range(2)]
    dp[0][0] = 0
    for i in range(n):
        for j in range(2):
            x = int(s[i]) + j
            if x < 10:
                dp[(i+1)%2][0] = min(dp[(i+1)%2][0], dp[i%2][j] + x + 0)
            if 0 < x:
                dp[(i+1)%2][1] = min(dp[(i+1)%2][1], dp[i%2][j] + 10 - x)
        for j in range(2):
            dp[i%2][j] = 10**18
    print(dp[n%2][0])

if __name__ == "__main__":
    main()

Fixed submission:
def main():
    s = input().rstrip()
    s = "0" + s
    dp = [0, float("inf")]
    for c in s:
        x = int(c)
        dp = [min(dp[0] + x, dp[1] + (10-x)), min(dp[0] + x+1, dp[1] + (10-x) - 1)]
    print(dp[0])

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    s = input().rstrip()
    s = s[::-1]
    s += "0"
    n = len(s)
    a = [int(c) for c in s]
    dp = [[10**18]*2 for _ in range(2)]
    dp[0][0] = 0
    for i in range(n):
        for j in range(2):
            x = a[i] + j
            if x < 10:
                dp[(i+1)%2][0] = min(dp[(i+1)%2][0], dp[i%2][j] + x + 0)
            if 0 < x:
                dp[(i+1)%2][1] = min(dp[(i+1)%2][1], dp[i%2][j] + 10 - x)
        for j in range(2):
            dp[i%2][j] = 10**18
    print(dp[n%2][0])

if __name__ == "__main__":
    main()

Fixed submission:
def main():
    s = input().rstrip()
    s = "0" + s
    dp = [0, float("inf")]
    for c in s:
        x = int(c)
        dp = [min(dp[0] + x, dp[1] + (10-x)), min(dp[0] + x+1, dp[1] + (10-x) - 1)]
    print(dp[0])

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    s = input().rstrip()
    s = s[::-1]
    s += "0"
    n = len(s)
    a = [int(c) for c in s]
    dp = [[float("inf")]*2 for _ in range(2)]
    dp[0][0] = 0
    for i in range(n):
        for j in range(2):
            x = a[i] + j
            if x < 10:
                dp[(i+1)%2][0] = min(dp[(i+1)%2][0], dp[i%2][j] + x + 0)
            if 0 < x:
                dp[(i+1)%2][1] = min(dp[(i+1)%2][1], dp[i%2][j] + 10 - x)
        for j in range(2):
            dp[i%2][j] = float("inf")
    print(dp[n%2][0])

if __name__ == "__main__":
    main()

Fixed submission:
def main():
    s = input().rstrip()
    s = "0" + s
    dp = [0, float("inf")]
    for c in s:
        x = int(c)
        dp = [min(dp[0] + x, dp[1] + (10-x)), min(dp[0] + x+1, dp[1] + (10-x) - 1)]
    print(dp[0])

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    s = input().rstrip()
    s = s[::-1]
    s += "0"
    n = len(s)
    a = [int(c) for c in s]
    dp = [[float("inf")]*2 for _ in range(n+1)]
    dp[0][0] = 0
    for i in range(n):
        for j in range(2):
            x = a[i] + j
            if x < 10:
                dp[i+1][0] = min(dp[i+1][0], dp[i][j] + x + 0)
            if 0 < x:
                dp[i+1][1] = min(dp[i+1][1], dp[i][j] + 10 - x)
    print(dp[n][0])

if __name__ == "__main__":
    main()

Fixed submission:
def main():
    s = input().rstrip()
    s = "0" + s
    dp = [0, float("inf")]
    for c in s:
        x = int(c)
        dp = [min(dp[0] + x, dp[1] + (10-x)), min(dp[0] + x+1, dp[1] + (10-x) - 1)]
    print(dp[0])

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    s = input().rstrip()
    s = s[::-1]
    s += "0"
    n = len(s)
    a = [int(c) for c in s]
    dp = [[float("inf")]*2 for _ in range(n+1)]
    dp[0][0] = 0
    for i in range(n):
        for j in range(2):
            x = a[i] + j
            for num in range(10):
                ni = i+1
                nj = 0
                rem = num - x
                if rem < 0:
                    nj = 1
                    rem += 10
                dp[ni][nj] = min(dp[ni][nj], dp[i][j] + num + rem)
    print(dp[n][0])

if __name__ == "__main__":
    main()

Fixed submission:
def main():
    s = input().rstrip()
    s = "0" + s
    dp = [0, float("inf")]
    for c in s:
        x = int(c)
        dp = [min(dp[0] + x, dp[1] + (10-x)), min(dp[0] + x+1, dp[1] + (10-x) - 1)]
    print(dp[0])

if __name__ == "__main__":
    main()