Language: C++

#include <bits/stdc++.h>
using namespace std;
 
typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;
#define MP make_pair
#define PB push_back
#define X first
#define Y second
 
#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define RFOR(i, b, a) for(int i = (b) - 1; i >= (a); --i)
#define ITER(it, a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define ALL(a) a.begin(), a.end()
#define SZ(a) (int)((a).size())
#define FILL(a, value) memset(a, value, sizeof(a))
#define debug(a) cout << #a << " = " << a << endl;

const double PI = acos(-1.0);
const LL INF = 1e9 + 47;
const LL LINF = INF * INF;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int N = 1 << 17;
LL n, x;

struct exam
{
	LL l, r, b;
	LL top;
	void read()
	{
		cin >> b >> l >> r;
		top = r * x - (r - l) * b;
	}
};

exam a[N];

int cmp(exam l, exam r)
{
	return l.top > r.top;
}

LL skiky(exam e, LL treba)
{
	if (treba > e.top)
		return INF;
		
	if (treba <= e.l * e.b)
		return (treba + e.l - 1) / e.l;
		
	return (treba + e.b * (e.r - e.l) + e.r - 1) / e.r;
}

LL solve1(LL treba)
{
	LL ans = 0;
	
	int last_full = -1;
	for(int i = 0; i < n && treba > 0; ++i)
	{
		if (treba >= a[i].top)
		{
			last_full = i;
			treba -= a[i].top;
			ans += x;
			continue;
		}
		
		break;				
	}
	
	int tut = INF;
	
	FOR(i, last_full + 1, n)
		tut = min(tut, skiky(a[i], treba));
	
	if (last_full == n - 1)
		tut = 0;
		
	ans += tut;
	return ans;
}

LL get(exam e, LL vykyn)
{
	assert(e.top >= vykyn);
	if (vykyn <= e.r * (x - e.b))
		return vykyn / e.r;
	
	return x - (e.r * (x - e.b) + e.l * e.b - vykyn + e.l - 1) / e.l;
}

LL solve2(LL treba)
{
	LL ans = 0;
	
	int last_full = -1;
	for(int i = 0; i < n && treba > 0; ++i)
	{
		if (treba >= a[i].top)
		{
			last_full = i;
			treba -= a[i].top;
			ans += x;
			continue;
		}
		
		break;				
	}
	
	if (last_full == n - 1)
	{
		assert(treba <= 0);
		return ans;
	}
	
	treba -= a[last_full + 1].top;
	ans += x;
	
	LL vykyn = -treba;
	
	int best = 0;
	FOR(i, 0, last_full + 1)	
		best = max(best, get(a[i], vykyn));
		
	assert(best < x);
	ans -= best;
	return ans;
}

int main()
{
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	//freopen("In.txt", "r", stdin);
	//freopen("In.txt", "w", stdout);

	cin >> n >> x;
	FOR(i, 0, n)
		a[i].read();
	
	LL treba = 0;
	sort(a, a + n, cmp);
	FOR(i, 0, n)
		treba += a[i].l * a[i].b;
	
	cout << min(solve1(treba), solve2(treba)) << endl;
	cerr << "Time elapsed: " << clock() / (double)CLOCKS_PER_SEC << endl;
	return 0;
}


Compiler output:
a.cc: In function 'LL solve1(LL)':
a.cc:78:26: error: no matching function for call to 'min(int&, LL)'
   78 |                 tut = min(tut, skiky(a[i], treba));
      |                       ~~~^~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:78:26: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'LL' {aka 'long long int'})
   78 |                 tut = min(tut, skiky(a[i], treba));
      |                       ~~~^~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:78:26: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   78 |                 tut = min(tut, skiky(a[i], treba));
      |                       ~~~^~~~~~~~~~~~~~~~~~~~~~~~~
a.cc: In function 'LL solve2(LL)':
a.cc:127:27: error: no matching function for call to 'max(int&, LL)'
  127 |                 best = max(best, get(a[i], vykyn));
      |                        ~~~^~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:127:27: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'LL' {aka 'long long int'})
  127 |                 best = max(best, get(a[i], vykyn));
      |                        ~~~^~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:127:27: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
  127 |                 best = max(best, get(a[i], vykyn));
      |                        ~~~^~~~~~~~~~~~~~~~~~~~~~~~