Problem p03019 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long lli;
typedef pair<int, int> pii;
const int maxn = 1e5 + 5;
int n, X;
int b[maxn], l[maxn], u[maxn];
lli empt[maxn], full[maxn], delta[maxn];

int order[maxn];

bool cmp(int i, int j) { return delta[i] > delta[j]; }

void ReadInput() {
  cin >> n >> X;
  for (int i = 0; i < n; ++i) {
    cin >> b[i] >> l[i] >> u[i];
    empt[i] = -1LL * b[i] * l[i];
    full[i] = 1LL * (X - b[i]) * u[i];
    delta[i] = full[i] - empt[i];
  }
  for (int i = 0; i < n; ++i)
    order[i] = i;
  sort(order, order + n, cmp);
}

lli score(int i, lli k) {
  if (k >= b[i])
    return 1LL * b[i] * l[i] + (k - b[i]) * u[i];
  return k * l[i];
}

bool Check(lli k) {
  int q = k / X, r = k % X;
  lli tot = 0;
  for (int i = 0; i < n; ++i)
    tot += empt[i];
  for (int i = 0; i < q; ++i)
    tot += delta[order[i]];
  for (int i = 0; i < n; ++i) {
    lli ntot = tot;
    if (i >= q)
      ntot += score(order[i], r);
    else {
      ntot -= delta[order[i]];
      ntot += delta[order[q]];
      ntot += score(order[i], r);
    }
    if (ntot >= 0)
      return true;
  }
  return false;
}

void Solve() {
  lli low = 0, high = 1LL * X * n;
  while (low <= high) {
    int mid = (low + high) / 2;
    if (Check(mid))
      high = mid - 1;
    else
      low = mid + 1;
  }
  cout << low << '\n';
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  ReadInput();
  Solve();
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long lli;
typedef pair<int, int> pii;
const int maxn = 1e5 + 5;
int n, X;
int b[maxn], l[maxn], u[maxn];
lli empt[maxn], full[maxn], delta[maxn];

int order[maxn];

bool cmp(int i, int j) { return delta[i] > delta[j]; }

void ReadInput() {
  cin >> n >> X;
  for (int i = 0; i < n; ++i) {
    cin >> b[i] >> l[i] >> u[i];
    empt[i] = -1LL * b[i] * l[i];
    full[i] = 1LL * (X - b[i]) * u[i];
    delta[i] = full[i] - empt[i];
  }
  for (int i = 0; i < n; ++i)
    order[i] = i;
  sort(order, order + n, cmp);
}

lli score(int i, lli k) {
  if (k >= b[i])
    return 1LL * b[i] * l[i] + (k - b[i]) * u[i];
  return k * l[i];
}

bool Check(lli k) {
  int q = k / X, r = k % X;
  lli tot = 0;
  for (int i = 0; i < n; ++i)
    tot += empt[i];
  for (int i = 0; i < q; ++i)
    tot += delta[order[i]];
  for (int i = 0; i < n; ++i) {
    lli ntot = tot;
    if (i >= q)
      ntot += score(order[i], r);
    else {
      ntot -= delta[order[i]];
      ntot += delta[order[q]];
      ntot += score(order[i], r);
    }
    if (ntot >= 0)
      return true;
  }
  return false;
}

void Solve() {
  lli low = 0, high = 1LL * X * n;
  while (low <= high) {
    lli mid = (low + high) / 2;
    if (Check(mid))
      high = mid - 1;
    else
      low = mid + 1;
  }
  cout << low << '\n';
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  ReadInput();
  Solve();
}