Language: C++

#include <stdio.h>
#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <algorithm>
#include <queue>
#include <list>
#include <stack>

using namespace std;

struct drag
{
	int m, val;
};

int n;
int ma, mb;
int result;
vector<drag> v;

void calc(int cost, int i, int m)
{
	if (i == n)
	{
		if (m != 0)
			return;
		if (cost == 0)
			return;
		if (cost < result)
			result = cost;
	}
	else
	{
		if (cost >= result)
			return;
		//	次の製品がm = 0でもcostは高くなるのでx
		if (m == 0 && cost > 0)
		{
			result = cost;
			return;
		}

		if (m + v[i].m <= 0)
		{
			calc(cost + v[i].val, i + 1, m + v[i].m);
			calc(cost, i + 1, m);
		}
	}
}


int main()
{
	result = INT_MAX;
	cin >> n >> ma >> mb;
	v.resize(n);
	for (int i = 0; i < n; i++)
	{
		int a, b;
		cin >> a >> b >> v[i].val;
		v[i].m = a * mb - b * ma;
	}
	sort(v.begin(), v.end(), [](drag &l, drag &r) {return l.m < r.m; });
	stable_sort(v.begin(), v.end(), [](drag &l, drag &r) {return l.val < r.val; });
	int plus = 0;
	int minus = -1;
	for (int i = 0; i < n; i++)
		if (v[i].m >= 0)
		{
			if (minus == -1)
				minus = i - 1;
			plus += v[i].m;
		}
	
	if (minus >= 0 && plus + v[minus].m < 0)
	{
		printf("-1");
		return;
	}

	calc(0, 0, 0);
	if (result == INT_MAX)
		result = -1;
	printf("%d", result);
	return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:56:18: error: 'INT_MAX' was not declared in this scope
   56 |         result = INT_MAX;
      |                  ^~~~~~~
a.cc:10:1: note: 'INT_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
    9 | #include <stack>
  +++ |+#include <climits>
   10 | 
a.cc:80:17: error: return-statement with no value, in function returning 'int' [-fpermissive]
   80 |                 return;
      |                 ^~~~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:71,
                 from /usr/include/c++/14/string:51,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:2:
/usr/include/c++/14/bits/predefined_ops.h: In instantiation of 'bool __gnu_cxx::__ops::_Iter_comp_val<_Compare>::operator()(_Iterator, _Value&) [with _Iterator = __gnu_cxx::__normal_iterator<drag*, std::vector<drag> >; _Value = const drag; _Compare = main()::<lambda(drag&, drag&)>]':
/usr/include/c++/14/bits/stl_algobase.h:1504:14:   required from '_ForwardIterator std::__lower_bound(_ForwardIterator, _ForwardIterator, const _Tp&, _Compare) [with _ForwardIterator = __gnu_cxx::__normal_iterator<drag*, vector<drag> >; _Tp = drag; _Compare = __gnu_cxx::__ops::_Iter_comp_val<main()::<lambda(drag&, drag&)> >]'
 1504 |           if (__comp(__middle, __val))
      |               ~~~~~~^~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:2460:26:   required from 'void std::__merge_without_buffer(_BidirectionalIterator, _BidirectionalIterator, _BidirectionalIterator, _Distance, _Distance, _Compare) [with _BidirectionalIterator = __gnu_cxx::__normal_iterator<drag*, vector<drag> >; _Distance = long int; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(drag&, drag&)> >]'
 2460 |             = std::__lower_bound(__middle, __last, *__first_cut,
      |               ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 2461 |                                  __gnu_cxx::__ops::__iter_comp_val(__comp));
      |                                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:2754:34:   required from 'void std::__inplace_stable_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<drag*, vector<drag> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(drag&, drag&)> >]'
 2754 |       std::__merge_without_buffer(__first, __middle, __last,
      |       ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
 2755 |                                   __middle - __first,
      |                                   ~~~~~~~~~~~~~~~~~~~
 2756 |                                   __last - __middle,
      |                                   ~~~~~~~~~~~~~~~~~~
 2757 |                                   __comp);
      |                                   ~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:4956:28:   required from 'void std::__stable_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<drag*, vector<drag> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(drag&, drag&)> >]'
 4956 |         std::__inplace_stable_sort(__first, __last, __comp);
      |         ~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:5030:36:   required from 'void std::stable_sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<drag*, vector<drag> >; _Compare = main()::<lambda(drag&, drag&)>]'
 5030 |       _GLIBCXX_STD_A::__stable_sort(__first, __last,
      |                                    ^
a.cc:66:13:   required from here
   66 |         stable_sort(v.begin(), v.end(), [](drag &l, drag &r) {return l.val < r.val; });
      |         ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/predefined_ops.h:196:30: error: no match for call to '(main()::<lambda(drag&, drag&)>) (drag&, const drag&)'
  196 |         { return bool(_M_comp(*__it, __val)); }
      |                       ~~~~~~~^~~~~~~~~~~~~~
/usr/include/c++/14/bits/predefined_ops.h:196:30: note: candidate: 'bool (*)(drag&, drag&)' (conversion)
/usr/include/c++/14/bits/predefined_ops.h:196:30: note:   conversion of argument 3 would be ill-formed:
/usr/include/c++/14/bits/predefined_ops.h:196:30: error: binding reference of type 'drag&' to 'const drag' discards qualifiers
a.cc:66:41: note: candidate: 'main()::<lambda(drag&, drag&)>' (near match)
   66 |         stable_sort(v.begin(), v.end(), [](drag &l, drag &r) {return l.val < r.val; });
      |                                         ^
a.cc:66:41: note:   conversion of argument 2 would be ill-formed:
a.cc:66:41: error: binding reference of type 'drag&' to 'const drag' discards qualifiers
/usr/include/c++/14/bits/predefined_ops.h: In instantiation of 'bool __gnu_cxx::__ops::_Val_comp_iter<_Compare>::operator()(_Value&, _Iterator) [with _Value = const drag; _Iterator = __gnu_cxx::__normal_iterator<drag*, std::vector<drag> >; _Compare = main()::<lambda(drag&, drag&)>]':
/usr/include/c++/14/bits/stl_algo.h:1993:14:   required from '_ForwardIterator std::__upper_bound(_ForwardIterator, _ForwardIterator, const _Tp&, _Compare) [with _ForwardIterator = __gnu_cxx::__normal_iterator<drag*, vector<drag> >; _Tp = drag; _Compare = __gnu_cxx::__ops::_Val_comp_iter<main()::<lambda(drag&, drag&)> >]'
 1993 |           if (__comp(__val, __middle))
      |               ~~~~~~^~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:2469:26:   required from 'void std::__merge_without_buffer(_BidirectionalIterator, _BidirectionalIterator, _BidirectionalIterator, _Distance, _Distance, _Compare) [with _BidirectionalIterator = __gnu_cxx::__normal_iterator<drag*, vector<drag> >; _Distance = long int; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(drag&, drag&)> >]'
 2469 |             = std::__upper_bound(__first, __middle, *__second_cut,
      |               ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 2470 |                                  __gnu_cxx::__ops::__val_comp_iter(__comp));
      |                                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:2754:34:   required from 'void std::__inplace_stable_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<drag*, vector<drag> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(drag&, drag&)> >]'
 2754 |       std::__merge_without_buffer(__first, __middle, __last,
      |       ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
 2755 |                                   __middle - __first,
      |                                   ~~~~~~~~~~~~~~~~~~~
 2756 |                                   __last - __middle,
      |                                   ~~~~~~~~~~~~~~~~~~
 2757 |                                   __comp);
      |                                   ~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:4956:28:   required from 'void std::__stable_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<drag*, vector<drag> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(drag&, drag&)> >]'
 4956 |         std::__inplace_stable_sort(__first, __last, __comp);
      |         ~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:5030:36:   required from 'void std::stable_sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<drag*, vector<drag> >; _Compare = main()::<lambda(drag&, drag&)>]'
 5030 |       _GLIBCXX_STD_A::__stable_sort(__first, __last,
      |                                    ^
a.cc:66:13:   required from here
   66 |         stable_sort(v.begin(), v.end(), [](drag &l, drag &r) {return l.val < r.val; });
      |         ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/predefined_ops.h:240:30: error: no match for call to '(main()::<lambda(drag&, drag&)>) (const drag&, drag&)'
  240 |         { return bool(_M_comp(__val, *__it)); }
      |                       ~~~~~~~^~~~~~~~~~~~~~
/usr/include/c++/14/bits/predefined_ops.h:240:30: note: candidate: 'bool (*)(drag&, drag&)' (conversion)
/usr/include/c++/14/bits/predefined_ops.h:240:30: note:   conversion of argument 2 would be ill-formed:
/usr/include/c++/14/bits/predefined_ops.h:240:30: error: binding reference of type 'drag&' to 'const drag' discards qualifiers
a.cc:66:41: note: candidate: 'main()::<lambda(drag&, drag&)>' (near match)
   66 |         stable_sort(v.begin(), v.end(), [](drag &l, drag &r) {return l.val < r.val; });
      |                                         ^
a.cc:66:41: note:   conversion of argument 1 would be ill-formed:
a.cc:66:41: error: binding reference of type 'drag&' to 'const drag' discards qualifiers