// Return the item indices and total value using dynamic programming
Return the item indices and total value using dynamic programming
// Return the item indices and total value using dynamic programming
func dpTopDown(n int, cap int) int {
	if cap == 0 || n == 0 {
		return 0
	}
	p := pair{n, cap}
	if v, ok := memoTopDown[p]; ok {
		return v
	}
	n--              
	if ws[n] > cap { 
		return dpTopDown(n, cap)
	}
	toInclude := vs[n] + dpTopDown(n, cap-ws[n])
	notInclude := dpTopDown(n, cap)
	v := max(toInclude, notInclude)
	memoTopDown[p] = v
	return v
}
func dpTopDown ( n int , cap int ) int { if cap == 0 || n == 0 { return 0 } p := pair { n , cap } if v , ok := memoTopDown [ p ] ; ok { return v } n -- if ws [ n ] > cap { return dpTopDown ( n , cap ) } toInclude := vs [ n ] + dpTopDown ( n , cap - ws [ n ] ) notInclude := dpTopDown ( n , cap ) v := max ( toInclude , notInclude ) memoTopDown [ p ] = v return v }
Return the item indices and total value using dynamic programming
Return the item indices and total value using dynamic programming
// make it 0-based index // The n'th item cannot fit in