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# (.NET Core 3.1.201) (judge verdict: Time Limit Exceeded)

using System;
using System.Collections.Generic;
using static Assistant.Input;
using static Assistant.Debug;
using System.Linq;
using Assistant;

namespace ABC054D
{
    class Program
    {
        static void Main(string[] args)
        {
            int N = RInt;
            int Ma = RInt;
            int Mb = RInt;

            List<int> canda = new List<int>();
            List<int> candb = new List<int>();
            int sa = Ma;
            int sb = Mb;
            while (sa <= 400 && sb <= 400)
            {
                canda.Add(sa);
                candb.Add(sb);
                sa += Ma;
                sb += Mb;
            }

            Med[] meds = new Med[N];
            for (int i = 0; i < N; i++)
            {
                int a = RInt;
                int b = RInt;
                int c = RInt;
                meds[i] = new Med { a = a, b = b, c = c };
            }

            // 半分全列挙
            var first = meds.Take(N / 2).ToArray();
            var last = meds.Skip(N / 2).ToArray();

            var bf = BitSearch(first.Length);
            var lf = BitSearch(last.Length);

            int[,] min = new int[401, 401];
            for (int i = 0; i < 401; i++)
            {
                for (int j = 0; j < 401; j++)
                {
                    min[i, j] = int.MaxValue / 2;
                }
            }

            foreach (var b in bf)
            {
                int suma = 0;
                int sumb = 0;
                int sumc = 0;
                foreach (var b_ in b)
                {
                    suma += first[b_].a;
                    sumb += first[b_].b;
                    sumc += first[b_].c;
                }
                min[suma, sumb] = Math.Min(min[suma, sumb], sumc);
            }

            int ans = int.MaxValue;
            foreach (var l in lf)
            {
                int suma = 0;
                int sumb = 0;
                int sumc = 0;
                foreach (var l_ in l)
                {
                    suma += last[l_].a;
                    sumb += last[l_].b;
                    sumc += last[l_].c;
                }

                for (int i = 0; i < canda.Count; i++)
                {
                    int rema = canda[i] - suma;
                    int remb = candb[i] - sumb;
                    if (rema < 0 || remb < 0) continue;
                    int cand = sumc + min[rema, remb];
                    ans = Math.Min(ans, cand);
                }
            }

            if (ans == int.MaxValue / 2) ans = -1;
            Console.WriteLine(ans);

        }

        public static int[][] BitSearch(int n)
        {
            int[][] ret = new int[1 << n][];
            for (int bit = 0; bit < (1 << n); ++bit)
            {
                List<int> S = new List<int>();
                for (int i = 0; i < n; i++)
                {
                    if ((bit & (1 << i)) != 0)
                    {
                        S.Add(i);
                    }
                }

                ret[bit] = S.ToArray();
            }
            return ret;
        }

        struct Med
        {
            public int a;
            public int b;
            public int c;
        }
    }


}

namespace Assistant
{
    static class Input
    {
        static List<string> line = new List<string>();
        static int index = 0;
        static String RNext()
        {
            if (line.Count <= index) line.AddRange(Console.ReadLine().Split());
            return line[index++];
        }
        public static int RInt => int.Parse(RNext());
        public static long RLong => long.Parse(RNext());
        public static int[] RInts => Console.ReadLine().Split().Select(int.Parse).ToArray();
        public static long[] RLongs => Console.ReadLine().Split().Select(long.Parse).ToArray();
        public static string RString => RNext();
        //以下未テスト
        public static int[] RIntsC(int c) => Enumerable.Repeat(0, c).Select(x => int.Parse(RNext())).ToArray();
        public static long[] RLongsC(int c) => Enumerable.Repeat(0, c).Select(x => long.Parse(RNext())).ToArray();
        public static char[][] RMap(int h) => Enumerable.Repeat(0, h).Select(x => Console.ReadLine().ToCharArray()).ToArray();
    }

    public struct Mlong
    {
        long _v;
        const long mod = 1000000007;
        public Mlong(long n = 0) : this() { _v = n >= mod || n < 0 ? (n % mod + mod) % mod : n; }

        public static implicit operator Mlong(long _x) => new Mlong(_x);
        public static implicit operator long(Mlong _x) => _x._v;
        public static Mlong operator +(Mlong m1, Mlong m2) { long m = m1._v + m2._v; return m >= mod ? m - mod : m; }
        public static Mlong operator -(Mlong m1, Mlong m2) { long m = m1._v - m2._v; return m >= 0 ? m : m + mod; }
        public static Mlong operator *(Mlong m1, Mlong m2) => m1._v * m2._v % mod;
        public static Mlong operator /(Mlong m1, Mlong m2) => m1._v * ModPow(m2._v, mod - 2) % mod;

        public static long ModPow(long a, long n)
        {
            if (n == 0) return 1;
            else if (n % 2 == 1) return a * ModPow(a, n - 1) % mod;
            else return ModPow(a * a % mod, n / 2);
        }

        static Mlong[] fac, finv, inv;
        public static void nCkInit(int max)
        {
            fac = new Mlong[max]; finv = new Mlong[max]; inv = new Mlong[max];
            fac[0] = fac[1] = 1;
            finv[0] = finv[1] = 1;
            inv[1] = 1;
            for (int i = 2; i < max; i++)
            {
                fac[i] = fac[i - 1] * i;
                inv[i] = mod - inv[mod % i] * (mod / i);
                finv[i] = finv[i - 1] * inv[i];
            }
        }

        public static Mlong nCk(int n, int k)
        {
            if (n < k) return 0;
            if (n < 0 || k < 0) return 0;
            return fac[n] * finv[k] * finv[n - k];
        }
    }

    static class Debug
    {
        public static void Draw<T>(T[] array, int mode = 0)
        {
#if DEBUG
            Console.WriteLine(string.Join(" ", array));
#endif
        }
        static public void Draw<T>(T[,] map, int mode = 1)
        {
#if DEBUG
            int W = map.GetLength(0);
            int H = map.GetLength(1);

            string[,] map2 = new string[W + 1, H + 1];
            for (int i = 0; i < W + 1; i++)
            {
                for (int j = 0; j < H + 1; j++)
                {
                    if (i == 0 && j == 0) map2[i, j] = 0.ToString();
                    else if (i == 0) map2[i, j] = (j - 1).ToString();
                    else if (j == 0) map2[i, j] = (i - 1).ToString();
                    else map2[i, j] = map[i - 1, j - 1].ToString();
                }
            }

            for (int i = 0; i < W + 1; i++)
            {
                for (int j = 0; j < H + 1; j++)
                {
                    if (mode == 0) Console.Write(map2[i, j].Last());
                    if (mode == 1) Console.Write(map2[i, j] + " ");
                }
                Console.WriteLine();
            }
            Console.WriteLine();
#endif
        }

    }
}