logo
Problems

Backpack II

Problem

Given n items with size Ai and value Vi, and a backpack with size m. What's the maximum value can you put into the backpack?

Example

Given 4 items with size [2, 3, 5, 7] and value [1, 5, 2, 4], and a backpack with size 10. The maximum value is 9.

Note

You cannot divide item into small pieces and the total size of items you choose should smaller or equal to m.

Challenge

O(nm) memory is acceptable, can you do it in O(m) memory?

Solution

A Dynamic Programming problem.

Online Judge