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] print res return res
def show(n,c,w,res) : print 'the bigest value is :',res[n][c] x = [False 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] = True j -= w[i-1] print 'the things we choose are :' for i in range(n) : if x[i] : print 'is the',i print ' '
if __name__ = '__main__' : n = 5 c = 10 w = [2,3,8,5,5] v = [10,34,54,12,64] res = bag(n,c,w,v) show(n,c,w,res)