Backpack with weights and values

You have a backpack that can bear up to ww units of weight. Given nn objects, each with a weight wiw_i and a value viv_i, compute the maximum sum of values achievable, in such a way that the sum of weights does not exceed ww. Take into account that objects cannot be cut: either you pick them, or you discard them.

Input

Input consists of several cases. Every case begins with ww and nn, followed by nn pairs of integer numbers wiw_i viv_i. Assume 1w10001 \le w \le 1000, 1n10001 \le n \le 1000, 1wip1 \le w_i \le p, and 1vi1061 \le v_i \le 10^6.

Output

For every case, print the maximum value of the objects that can be stored in the backpack.

Problem information

Author: Unknown
Translator: Salvador Roura

Generation: 2026-01-25T10:33:45.406Z

© Jutge.org, 2006–2026.
https://jutge.org