Problem p03019 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define LOCAL
using std::to_string;
auto to_string(std::string s) -> std::string { return '"' + s + '"'; }
auto to_string(char c) -> std::string { return "'" + std::string{c} + "'"; }
auto to_string(const char *s) -> std::string {
  return to_string((std::string)s);
}
auto to_string(bool b) -> std::string { return (b ? "true" : "false"); }
template <typename T, typename U>
auto to_string(std::pair<T, U> p) -> std::string {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <size_t N> auto to_string(std::bitset<N> bs) -> std::string {
  std::string res{};
  for (size_t i = 0; i < N; i++)
    res.insert(res.begin(), bs.test(i) ? '1' : '0');
  return res;
}
template <typename T> auto to_string(T v) -> std::string {
  bool flg = false;
  std::string res = "{";
  for (auto const &x : v) {
    if (flg)
      res += ", ";
    else
      flg = true;
    res += to_string(x);
  }
  res += "}";
  return res;
}
void debug_out() { std::cerr << std::endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  std::cerr << " " << to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...)                                                             \
  std::cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
int main() {
  std::cin.tie(0);
  std::cin.sync_with_stdio(false);
  size_t n;
  long long X;
  std::cin >> n >> X;
  std::vector<long long> b(n), l(n), u(n), full(n);
  for (size_t i = 0; i < n; i++) {
    std::cin >> b.at(i) >> l.at(i) >> u.at(i);
    full.at(i) = b.at(i) * l.at(i) + (X - b.at(i)) * u.at(i);
  }
  std::vector<size_t> ord(n);
  std::iota(ord.begin(), ord.end(), 0);
  std::sort(ord.begin(), ord.end(),
            [&](auto i, auto j) { return full.at(i) > full.at(j); });
  auto cal = [&](size_t i, long long x) {
    assert(i < n);
    assert(0 <= x && x <= X);
    auto j = ord.at(i);
    if (x <= b.at(j))
      return -(b.at(j) - x) * l.at(j);
    else
      return (x - b.at(j)) * u.at(j);
  };
  auto diff = [&](size_t i, long long x, long long y) {
    return cal(i, y) - cal(i, x);
  };
  auto check = [&](long long time) {
    if (time > X * (long long)n)
      return true;
    size_t q = time / X;
    long long r = time % X;
    assert(q <= n);
    assert(0LL <= r && r < X);
    long long base = 0;
    for (size_t i = 0; i < q; i++) {
      base += cal(i, X);
    }
    for (size_t i = q; i < n; i++) {
      base += cal(i, 0);
    }
    for (size_t i = 0; i < n; i++) {
      auto score = base;
      if (i < q) {
        auto a = diff(i, X, r);
        auto b = diff(q, 0, X);
        score += a + b;
      } else {
        auto c = diff(i, 0, r);
        score += c;
      }
      if (score >= 0)
        return true;
      debug(i, score);
    }
    return false;
  };
  long long ok = X * (long long)n, ng = -1, md;
  while (abs(ok - ng) > 1) {
    md = (ok + ng) >> 1;
    if (check(md))
      ok = md;
    else
      ng = md;
  }
  std::cout << ok << std::endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define LOCAL
using std::to_string;
auto to_string(std::string s) -> std::string { return '"' + s + '"'; }
auto to_string(char c) -> std::string { return "'" + std::string{c} + "'"; }
auto to_string(const char *s) -> std::string {
  return to_string((std::string)s);
}
auto to_string(bool b) -> std::string { return (b ? "true" : "false"); }
template <typename T, typename U>
auto to_string(std::pair<T, U> p) -> std::string {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <size_t N> auto to_string(std::bitset<N> bs) -> std::string {
  std::string res{};
  for (size_t i = 0; i < N; i++)
    res.insert(res.begin(), bs.test(i) ? '1' : '0');
  return res;
}
template <typename T> auto to_string(T v) -> std::string {
  bool flg = false;
  std::string res = "{";
  for (auto const &x : v) {
    if (flg)
      res += ", ";
    else
      flg = true;
    res += to_string(x);
  }
  res += "}";
  return res;
}
void debug_out() { std::cerr << std::endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  std::cerr << " " << to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...)                                                             \
  std::cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
int main() {
  std::cin.tie(0);
  std::cin.sync_with_stdio(false);
  size_t n;
  long long X;
  std::cin >> n >> X;
  std::vector<long long> b(n), l(n), u(n), full(n);
  for (size_t i = 0; i < n; i++) {
    std::cin >> b.at(i) >> l.at(i) >> u.at(i);
    full.at(i) = b.at(i) * l.at(i) + (X - b.at(i)) * u.at(i);
  }
  std::vector<size_t> ord(n);
  std::iota(ord.begin(), ord.end(), 0);
  std::sort(ord.begin(), ord.end(),
            [&](auto i, auto j) { return full.at(i) > full.at(j); });
  auto cal = [&](size_t i, long long x) {
    assert(i < n);
    assert(0 <= x && x <= X);
    auto j = ord.at(i);
    if (x <= b.at(j))
      return -(b.at(j) - x) * l.at(j);
    else
      return (x - b.at(j)) * u.at(j);
  };
  auto diff = [&](size_t i, long long x, long long y) {
    return cal(i, y) - cal(i, x);
  };
  auto check = [&](long long time) {
    if (time > X * (long long)n)
      return true;
    size_t q = time / X;
    long long r = time % X;
    assert(q <= n);
    assert(0LL <= r && r < X);
    long long base = 0;
    for (size_t i = 0; i < q; i++) {
      base += cal(i, X);
    }
    for (size_t i = q; i < n; i++) {
      base += cal(i, 0);
    }
    for (size_t i = 0; i < n; i++) {
      auto score = base;
      if (i < q) {
        auto a = diff(i, X, r);
        auto b = diff(q, 0, X);
        score += a + b;
      } else {
        auto c = diff(i, 0, r);
        score += c;
      }
      if (score >= 0)
        return true;
    }
    return false;
  };
  long long ok = X * (long long)n, ng = -1, md;
  while (abs(ok - ng) > 1) {
    md = (ok + ng) >> 1;
    if (check(md))
      ok = md;
    else
      ng = md;
  }
  std::cout << ok << std::endl;
  return 0;
}