Problem p03019 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <tuple>
#include <unordered_map>
#include <vector>

#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()
#define pb push_back

using namespace std;

typedef long long ll;
typedef double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef unsigned int uint;
typedef unsigned long long ull;

mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
// default_random_engine generator;
// mt19937 rnd(1);

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  int n;
  ll x;
  cin >> n >> x;

  vector<ll> b(n), l(n), r(n);

  for (int i = 0; i < n; ++i) {
    cin >> b[i] >> l[i] >> r[i];
  }

  vector<pll> score(n);

  ll init = 0;
  for (int i = 0; i < n; ++i) {
    // cout << b[i] * l[i] + (x - b[i]) * r[i] << endl;
    score[i] = {b[i] * l[i] + (x - b[i]) * r[i], i};
    init += b[i] * l[i];
  }

  sort(all(score));
  reverse(all(score));

  // for (int i = 0; i < n; ++i) {
  //     cout << score[i].first << " " << score[i].second << endl;
  // }

  ll lf = -1, rg = x * n;

  while (rg - lf > 1) {
    int mid = (lf + rg) / 2;

    vector<int> used(n);

    ll cnt = mid / x;
    ll rem = mid % x;

    ll res = 0;

    for (int i = 0; i <= cnt; ++i) {
      used[score[i].second] = 1;
      res += score[i].first;
    }

    bool ok = false;
    for (int i = 0; i < n; ++i) {
      ll cur = min(rem, b[i]) * l[i] + max(rem - b[i], 0ll) * r[i];
      // cout << cur << endl;
      cur += res;
      // cout << cur << endl;
      // cout << cnt << " " << rem << endl;

      if (used[i]) {
        cur -= b[i] * l[i] + (x - b[i]) * r[i];
      } else {
        int j = score[cnt].second;
        cur -= b[j] * l[j] + (x - b[j]) * r[j];
      }

      if (cur >= init) {
        ok = true;
      }

      // cout << cur << " " << init << endl;
    }

    if (ok) {
      rg = mid;
    } else {
      lf = mid;
    }
  }

  cout << rg << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <tuple>
#include <unordered_map>
#include <vector>

#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()
#define pb push_back

using namespace std;

typedef long long ll;
typedef double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef unsigned int uint;
typedef unsigned long long ull;

mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
// default_random_engine generator;
// mt19937 rnd(1);

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  int n;
  ll x;
  cin >> n >> x;

  vector<ll> b(n), l(n), r(n);

  for (int i = 0; i < n; ++i) {
    cin >> b[i] >> l[i] >> r[i];
  }

  vector<pll> score(n);

  ll init = 0;
  for (int i = 0; i < n; ++i) {
    // cout << b[i] * l[i] + (x - b[i]) * r[i] << endl;
    score[i] = {b[i] * l[i] + (x - b[i]) * r[i], i};
    init += b[i] * l[i];
  }

  sort(all(score));
  reverse(all(score));

  // for (int i = 0; i < n; ++i) {
  //     cout << score[i].first << " " << score[i].second << endl;
  // }

  ll lf = -1, rg = x * n;

  while (rg - lf > 1) {
    ll mid = (lf + rg) / 2;

    vector<int> used(n);

    ll cnt = mid / x;
    ll rem = mid % x;

    ll res = 0;

    for (int i = 0; i <= cnt; ++i) {
      used[score[i].second] = 1;
      res += score[i].first;
    }

    bool ok = false;
    for (int i = 0; i < n; ++i) {
      ll cur = min(rem, b[i]) * l[i] + max(rem - b[i], 0ll) * r[i];
      // cout << cur << endl;
      cur += res;
      // cout << cur << endl;
      // cout << cnt << " " << rem << endl;

      if (used[i]) {
        cur -= b[i] * l[i] + (x - b[i]) * r[i];
      } else {
        int j = score[cnt].second;
        cur -= b[j] * l[j] + (x - b[j]) * r[j];
      }

      if (cur >= init) {
        ok = true;
      }

      // cout << cur << " " << init << endl;
    }

    if (ok) {
      rg = mid;
    } else {
      lf = mid;
    }
  }

  cout << rg << endl;
}