来个 Python AC的
def bag(n, c, w, v):
res = [[-1 for j in range(c + 1)] for i in range(n + 1)]
for j in range(c + 1):
res[0][j] = 0
for i in range(1, n + 1):
for j in range(1, c + 1):
res[i][j] = res[i - 1][j]
if j >= w[i - 1] and res[i][j] < res[i - 1][j - w[i - 1]] + v[i - 1]:
res[i][j] = res[i - 1][j - w[i - 1]] + v[i - 1]
return res
def show(n, c, w, res):
x = [0 for i in range(n)]
j = c
for i in range(n,0,-1):
if res[i][j] > res[i - 1][j]:
x[i - 1] = 1
j -= w[i - 1]
for i in range(n):
if x[i]==1:
print(i+1),
if name == 'main':
c = int(raw_input())
w = raw_input()
w = w.split()
for i in range(len(w)):
w[i] = int(w[i])
v = raw_input()
v = v.split()
for i in range(len(v)):
v[i] = int(v[i])
n = len(v)
res = bag(n, c, w, v)
show(n, c, w, res)