#ABC054D. Mixing Experiment
Mixing Experiment
Problem Statement
Dolphin is planning to generate a small amount of a certain chemical substance .
In order to generate the substance , he must prepare a solution which is a mixture of two substances and in the ratio of .
He does not have any stock of chemicals, however, so he will purchase some chemicals at a local pharmacy.
The pharmacy sells kinds of chemicals. For each kind of chemical, there is exactly one package of that chemical in stock.
The package of chemical contains grams of the substance and bi grams of the substance , and is sold for yen (the currency of Japan).
Dolphin will purchase some of these packages. For some reason, he must use all contents of the purchased packages to generate the substance .
Find the minimum amount of money required to generate the substance .
If it is not possible to generate the substance by purchasing any combination of packages at the pharmacy, report that fact.
Constraints
- and are integers.
Input
The input is given from Standard Input in the following format:
N Ma Mb a1 b1 c1 a2 b2 c2 : aN bN cN
Output
Print the minimum amount of money required to generate the substance . If it is not possible to generate the substance , print -1
instead.
Samples
3 1 1
1 2 1
2 1 2
3 3 10
3
The amount of money spent will be minimized by purchasing the packages of chemicals and .
In this case, the mixture of the purchased chemicals will contain grams of the substance and grams of the substance , which are in the desired ratio: . The total price of these packages is yen.
1 1 10
10 10 10
-1
The ratio of the two substances and cannot be satisfied by purchasing any combination of the packages. Thus, the output should be -1
.