Advertisement blocking software were detected ;( Please add this webpage to whitelist.

SCUBADIV - Scuba diver

A scuba diver uses a special equipment for diving. He has a cylinder with two containers: one with oxygen and the other with nitrogen. Depending on the time he wants to stay under water and the depth of diving the scuba diver needs various amount of oxygen and nitrogen. The scuba diver has at his disposal a certain number of cylinders. Each cylinder can be described by its weight and the volume of gas it contains. In order to complete his task the scuba diver needs specific amount of oxygen and nitrogen. What is the minimal total weight of cylinders he has to take to complete the task?


The scuba diver has at his disposal 5 cylinders described below. Each description consists of: volume of oxygen, volume of nitrogen (both values are given in litres) and weight of the cylinder (given in decagrams):

3 36 120
10 25 129
5 50 250
1 45 130
4 20 119

If the scuba diver needs 5 litres of oxygen and 60 litres of nitrogen then he has to take two cylinders of total weight 249 (for example the first and the second ones or the fourth and the fifth ones).


Write a program that for each test case:

  • reads scuba diver's demand for oxygen and nitrogen, the number of accessible cylinders and their descriptions;
  • computes the minimal total weight of cylinders the scuba diver needs to complete his task;
  • outputs the result.

Note: the given set of cylinders always allows to complete the given task.


The number of test cases c is in the first line of input, then c test cases follow separated by an empty line.

In the first line of a test case there are two integers t, a separated by a single space, 1 <= t <= 21 and 1 <= a <= 79. They denote volumes of oxygen and nitrogen respectively, needed to complete the task. The second line contains one integer n, 1 <= n <= 1000, which is the number of accessible cylinders. The following n lines contain descriptions of cylinders; i-th line contains three integers ti, ai, wi separated by single spaces, (1 <= ti <= 21, 1 <= ai <= 79, 1 <= wi <= 800). These are respectively: volume of oxygen and nitrogen in the i-th cylinder and the weight of this cylinder.


For each test case your program should output one line with the minimal total weight of cylinders the scuba diver should take to complete the task.


Sample input:
5 60
3 36 120
10 25 129
5 50 250
1 45 130
4 20 119

Sample output:

hide comments
Bhuvnesh Jain: 2015-10-29 13:58:46

Is Ankit Kumar crack person or what? You have solved the question does not mean that you spoil it for others. Always keeps on pasting links of spoj solution for every question attempted by him. Request Spoj admin to ban/ hide such links

naruto09: 2015-10-28 15:51:11

@ANKIT KUMAR please remove the link of solution from your comment

tille: 2015-10-25 19:01:06

ANKIT KUMAR why you paste the solution? you fucked up my thinking proccess.

Last edit: 2015-10-26 02:42:39
Mohit Rathore: 2015-09-01 12:24:39

Bottoms up solution anyone? Recursive solution ain't that satiating.

(Tjandra Satria Gunawan)(曾毅昆): 2015-07-28 06:49:30

2D knapsack was really great ;-)

Rishabh Joshi: 2015-07-15 17:11:26

Very nice problem! Nice recurse.
Liked solving it.

GAURAV CHANDEL: 2015-04-18 10:19:23

good knapsack type problem...

xxbloodysantaxx: 2015-03-24 15:24:09

He needs exactly that Volume of oxygen and Nitrogen or rather more can be ok??
Got it

Last edit: 2015-03-24 15:31:00
arbit: 2015-03-22 19:34:49

I am not yet totally sure how to solve this best.
I had the same idea as the most popular idea on internet.
Maybe the test cases weren't very harsh.

Please provide any hints/solutions which can use just a 2D array without taking into consideration the maximum amount of oxygen/nitrogen that can come up in the solution

Added by:Michał Czuczman
Time limit:5s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel Pentium G860 3GHz)
Resource:5th Polish Olympiad in Informatics, stage 2