Last active
October 1, 2015 12:50
-
-
Save markroxor/a6bd5203a4638baabdf4 to your computer and use it in GitHub Desktop.
spoj problem?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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