Language: C++

#include <bits/stdc++.h>
#define M 200010
#define PLL pair <ll, ll>
#define x first
#define y second

using namespace std;

typedef long long ll;

int n, X;

ll B[M], L[M], U[M];

struct node {
	ll A, B;
	node() {A = 0, B = 1;}
	node(ll _1, ll _2) {A = _1, B = _2;}
};

ll need = 0;

PLL Pr[M];

ll Sum[M], W[M];

bool cmp(PLL A, PLL B) {return A.x > B.x;}

ll check(ll x, ll forb, ll w) {
	ll fin = 0;
	if(forb <= x) fin = Sum[x + 1] - Pr[forb].x; else fin = Sum[x];
	if(fin > need) return X * x;
	if(fin + Pr[forb].x < need) return 1e18 + x;
	if(fin + L[w] * B[w] >= need) {
		ll res = need - fin;
		return X * x + res / L[w] + (res % L[w] != 0);
	} else {
		ll res = need - fin - L[w] * B[w];
		return X * x + B[w] + res / U[w] + (res % U[w] != 0);
	} 
}

int main() {
	scanf("%d%d", &n, &X);
	for(int i = 1; i <= n; i++) scanf("%lld%lld%lld", &B[i], &L[i], &U[i]), need += B[i] * L[i], Pr[i] = PLL((U[i] * (X - B[i]) + L[i] * B[i]), i);
	sort(Pr + 1, Pr + n + 1, cmp);
	for(int i = 1; i <= n; i++) Sum[i] = Sum[i - 1] + Pr[i].x, W[Pr[i].y] = i;
	ll ans = 1e18;
	for(int i = 1; i <= n; i++) {
		int w = W[i];
		ll l = 0, r = n - 1, nw = 0;
		for(; l <= r; ) {
			int md = (l + r) / 2;
			ll fin = w <= md ? Sum[md + 1] - Pr[w].x : Sum[md];
			if(fin > need) r = md - 1; else nw = md, l = md + 1;
		}
		for(int j = max(nw - 3, 0); j <= min(nw + 3, n - 1); j++)
		ans = min(ans, check(j, w, i));
	}
	printf("%lld\n", ans);
	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:57:32: error: no matching function for call to 'max(ll, int)'
   57 |                 for(int j = max(nw - 3, 0); j <= min(nw + 3, n - 1); j++)
      |                             ~~~^~~~~~~~~~~
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: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:57:32: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   57 |                 for(int j = max(nw - 3, 0); j <= min(nw + 3, n - 1); j++)
      |                             ~~~^~~~~~~~~~~
/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
In file included from /usr/include/c++/14/algorithm:61:
/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:57:32: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   57 |                 for(int j = max(nw - 3, 0); j <= min(nw + 3, n - 1); j++)
      |                             ~~~^~~~~~~~~~~
a.cc:57:53: error: no matching function for call to 'min(ll, int)'
   57 |                 for(int j = max(nw - 3, 0); j <= min(nw + 3, n - 1); j++)
      |                                                  ~~~^~~~~~~~~~~~~~~
/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:57:53: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   57 |                 for(int j = max(nw - 3, 0); j <= min(nw + 3, n - 1); j++)
      |                                                  ~~~^~~~~~~~~~~~~~~
/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
/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:57:53: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   57 |                 for(int j = max(nw - 3, 0); j <= min(nw + 3, n - 1); j++)
      |                                                  ~~~^~~~~~~~~~~~~~~