// n is the number of items, cap is the maximum capacity
n is the number of items, cap is the maximum capacity
// n is the number of items cap is the maximum capacity
func dpBottomUp(n int, cap int) int {
	memo := make([][]int, n)
	for i := 0; i < n; i++ {
		memo[i] = make([]int, cap+1)
	}
	for j := 1; j <= cap; j++ {
		if ws[0] > j { 
			memo[0][j] = 0
		} else {
			memo[0][j] = vs[0]
		}
	}
	for i := 1; i < n; i++ {
		for j := 1; j <= cap; j++ {
			if ws[i] > j { 
				memo[i][j] = memo[i-1][j]
			} else {
				toInclude := vs[i] + memo[i-1][j-ws[i]]
				notInclude := memo[i-1][j]
				memo[i][j] = max(toInclude, notInclude)
			}
		}
	}
	return memo[n-1][cap]
}
func dpBottomUp ( n int , cap int ) int { memo := make ( [ ] [ ] int , n ) for i := 0 ; i < n ; i ++ { memo [ i ] = make ( [ ] int , cap + 1 ) } for j := 1 ; j <= cap ; j ++ { if ws [ 0 ] > j { memo [ 0 ] [ j ] = 0 } else { memo [ 0 ] [ j ] = vs [ 0 ] } } for i := 1 ; i < n ; i ++ { for j := 1 ; j <= cap ; j ++ { if ws [ i ] > j { memo [ i ] [ j ] = memo [ i - 1 ] [ j ] } else { toInclude := vs [ i ] + memo [ i - 1 ] [ j - ws [ i ] ] notInclude := memo [ i - 1 ] [ j ] memo [ i ] [ j ] = max ( toInclude , notInclude ) } } } return memo [ n - 1 ] [ cap ] }
n is the number of items, cap is the maximum capacity
n is the number of items cap is the maximum capacity
// first row of memo: one item problem // first item is heavier than j // not enough capacity for the last item