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
rafa45: 2021-09-08 13:00:17

cin and cout in cpp give wa but scanf and printf work fine for same code.

prezesstefan: 2021-06-23 16:40:12

Java: top-down (recursive) approach TLEs, so you can start with that but you'll need to rewrite bottom-up
no need to declare dp[1001][500][500], can be more precise with dp[n+1][t+1][a+1]

distructo: 2020-12-30 08:43:07

Declare the max dp as dp[1001][500][500]

kumar_anubhav: 2020-08-16 04:12:27

what are the three states please anyone ?

amansahu112: 2020-05-07 20:07:44

don't let the index in dp go negative..!!

vidit1400: 2020-05-05 01:47:27

just pay close attention to when it gets less than 0 . See to that whenever u pass value it is never less than 0 by max(0,..) thus lowest it gets is 0 and u escape segfault

aditya_shukla: 2020-04-23 18:37:41

BufferedReader input not working while Scanner does.
What kind of judge is this.

darker__space: 2020-04-08 08:39:56

try your best but after investing much time....u really need to learn new concepts...

Last edit: 2020-04-08 08:40:19
varun_pandey: 2020-04-01 21:07:08

mind dp[0][0][0]=0,,,case costed me several wa

dylanthomas: 2020-03-18 00:13:41

great problem

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