100 #ABC054D. [ABC054D] Mixing Experiment

[ABC054D] Mixing Experiment

Score : 400400 points

Problem Statement

Dolphin is planning to generate a small amount of a certain chemical substance C. In order to generate the substance C, he must prepare a solution which is a mixture of two substances A and B in the ratio of Ma:MbM_a:M_b. He does not have any stock of chemicals, however, so he will purchase some chemicals at a local pharmacy. The pharmacy sells NN kinds of chemicals. For each kind of chemical, there is exactly one package of that chemical in stock. The package of chemical ii contains aia_i grams of the substance A and bib_i grams of the substance B, and is sold for cic_i 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 C. Find the minimum amount of money required to generate the substance C. If it is not possible to generate the substance C by purchasing any combination of packages at the pharmacy, report that fact.

Constraints

  • 1N401 \leq N \leq 40
  • 1ai,bi101 \leq a_i,b_i \leq 10
  • 1ci1001 \leq c_i \leq 100
  • 1Ma,Mb101 \leq M_a,M_b \leq 10
  • gcd(Ma,Mb)=1gcd(M_a,M_b)=1
  • aia_i, bib_i, cic_i, MaM_a and MbM_b are integers.

Input

The input is given from Standard Input in the following format:

NN MaM_a MbM_b

a1a_1 b1b_1 c1c_1

a2a_2 b2b_2 c2c_2

::

aNa_N bNb_N cNc_N

Output

Print the minimum amount of money required to generate the substance C. If it is not possible to generate the substance C, print -1 instead.

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 11 and 22. In this case, the mixture of the purchased chemicals will contain 33 grams of the substance A and 33 grams of the substance B, which are in the desired ratio: 3:3=1:13:3=1:1. The total price of these packages is 33 yen.

1 1 10
10 10 10
-1

The ratio 1:101:10 of the two substances A and B cannot be satisfied by purchasing any combination of the packages. Thus, the output should be -1.