Problem p03008 (python)

Fixed submission:
def examD():
    N = I()
    g, s, c = [0]*2, [0]*2, [0]*2
    for i in range(2):
        g[i],s[i],c[i] = LI()
    dp1 = [0]*(N+1)
    v1 = [g[1]-g[0],s[1]-s[0],c[1]-c[0]]
    w1 = [g[0],s[0],c[0]]
    v2 = [g[0]-g[1],s[0]-s[1],c[0]-c[1]]
    w2 = [g[1],s[1],c[1]]
    for i in range(N):
        if i+w1[0]<=N:
            dp1[i+w1[0]] = max(dp1[i+w1[0]],dp1[i]+v1[0])
        if i+w1[1]<=N:
            dp1[i+w1[1]] = max(dp1[i+w1[1]],dp1[i]+v1[1])
        if i+w1[2]<=N:
            dp1[i+w1[2]] = max(dp1[i+w1[2]],dp1[i]+v1[2])
    newN = max(dp1)+N
#    print(newN)
    dp2 = [0]*(newN+1)
    for i in range(newN):
        if v2[0]>0 and i+w2[0]<=newN:
            dp2[i+w2[0]] = max(dp2[i+w2[0]],dp2[i]+v2[0])
        if v2[1]>0 and i+w2[1]<=newN:
            dp2[i+w2[1]] = max(dp2[i+w2[1]],dp2[i]+v2[1])
        if v2[2]>0 and i+w2[2]<=newN:
            dp2[i+w2[2]] = max(dp2[i+w2[2]],dp2[i]+v2[2])
    ans = dp2[newN]+newN
    print(ans)

from string import digits
import sys,copy,bisect,itertools,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examD()

Buggy submission (verdict: Runtime Error):
def examA():
    N = I()
    X = [LI()for _ in range(N)]
    l = 0; r = inf
    while(r-l>_ep):
        now = (l+r)/2
        x_l = -inf; x_r = inf
        y_l = -inf; y_r = inf
        for x,y,c in X:
            move = now / c
            x_l = max(x_l,x-move)
            x_r = min(x_r,x+move)
            y_l = max(y_l,y-move)
            y_r = min(y_r,y+move)
        if x_l+_ep<x_r and y_l+_ep<y_r:
            r = now
        else:
            l = now
    ans = r
    print(ans)
    return

def examB():
    N = I()
    C = LI()
    C.sort()
    base = pow(2,2*N-1,mod)
    inv = pow(2,mod-2,mod)
    ans = 0
    for i in range(N):
        cur = C[i]*base*(1+(N-1-i)*inv)
        ans += cur
        ans %= mod
    print(ans)
    return

def examC():
    N = I()
    Ag,As,Ab = LI()
    Bg,Bs,Bb = LI()
    dp = [i for i in range(N+1)]
    for i in range(N):
        if i+Ag<=N:
            dp[i+Ag] = max(dp[i+Ag],dp[i]+Bg)
        if i+As<=N:
            dp[i+As] = max(dp[i+As],dp[i]+Bs)
        if i+Ab<=N:
            dp[i+Ab] = max(dp[i+Ab],dp[i]+Bb)
    ne_N = max(dp)
    #print(dp)
    dp2 = [i for i in range(ne_N+1)]
    for i in range(ne_N):
        if dp2[i]<0:
            continue
        if i+Bg<=ne_N:
            dp2[i+Bg] = max(dp2[i+Bg],dp2[i]+Ag)
        if i+Bs<=ne_N:
            dp2[i+As] = max(dp2[i+Bs],dp2[i]+As)
        if i+Bb<=ne_N:
            dp2[i+Bb] = max(dp2[i+Bb],dp2[i]+Ab)
    ans = max(dp2)
    print(ans)
    return


import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def I(): return int(readline())
def LI(): return list(map(int,readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**9
_ep = 10**(-8)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**7)

if __name__ == '__main__':
    examC()

"""
142
12 9 1445 0 1
asd dfg hj o o
aidn
"""