Problem p03019 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
const ll INF = 1LL << 60;
const ll MOD = 1000000007;
template <class T> bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}
template <class C> void print(const C &c, std::ostream &os = std::cout) {
  std::copy(std::begin(c), std::end(c),
            std::ostream_iterator<typename C::value_type>(os, " "));
  os << std::endl;
}

int main() {
  int n, x;
  cin >> n >> x;
  struct test {
    ll b, l, u, maxx;
    ll operator()(ll x) {
      if (x <= b)
        return l * x;
      else
        return l * b + u * (x - b);
    }
    bool operator<(const test &rhs) const { return maxx > rhs.maxx; };
  };
  vector<test> tests;
  ll sumb = 0;
  for (int i = 0; i < n; ++i) {
    ll b, l, u;
    cin >> b >> l >> u;
    sumb -= b * l;
    ll maxx = l * b + u * (x - b);
    test t = {b, l, u, maxx};
    tests.push_back(t);
  }
  if (sumb == 0) {
    cout << 0 << "\n";
    return 0;
  }
  sort(tests.begin(), tests.end());
  vl cumsum(n, 0);
  cumsum[0] = tests[0].maxx;
  for (int i = 1; i < n; ++i) {
    cumsum[i] = cumsum[i - 1] + tests[i].maxx;
  }
  auto check = [&](ll mid) {
    for (int i = 0; i < n; ++i) {
      ll fullmark = mid / x;
      if (fullmark >= n) {
        return true;
      }
      ll score = sumb + tests[i](mid % x);
      if (fullmark != 0)
        score += cumsum[fullmark - 1];
      if (fullmark - 1 >= i) {
        score -= tests[i].maxx;
        score += tests[fullmark].maxx;
      }
      if (score >= 0)
        return true;
    }
    return false;
  };
  ll ok = n * x;
  ll ng = 0;
  while (abs(ok - ng) != 1) {
    ll mid = (ng + ok) / 2;
    if (check(mid))
      ok = mid;
    else
      ng = mid;
  }
  cout << ok << "\n";
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
const ll INF = 1LL << 60;
const ll MOD = 1000000007;
template <class T> bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}
template <class C> void print(const C &c, std::ostream &os = std::cout) {
  std::copy(std::begin(c), std::end(c),
            std::ostream_iterator<typename C::value_type>(os, " "));
  os << std::endl;
}

int main() {
  ll n, x;
  cin >> n >> x;
  struct test {
    ll b, l, u, maxx;
    ll operator()(ll x) {
      if (x <= b)
        return l * x;
      else
        return l * b + u * (x - b);
    }
    bool operator<(const test &rhs) const { return maxx > rhs.maxx; };
  };
  vector<test> tests;
  ll sumb = 0;
  for (int i = 0; i < n; ++i) {
    ll b, l, u;
    cin >> b >> l >> u;
    sumb -= b * l;
    ll maxx = l * b + u * (x - b);
    test t = {b, l, u, maxx};
    tests.push_back(t);
  }
  if (sumb == 0) {
    cout << 0 << "\n";
    return 0;
  }
  sort(tests.begin(), tests.end());
  vl cumsum(n, 0);
  cumsum[0] = tests[0].maxx;
  for (int i = 1; i < n; ++i) {
    cumsum[i] = cumsum[i - 1] + tests[i].maxx;
  }
  auto check = [&](ll mid) {
    for (int i = 0; i < n; ++i) {
      ll fullmark = mid / x;
      if (fullmark >= n) {
        return true;
      }
      ll score = sumb + tests[i](mid % x);
      if (fullmark != 0)
        score += cumsum[fullmark - 1];
      if (fullmark - 1 >= i) {
        score -= tests[i].maxx;
        score += tests[fullmark].maxx;
      }
      if (score >= 0)
        return true;
    }
    return false;
  };
  ll ok = n * x;
  ll ng = 0;
  while (abs(ok - ng) != 1) {
    ll mid = (ng + ok) / 2;
    if (check(mid))
      ok = mid;
    else
      ng = mid;
  }
  cout << ok << "\n";
  return 0;
}