Problem p03019 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#ifdef ENABLE_DEBUG
#define dump(a) cerr << #a << "=" << a << endl
#define dumparr(a, n) cerr << #a << "[" << n << "]=" << a[n] << endl
#else
#define dump(a)
#define dumparr(a, n)
#endif
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define For(i, a) FOR(i, 0, a)
#define REV(i, a, b) for (int i = b - 1; i >= a; i--)
#define Rev(i, a) REV(i, 0, a)
#define REP(a) For(i, a)

typedef long long int ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> ppll;
typedef vector<ll> vll;

const ll INF = (1LL << 50);

template <class S, class T>
std::ostream &operator<<(std::ostream &os, pair<S, T> a) {
  os << "(" << a.first << "," << a.second << ")";
  return os;
}
template <class T> std::ostream &operator<<(std::ostream &os, vector<T> a) {
  os << "[ ";
  REP(a.size()) { os << a[i] << " "; }
  os << " ]";
  return os;
}

void solve(long long N, long long X, std::vector<long long> b,
           std::vector<long long> l, std::vector<long long> u) {
  ll lf = -1, rt = accumulate(b.begin(), b.end(), 0) * 10;
  vector<pll> p;
  REP(N) { p.push_back(pll(l[i] * b[i] + u[i] * (X - b[i]), i)); }
  sort(p.begin(), p.end(), greater<pll>());
  vector<ll> sump;
  sump.push_back(0);
  REP(N) { sump.push_back(sump.back() + p[i].first); }
  dump(p);
  dump(sump);
  ll bsum = 0;
  REP(N) { bsum += b[i] * l[i]; }
  while (lf + 1 < rt) {
    ll mid = (lf + rt) / 2;
    ll ans = 0;
    if (mid % X)
      REP(N) {
        ll sum = 0;

        if (i < mid / X) {
          sum = sump[mid / X + 1] - p[i].first;
        } else {
          sum = sump[mid / X];
        }
        ll idx = p[i].second;
        if (mid % X < b[idx]) {
          sum += (mid % X) * l[idx];
        } else {
          sum += (mid % X - b[idx]) * u[idx] + b[idx] * l[idx];
        }
        ans = max(ans, sum);
        if (ans >= bsum)
          break;
      }
    else {
      ans = sump[mid / X];
    }
    if (ans >= bsum) {
      rt = mid;
    } else {
      lf = mid;
    }
  }
  cout << rt << endl;
}

int main() {
  long long N;
  scanf("%lld", &N);
  long long X;
  scanf("%lld", &X);
  std::vector<long long> b(N);
  std::vector<long long> l(N);
  std::vector<long long> u(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &b[i]);
    scanf("%lld", &l[i]);
    scanf("%lld", &u[i]);
  }
  solve(N, X, std::move(b), std::move(l), std::move(u));
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#ifdef ENABLE_DEBUG
#define dump(a) cerr << #a << "=" << a << endl
#define dumparr(a, n) cerr << #a << "[" << n << "]=" << a[n] << endl
#else
#define dump(a)
#define dumparr(a, n)
#endif
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define For(i, a) FOR(i, 0, a)
#define REV(i, a, b) for (int i = b - 1; i >= a; i--)
#define Rev(i, a) REV(i, 0, a)
#define REP(a) For(i, a)

typedef long long int ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> ppll;
typedef vector<ll> vll;

const ll INF = (1LL << 50);

template <class S, class T>
std::ostream &operator<<(std::ostream &os, pair<S, T> a) {
  os << "(" << a.first << "," << a.second << ")";
  return os;
}
template <class T> std::ostream &operator<<(std::ostream &os, vector<T> a) {
  os << "[ ";
  REP(a.size()) { os << a[i] << " "; }
  os << " ]";
  return os;
}

void solve(long long N, long long X, std::vector<long long> b,
           std::vector<long long> l, std::vector<long long> u) {
  ll lf = -1, rt = accumulate(b.begin(), b.end(), 0LL);
  vector<pll> p;
  REP(N) { p.push_back(pll(l[i] * b[i] + u[i] * (X - b[i]), i)); }
  sort(p.begin(), p.end(), greater<pll>());
  vector<ll> sump;
  sump.push_back(0);
  REP(N) { sump.push_back(sump.back() + p[i].first); }
  dump(p);
  dump(sump);
  ll bsum = 0;
  REP(N) { bsum += b[i] * l[i]; }
  while (lf + 1 < rt) {
    ll mid = (lf + rt) / 2;
    ll ans = 0;
    if (mid % X)
      REP(N) {
        ll sum = 0;

        if (i < mid / X) {
          sum = sump[mid / X + 1] - p[i].first;
        } else {
          sum = sump[mid / X];
        }
        ll idx = p[i].second;
        if (mid % X < b[idx]) {
          sum += (mid % X) * l[idx];
        } else {
          sum += (mid % X - b[idx]) * u[idx] + b[idx] * l[idx];
        }
        ans = max(ans, sum);
        if (ans >= bsum)
          break;
      }
    else {
      ans = sump[mid / X];
    }
    if (ans >= bsum) {
      rt = mid;
    } else {
      lf = mid;
    }
  }
  cout << rt << endl;
}

int main() {
  long long N;
  scanf("%lld", &N);
  long long X;
  scanf("%lld", &X);
  std::vector<long long> b(N);
  std::vector<long long> l(N);
  std::vector<long long> u(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &b[i]);
    scanf("%lld", &l[i]);
    scanf("%lld", &u[i]);
  }
  solve(N, X, std::move(b), std::move(l), std::move(u));
  return 0;
}