Language: C++

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, pii> pipii;
typedef pair<pii, int> ppiii;
typedef vector<vector<ll>> matrix;

int n, x;
pipii l[100005]; // <hours, <lower, upper>>

ll gain(pipii a, ll h) {
  assert(h <= x);
  ll ret = 0;
  ret += min(a.first, h) * (ll)a.second.first;
  ret += max(0, h - a.first) * (ll)a.second.second;
  return ret;
}

bool can(ll t) {
  ll need = 0;
  for(int i = 0; i < n; i++) {
    need += l[i].first * (ll)l[i].second.first;
  }
  vector<pipii> leftover;
  for(int i = 0; i < n; i++) {
    if(t >= x) {
      need -= gain(l[i], x);
      t -= x;
    }
    else {
      leftover.push_back(l[i]);
    }
  }
  if(t) {
    assert(t < x);
    ll maxInc = 0;
    for(pipii out: leftover) {
      maxInc = max(maxInc, gain(out, t));
    }
    need -= maxInc;
  }
  return need <= 0;
}

bool cmp(pipii a, pipii b) {
  return gain(a, x) > gain(b, x);
}

void solve() {
  cin >> n >> x;
  for(int i = 0; i < n; i++) {
    cin >> l[i].first >> l[i].second.first >> l[i].second.second;
  }
  sort(l, l+n, cmp);
  ll lhs = 0;
  ll rhs = n * (ll)x;
  while(lhs != rhs) {
    ll mid = (lhs+rhs)/2;
    if(can(mid)) rhs = mid;
    else lhs = mid+1;
  }
  cout << lhs << "\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL); cout.tie(NULL);
  /*
  int t;
  cin >> t;
  for(int i = 1; i <= t; i++) {
    cout << "Case #" << i << ": ";
    solve();
  }
  */
  solve();
}


Compiler output:
a.cc: In function 'll gain(pipii, ll)':
a.cc:32:13: error: no matching function for call to 'min(int&, ll&)'
   32 |   ret += min(a.first, h) * (ll)a.second.first;
      |          ~~~^~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 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:32:13: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'll' {aka 'long long int'})
   32 |   ret += min(a.first, h) * (ll)a.second.first;
      |          ~~~^~~~~~~~~~~~
/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:32:13: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   32 |   ret += min(a.first, h) * (ll)a.second.first;
      |          ~~~^~~~~~~~~~~~
a.cc:33:13: error: no matching function for call to 'max(int, ll)'
   33 |   ret += max(0, h - a.first) * (ll)a.second.second;
      |          ~~~^~~~~~~~~~~~~~~~
/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:33:13: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'll' {aka 'long long int'})
   33 |   ret += max(0, h - a.first) * (ll)a.second.second;
      |          ~~~^~~~~~~~~~~~~~~~
/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:33:13: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   33 |   ret += max(0, h - a.first) * (ll)a.second.second;
      |          ~~~^~~~~~~~~~~~~~~~