Problem:
Score : 400 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 M_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 N kinds of chemicals. For each kind of chemical, there is exactly one package of that chemical in stock.

The package of chemical i contains a_i grams of the substance A and b_i grams of the substance B, and is sold for c_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

1≦N≦40

1≦a_i,b_i≦10

1≦c_i≦100

1≦M_a,M_b≦10

gcd(M_a,M_b)=1

a_i, b_i, c_i, M_a and M_b are integers.

Input

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

N M_a M_b
a_1 b_1 c_1
a_2 b_2 c_2
:
a_N b_N c_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.

Sample Input 1

3 1 1
1 2 1
2 1 2
3 3 10

Sample Output 1

3

The amount of money spent will be minimized by purchasing the packages of chemicals 1 and 2.

In this case, the mixture of the purchased chemicals will contain 3 grams of the substance A and 3 grams of the substance B, which are in the desired ratio: 3:3=1:1.

The total price of these packages is 3 yen.

Sample Input 2

1 1 10
10 10 10

Sample Output 2

-1

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

Solution in Rust (1.15.1) (judge verdict: Wrong Answer)

#![allow(unused_imports, unused_variables, dead_code, non_snake_case, unused_macros)]
use std::io::{stdin, Read, StdinLock};
use std::str::FromStr;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

fn getline() -> String{
    let mut res = String::new();
    std::io::stdin().read_line(&mut res).ok();
    res
}

macro_rules! readl {
    ($t: ty) => {
        {
            let s = getline();
            s.trim().parse::<$t>().unwrap()
        }
    };
    ($( $t: ty),+ ) => {
        {
            let s = getline();
            let mut iter = s.trim().split(' ');
            ($(iter.next().unwrap().parse::<$t>().unwrap(),)*) 
        }
    };
}

macro_rules! readlvec {
    ($t: ty) => {
        {
            let s = getline();
            let iter = s.trim().split(' ');
            iter.map(|x| x.parse().unwrap()).collect::<Vec<$t>>()
        }
    }
}

macro_rules! mvec {
    ($v: expr, $s: expr) => {
        vec![$v; $s]
    };
    ($v: expr, $s: expr, $($t: expr),*) => {
        vec![mvec!($v, $($t),*); $s]
    };
}

macro_rules! debug {
    ($x: expr) => {
        println!("{}: {:?}", stringify!($x), $x)
    }
}

fn printiter<'a, T>(v: &'a T)
where
    &'a T: std::iter::IntoIterator, 
    <&'a T as std::iter::IntoIterator>::Item: std::fmt::Display {
    for (i,e) in v.into_iter().enumerate() {
        if i != 0 {
            print!(" ");
        }
        print!("{}", e);
    }
    println!("");
}

struct ContestPrinter {
    s: String,
}

impl ContestPrinter {
    fn new() -> ContestPrinter {
        ContestPrinter {
        s: String::new(),
        }
    }

    fn print<T>(&mut self, x: T)
        where T: std::fmt::Display {
        self.s.push_str(format!("{}", x).as_str());
    }

    fn println<T>(&mut self, x: T)
        where T: std::fmt::Display {
        self.s.push_str(format!("{}\n", x).as_str());
    }
}

impl std::ops::Drop for ContestPrinter {
    fn drop(&mut self) {
        print!("{}", self.s);
    }
}

fn is_max_i64(num: i64) -> bool { if num == i64::max_value() { true } else { false } }

// 指定した数までの素数のvec エラトステネスの篩
fn prime_list(n: usize) -> Vec<usize> {
    let mut ret: Vec<usize> = Vec::new();
    let num = n - 1;
    let mut table: Vec<bool> = vec![false; num];
    for i in 0..num {
        if !table[i] {
            let next = i + 2;
            ret.push(next);
            let mut j = i + next;
            while j < num {
                table[j] = true;
                j += next;
            }
        }
    }
    ret
} // [2, 3, 5, 7, ..., n]

// 素因数分解
fn prime_factorization(n: usize) -> Vec<(usize, usize)> {
    let mut ret: Vec<(usize, usize)> = Vec::new();
    let pl = prime_list((n as f64).sqrt() as usize + 1);
    let mut num = n;
    for p in pl { // 各素数で割れるだけ割る
        let mut cnt = 0;
        while (num % p) == 0 {
            num /= p;
            cnt += 1;
        }
        if cnt > 0 { ret.push((p, cnt)); }
    }
    if ret.is_empty() { vec![(n, 1); 1] }
    else { ret }
} // n = 2*2*2 + 3 -> [(2, 3), (3, 1)]

fn is_prime(n: i64) -> bool {
    let mut i = 2;
    loop {
        if (n % i) == 0 {return false; }
        i += 1;
        if (i*i) > n { break; }
    }
    return true;
}

// オイラーのφ関数 nと互いに素（gcdが1）な数の個数が取れる
fn euler_phi_vec(n: usize) -> Vec<u64> {
    let mut f: Vec<u64> = vec![0; n];
    let mut p: Vec<u64> = vec![1; n];
    for i in 0..n { f[i] = i as u64; }
    for i in 2..n {
        if p[i] != 0 {
            f[i] -= f[i] / i as u64;
            let mut j = i + i;
            loop {
                if j >= n { break; }
                p[j] = 0;
                f[j] -= f[j] / i as u64;
                j += i;
            }
        }
    }
    f
}

fn main() {
    let mut pr = ContestPrinter::new();
    
    let MAX = 1000000;
    let (N, MA, MB) = readl!(usize, usize, usize);
    let mut abc = Vec::new();

    let (N1, A, B) = (N + 1, 401, 401);
    // dp[i][j][k] = k番目まで調べた時の合計幅i，使用枚数jの重要度の最大値
    // 更新: k番目まで調べたけどそれを使わない時が最大 = dp[i][j][k-1] か，
    //                           使う時が最大 = dp[i-a[k-1]][j-1][k-1] + b[k-1]　のどちらか
    let mut dp: Vec<Vec<Vec<i32>>> = vec![vec![vec![MAX; B]; A]; N1];

    for i in 0..N {
        let (a, b, c) = readl!(usize, usize, i32);
        abc.push((a, b, c));
//        dp[1][a][b] = c;
    }
    dp[0][0][0] = 0;
    
    for i in 1..N1 {
        for j in 1..A {
            for k in 1..B {
                let (a, b, c) = abc[i - 1];
                let mut n_use = min(dp[i - 1][j][k], dp[i][j][k]);
                let mut y_use = if (a > j) | (b > k) { dp[i][j][k] }
                            else { dp[i - 1][j - a][k - b] };   
                dp[i][j][k] = min(n_use, y_use + c);
            }
        }
    }

    let mut ans = MAX;
    for i in 0..A {
        for j in 0..B {
            if (i*MB) != (j*MA) { continue; }
            let v = dp[N][i][j];
            ans = min(ans, v);
        }
    }
    
    pr.println(if ans == MAX { -1 } else { ans });
}