Optimal choice

Angel, a good friend of yours, has a truck that can transport a maximum
weight W. He has n objects at home, each with weight w_(i) and value
v_(i). He will depart, so he wants to pick the most valuable subset of
objects with total weight no larger than W. However, Angel does not like
to compute optimal solutions. Can you help him?

Input

Input consists of several cases with only integer numbers. Every case
begins with W and n, followed by n pairs w_(i), v_(i). Assume
1 ≤ W ≤ 10¹², 1 ≤ n ≤ 100, 1 ≤ w_(i) ≤ W, and 1 ≤ v_(i) ≤ 100.

Output

For every case, print three lines. On the first, print the largest
possible total value. On the second, print the number of objects of the
optimal subset. On the third, print in increasing order and separated by
spaces the indices (starting at one) of the chosen objects. If there is
more than one optimal solution, you can choose any one.

Problem information

Author: Salvador Roura

Generation: 2026-01-25T11:25:05.453Z

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