Deep in the scorching sands of the Egyptian desert lies a long-forgotten tomb, rumored to hold treasures of immeasurable value. One moonlit night, an intrepid adventurer discovers the hidden entrance to this tomb. Inside, they find treasures that would make anyone's fortune — golden statues, ancient artifacts, gemstone-encrusted jewelry, and relics from a bygone era.
However, the adventurer faces a problem: their backpack can only hold a limited weight, and the tomb's winding corridors are treacherous. Each item has a specific weight and value, and the adventurer must carefully choose which treasures to carry out to maximize their haul.
Your task is to help the adventurer decide which items to take to achieve the maximum total value without exceeding their backpack's weight limit.
The adventurer is in the tomb with
- weight
$w_i$ : the amount the item weighs (measured in kilograms), - value
$v_i$ : the worth of the item in gold coins.
The adventurer's backpack has a maximum weight capacity
Your job is to determine the maximum total value of the items the adventurer can carry out of the tomb without exceeding the weight limit of their backpack.
The first line contains an integer
Each testcase starts with a line with two integers
The next
For each testcase, output a single integer: the maximum value the adventurer can carry in their backpack.
1
4 7
2 10
3 15
4 20
5 25
35
The adventurer has four items to choose from, with weights and values as follows:
- Item 1: weight =
$2$ , value =$10$ . - Item 2: weight =
$3$ , value =$15$ . - Item 3: weight =
$4$ , value =$20$ . - Item 4: weight =
$5$ , value =$25$ .
The backpack’s maximum capacity is
- If the adventurer chooses items 2 and 3, with a total weight of
$7$ , they can carry a total value of$10 + 15 = 35$ . - He can also choose items 1 and 4, for the same result.
- Any other combination would result in a lower total value.
Thus, the maximum value is
Extend your program to also output the list of items the adventurer should take to achieve the maximum value. For example, given the input above, your program should output something like:
35
Items: 1, 4
Items can be listed in any order.