Skip to content

Instantly share code, notes, and snippets.

@markroxor
Last active October 1, 2015 12:50
Show Gist options
  • Save markroxor/a6bd5203a4638baabdf4 to your computer and use it in GitHub Desktop.
Save markroxor/a6bd5203a4638baabdf4 to your computer and use it in GitHub Desktop.
spoj problem?
from sys import stdin
nex = iter(map(int,stdin.read().split())).__next__
def knapSack(wt,val,W,N):
dp = [[0]*(5+W)]*(5+N)
for n in range(N+1):
for w in range(W+1):
if(w==0 or n==0):
dp[n][w] = 0
elif (wt[n-1]<=w):
dp[n][w] = max(dp[n-1][w],val[n-1]+dp[n-1][w-wt[n-1]])
else:
dp[n][w] = dp[n-1][w]
return dp[N][W]
while True:
W = nex()
N = nex()
if not W and not N:
break
val = []
wt = []
for i in range(N):
wt.append(nex())
val.append(nex())
print (knapSack(wt,val,W,N))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment