Sluggish Decisional Knapsack

We consider the classical Knapsack problem in its 0/1 variant; this means that each object is either taken fully into the knapsack, exactly once, or left fully out. This problem refers to a decisional version where both a weight limit and a desired value are specified, and where we wish to obtain all the possible solutions. Alternatively, problem X59240 corresponds instead to the more standard optimization version.

More precisely, given are a desired total value totvtotv, a weight limit limwlimw, and a sequence of nn objects consisting of weight and value of each object. A solution is a subset of objects whose sum of weights is at most limwlimw and whose sum of values is at least totvtotv.

Write a program that reads an instance consisting of totvtotv, limwlimw, nn, and the nn pairs of weight and value of the nn objects (in this order) and prints all the solutions: sets of object numbers where the total weight does not exceed limwlimw and the total value is at least totvtotv. Note that there may be instances where the answer is empty, when no solution at all is possible.

Input

Input is an instance that starts with totvtotv, the minimum total value desired for the knapsack, followed by limwlimw, the maximum weight, and nn, the quantity of objects. Then follow nn pairs: the weight and value of each of the nn objects. Here nn is a non-negative integer, and all the other values are positive integers.

Output

Print one line for each possible solution. In each solution, objects are identified by the numbers 0 to n1n-1, in the same order in which their weights and values were read; hence, each line must consist of the indices (between 0 and n1n-1) of the objects taken for that solution, separated by single blank spaces.

Observation

The lines corresponding to the solutions can be printed in any order. Further, the object indices making up each solution can be printed within the corresponding line also in any order. On the other hand, the time allowance of this problem is rather mild, and even quite inefficient solutions may be accepted (hence the "sluggish" adjective).

Problem information

Author: José Luis Balcázar

Generation: 2026-01-25T17:26:11.092Z

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