Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3

def main():
    N = int(input())
    gsba = list(map(int, input().split()))
    gsbb = list(map(int, input().split()))
    
    # ナップザック問題として解く
    w = gsba
    v = [gsbb[i]-gsba[i] for i in range(3)]
    dp = [0]*(N+1)

    for i in range(3):
        for j in range(N+1):
            if j >= w[i]:
                dp[j] = max(dp[j],dp[j-w[i]]+v[i])

    M = dp[-1] + N
    w = gsbb
    v = [gsba[i]-gsbb[i] for i in range(3)]
    dp = [0]*(M+1)

    for i in range(3):
        for j in range(M+1):
            if j >= w[i]:
                dp[j] = max(dp[j],dp[j-w[i]]+v[i])

    print(dp[-1] + M)

if __name__ == "__main__":
    main()

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

def main():
    N = int(input())
    gsba = list(map(int, input().split()))
    gsbb = list(map(int, input().split()))
    
    # ナップザック問題として解く
    w = gsba
    v = [gsbb[i]-gsba[i] for i in range(3)]
    dp = [0]*(N+1)

    for i in range(3):
        for j in range(N+1):
            if j >= w[i]:
                dp[j] = max(dp[j],dp[j-w[i]]+v[i])

    M = dp[-1] + N
    w = gsbb
    v = [gsba[i]-gsbb[i] for i in range(3)]
    dp = [0]*(M+1)

    for i in range(3):
        for j in range(M+1):
            if j >= w[i]:
                dp[j] = max(dp[j],dp[j-w[i]]+v[i])

    print(dp[-1] + M)

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3

def main():
    N = int(input())
    ga, sa, ba = map(int, input().split())
    gb, sb, bb = map(int, input().split())

    ans = 0
    flags = [ga >= gb, sa >= sb, ba >= bb]

    for x in [range(N//ga+1), [0]][flags[0]]:
        for y in [range((N-ga*x)//sa+1), [0]][flags[1]]:
            for z in [range((N-ga*x-sa*y)//ba+1), [0]][flags[2]]:
                M = N + (gb-ga)*x + (sb-sa)*y + (bb-ba)*z# 取引所Bでのどんぐりの数
                for p in [[0], range(M//gb+1)][flags[0]]:
                    for q in [[0], range((M-gb*p)//sb+1)][flags[1]]:
                        for r in [[0], range((M-gb*p-sb*q)//bb+1)][flags[2]]:
                            ans = max(ans, M+(ga-gb)*p+(sa-sb)*q+(ba-bb)*r)
    print(ans)

if __name__ == "__main__":
    main()

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

def main():
    N = int(input())
    gsba = list(map(int, input().split()))
    gsbb = list(map(int, input().split()))
    
    # ナップザック問題として解く
    w = gsba
    v = [gsbb[i]-gsba[i] for i in range(3)]
    dp = [0]*(N+1)

    for i in range(3):
        for j in range(N+1):
            if j >= w[i]:
                dp[j] = max(dp[j],dp[j-w[i]]+v[i])

    M = dp[-1] + N
    w = gsbb
    v = [gsba[i]-gsbb[i] for i in range(3)]
    dp = [0]*(M+1)

    for i in range(3):
        for j in range(M+1):
            if j >= w[i]:
                dp[j] = max(dp[j],dp[j-w[i]]+v[i])

    print(dp[-1] + M)

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right

def main():
    N = int(input())
    ga, sa, ba = map(int, input().split())
    gb, sb, bb = map(int, input().split())

    ans = 0
    flags = [ga >= gb, sa >= sb, ba >= bb]

    for x in [range(N//ga+1), [0]][flags[0]]:
        # yl = [range((N-ga*x)//sa+1), [0]][flags[1]]
        for y in [range((N-ga*x)//sa+1), [0]][flags[1]]:
            for z in [range((N-ga*x-sa*y)//ba+1), [0]][flags[2]]:
                M = N + (gb-ga)*x + (sb-sa)*y + (bb-ba)*z# 取引所Bでのどんぐりの数
                for p in [[0], range(M//gb+1)][flags[0]]:
                    for q in [[0], range((M-gb*p)//sb+1)][flags[1]]:
                        for r in [[0], range((M-gb*p-sb*q)//bb+1)][flags[2]]:
                            ans = max(ans, M+(ga-gb)*p+(sa-sb)*q+(ba-bb)*r)
    print(ans)

if __name__ == "__main__":
    main()


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

def main():
    N = int(input())
    gsba = list(map(int, input().split()))
    gsbb = list(map(int, input().split()))
    
    # ナップザック問題として解く
    w = gsba
    v = [gsbb[i]-gsba[i] for i in range(3)]
    dp = [0]*(N+1)

    for i in range(3):
        for j in range(N+1):
            if j >= w[i]:
                dp[j] = max(dp[j],dp[j-w[i]]+v[i])

    M = dp[-1] + N
    w = gsbb
    v = [gsba[i]-gsbb[i] for i in range(3)]
    dp = [0]*(M+1)

    for i in range(3):
        for j in range(M+1):
            if j >= w[i]:
                dp[j] = max(dp[j],dp[j-w[i]]+v[i])

    print(dp[-1] + M)

if __name__ == "__main__":
    main()