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 C# (Mono 4.6.2.0) (judge verdict: Runtime Error)

using System;
using System.Linq;
using System.Collections.Generic;
using static AtCoderer.Comfort;
using static AtCoderer.Math;
using static AtCoderer.Algorithm;
using static System.Math;

namespace AtCoderer
{
    public class App
    {
        public static void Main(string[] args)
        {
            Solve();
            Console.ReadKey();
        }
        public static void Solve()
        {
            int n, ma, mb;
            ParseArray(out n, out ma, out mb);
            int[,,] dp = new int[400, 400, 40];
            for (int i = 0; i < 400; i++)
            {
                for (int j = 0; j < 400; j++)
                {
                    for (int k = 0; k < 40; k++)
                    {
                        dp[i, j, k] = 231031015;
                    }
                }
            }
            dp[0, 0, 0] = 0;
            for (int k = 0; k < n; k++)
            {
                int a, b, c;
                ParseArray(out a, out b, out c);
                for (int i = 0; i < 400; i++)
                {
                    for (int j = 0; j < 400; j++)
                    {
                        dp[i + a, j + b, k + 1] = dp[i, j, k] + c;
                        dp[i+1, j, k + 1] = dp[i, j, k];
                    }
                }
            }
            int f = int.MaxValue;
            for (int k = 0; k < n; k++)
            {
                for (int i = 0; i < 400; i++)
                {
                    for (int j = 0; j < 400; j++)
                    {
                        if (i * mb == j * ma && (i != 0 || j != 0))
                        {
                            f = Min(f, dp[i, j, k]);
                        }
                    }
                }
            }
            Console.WriteLine(f > 100000 ? -1 : f);
        }
        public static int DivBy2(int a)
        {
            int p = 0;
            while (a % 2 != 1)
            {
                a /= 2;
                p++;
            }
            return p;
        }
    }
    public static class Comfort
    {
        //ソート
        public static int[] OrderBy(this int[] IArr)
        {
            int[] arr = new int[IArr.Length];
            Array.Copy(IArr, arr, IArr.Length);
            Array.Sort(arr);
            return arr;
        }
        //ソート
        public static char[] OrderBy(this string IArr)
        {
            char[] arr = new char[IArr.Length];
            Array.Copy(IArr.ToCharArray(), arr, IArr.Length);
            Array.Sort(arr);
            return arr;
        }
        //MinとMax
        public static int Max(params int[] p) => p.Max();
        public static long Max(params long[] p) => p.Max();
        public static int Min(params int[] p) => p.Min();
        public static long Min(params long[] p) => p.Min();
        //整数入力
        public static int Parse()
        {
            return int.Parse(Console.ReadLine());
        }
        //整数列入力
        public static int[] ParseArray(this string s)
        {
            return s.Split().Select(v => int.Parse(v)).ToArray();
        }
        //整数列入力
        public static long[] ParseArrayL(this string s)
        {
            return s.Split().Select(v => long.Parse(v)).ToArray();
        }
        //整数列入力
        public static int[] ParseArray()
        {
            return Console.ReadLine().Split().Select(v => int.Parse(v)).ToArray();
        }
        //整数列入力
        public static long[] ParseArrayL()
        {
            return Console.ReadLine().Split().Select(v => long.Parse(v)).ToArray();
        }
        //2整数入力
        public static void ParseArray(out int a, out int b)
        {
            var r = Console.ReadLine().Split().Select(v => int.Parse(v)).ToArray();
            a = r[0];
            b = r[1];
        }
        //3整数入力
        public static void ParseArray(out int a, out int b, out int c)
        {
            var r = Console.ReadLine().Split().Select(v => int.Parse(v)).ToArray();
            a = r[0];
            b = r[1];
            c = r[2];
        }
        //4整数入力
        public static void ParseArray(out int a, out int b, out int c, out int d)
        {
            var r = Console.ReadLine().Split().Select(v => int.Parse(v)).ToArray();
            a = r[0];
            b = r[1];
            c = r[2];
            d = r[3];
        }
        //2整数入力
        public static void ParseArray(out long a, out long b)
        {
            var r = Console.ReadLine().Split().Select(v => long.Parse(v)).ToArray();
            a = r[0];
            b = r[1];
        }
        //3整数入力
        public static void ParseArray(out long a, out long b, out long c)
        {
            var r = Console.ReadLine().Split().Select(v => long.Parse(v)).ToArray();
            a = r[0];
            b = r[1];
            c = r[2];
        }
        //4整数入力
        public static void ParseArray(out long a, out long b, out long c, out long d)
        {
            var r = Console.ReadLine().Split().Select(v => long.Parse(v)).ToArray();
            a = r[0];
            b = r[1];
            c = r[2];
            d = r[3];
        }
        //char配列のstring化
        public static string ToString(this IEnumerable<char> v)
        {
            return string.Join("", v);
        }
        //配列のstring化
        public static string Join<T>(this IEnumerable<T> v, char c)
        {
            return string.Join(c.ToString(), v);
        }
        //配列のstring化
        public static string Join<T>(this IEnumerable<T> v, string s)
        {
            return string.Join(s, v);
        }
    }
    public static class Math
    {
        //自然数判定
        public static bool IsNatural(this float v) => v - (int)v == 0;
        public static bool IsNatural(this double v) => v - (int)v == 0;
        //非重複順列
        public static long nCm(long n, long m)
        {
            if (m == 0) return 1;
            if (n == 0) return 0;
            return n * nCm(n - 1, m - 1) / m % 1000000007;
        }
        //素因数分解
        public static IEnumerable<int> Factor(int n)
        {
            int i = 2;
            int tmp = n;

            while (i * i <= n)
            {
                if (tmp % i == 0)
                {
                    tmp /= i;
                    yield return i;
                }
                else
                {
                    i++;
                }
            }
            if (tmp != 1) yield return tmp;
        }
        //最大公約数
        public static int Gcd(int a, int b) => a > b ? GcdRecursive(a, b) : GcdRecursive(b, a);
        private static int GcdRecursive(int x, int y) => y == 0 ? x : GcdRecursive(y, x % y);
        //最大公約数
        public static long Gcd(long a, long b) => a > b ? GcdRecursive(a, b) : GcdRecursive(b, a);
        private static long GcdRecursive(long x, long y) => y == 0 ? x : GcdRecursive(y, x % y);
        //最小公倍数
        public static long Lcm(int a, int b) => ((long)a) * b / Gcd(a, b);
    }
    public static class Algorithm
    {
        //2値タプル
        public class Point
        {
            public int a;
            public int b;
            public Point(int x, int y)
            {
                a = x;
                b = y;
            }
        }
        //3値タプル
        public class Tri
        {
            public int a;
            public int b;
            public int c;
            public Tri(int x, int y, int z)
            {
                a = x;
                b = y;
                c = z;
            }
        }
        //全列挙のクラス
        public class EnumAll
        {
            bool Over = false;
            int UpperValue;
            int Dimension;
            public int[] Current { private set; get; }
            public EnumAll(int Value, int Dimension)
            {
                this.Dimension = Dimension;
                UpperValue = Value;
                Current = new int[Dimension];
                Current[0] = -1;
            }
            public bool Next()
            {
                if (Over)
                    return false;
                bool flag = false;
                for (int i = 0; i < Dimension; i++)
                {
                    if (++Current[i] >= UpperValue)
                        Current[i] = 0;
                    else
                    {
                        flag = true;
                        break;
                    }
                }
                if (!flag)
                {
                    Over = true;
                    return false;
                }
                return true;
            }
            public bool IsOver()
            {
                return Over;
            }
        }
        //Union-Find
        public class UnionFind
        {
            private int[] Parent;
            private int GetRoot(int v)
            {
                if (Parent[v] < 0)
                    return v;
                else
                    return Parent[v] = GetRoot(Parent[v]);
            }
            public UnionFind(int size)
            {
                Parent = new int[size];
                for (int i = 0; i < size; i++)
                    Parent[i] = -1;
            }
            public bool AreGrouped(int a, int b)
            {
                return GetRoot(a) == GetRoot(b);
            }
            public void Connect(int a, int b)
            {
                a = GetRoot(a);
                b = GetRoot(b);
                if (a == b) return;
                if (Parent[b] < Parent[a])
                {
                    Parent[b] += Parent[a];
                    Parent[a] = b;
                }
                else
                {
                    Parent[a] += Parent[b];
                    Parent[b] = a;
                }
            }
            public int GetGroupSize(int a)
            {
                return -Parent[GetRoot(a)];
            }
        }
    }
}