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?
Example
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).
Task
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.
Input
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; ith line contains three integers t_{i}, a_{i}, w_{i} separated by single spaces, (1 <= t_{i} <= 21, 1 <= a_{i} <= 79, 1 <= w_{i} <= 800). These are respectively: volume of oxygen and nitrogen in the ith cylinder and the weight of this cylinder.
Output
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.
Example
Sample input: 1 5 60 5 3 36 120 10 25 129 5 50 250 1 45 130 4 20 119 Sample output: 249
hide comments
ahmedfouadnew:
20190730 01:11:00
great problem!!!


cenation007:
20190628 19:36:17
noice one


nobody550:
20190303 09:45:58
A 3d knapsack problem 

ankit1cool:
20180622 20:13:38
Great question learnt a new thing about memoziation 

s_a_k_s_h_a_m:
20180615 14:24:37
good problem Last edit: 20180615 14:25:26 

Sonu Sharma:
20180608 17:10:46
Average , but tricky question. Advice: Assert that any of the value of o2 and n2 should not be negative. 

archit4077sh:
20180404 12:31:24
Last edit: 20180523 20:30:53 

cj23897:
20170628 19:09:25
Nice question 

cichipi_:
20170609 21:25:55
# 3D dp using memset > TLE...........take 3d temp array to handle it


sas1905:
20170526 16:19:25
3D dp+recursion ..Nice one..:) 
Added by:  MichaĆ Czuczman 
Date:  20040810 
Time limit:  5s 
Source limit:  50000B 
Memory limit:  1536MB 
Cluster:  Cube (Intel G860) 
Languages:  All 
Resource:  5th Polish Olympiad in Informatics, stage 2 