Pagini recente » Cod sursa (job #474185) | Cod sursa (job #3265100) | Cod sursa (job #2533207)
file = open("rucsac.in", "r")
N, G = map(int, file.readline().split())
v = []
for _ in range(N):
c, p = map(int, file.readline().split())
v.append([c, p])
pd = [[0 for _ in range(G+1)] for __ in range(2)]
for n in range(N):
for g in range(1, G+1):
pd[n%2][g] = pd[n%2][g-1]
if n > 0:
pd[n%2][g] = pd[(n-1)%2][g]
if g >= v[n][0]:
pd[n%2][g] = max(pd[n%2][g], v[n][1]+pd[(n-1)%2][g-v[n][0]])
file.close()
file = open("rucsac.out", "w")
file.write(str(pd[(N-1)%2][G]))
file.close()