Language: C++

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cassert>
#include <map>
using namespace std;
int x;
struct Subj {
  int64_t b, l, r;

  int64_t GetSum() const {
    return b * 1ll * l + r * 1ll * (x - b);
  }

  int64_t GetSum(int64_t cnt) const {
    if (cnt <= b) {
      return l * cnt;
    } else {
      return l * b + r * (cnt - b);
    }
  }
};
int64_t ans = 1e18;
void RelaxAnswer(int64_t d) {
  ans = min(ans, d);
}

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

  int n;
  cin >> n >> x;
  vector<Subj> subj(n + 1);
  vector<int64_t> sums(n + 1);
  int64_t cur_sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> subj[i].b >> subj[i].l >> subj[i].r;
    cur_sum -= subj[i].l * subj[i].b;
  }
  subj[n].b = 0;
  subj[n].l = 0;
  subj[n].r = 0;
  n++;

  sort(subj.begin(), subj.end(), [](const Subj& lhs, const Subj& rhs) {
      return lhs.GetSum() < rhs.GetSum();
  });
  for (int i = 0; i < n; i++) {
    if (i == 0) {
      sums[i] = subj[i].GetSum();
    } else {
      sums[i] = subj[i].GetSum() + sums[i - 1];
    }
  }
  auto get_on_on = [&](int l, int r) {
    if (l > r) {
      return 0ll;
    }
    int64_t ll = l >= 1 ? sums[l - 1] : 0ll;
    return sums[r] - ll;
  };
  auto get_on = [&](int l, int id, int64_t val, int r) {
    if (id < l || id > r) {
      return get_on_on(l, r) + val;
    }
    return get_on_on(l, id - 1) + get_on_on(id + 1, r) + val;
  };

  {

    auto can_m = [&](int64_t check_val) {

      for (int i = n - 1; i >= 0; i--) {

        auto i_sum = subj[i].GetSum((check_val % x));
        if (i_sum + cur_sum >= 0) {
          return true;
        }

        int l = 0;
        int r = n - 1;

        auto check_bin = [&](int m) {
          if (get_on(n - 1 - m, i, i_sum, n - 1) + cur_sum >= 0) {
            return true;
          } else {
            return false;
          }
        };

        while (r - l > 1) {
          int m = (l + r) / 2;
          if (check_bin(m)) {
            r = m;
          } else {
            l = m;
          }
        }
        for (int v : {l, r}) {
          if (check_bin(v)) {
            uint64_t id_sum = (v + 1) * 1ll * x;
            if (n - v - 1 <= i) {
              id_sum -= (x - (check_val % x));
            }
            if (id_sum <= check_val) {
              return true;
            }
          }
        }
      }
      return false;
    };



    int64_t l = 0;
    int64_t r = 1e18;



    while (r - l > 1) {
      int64_t m = (l + r) / 2;
      if (can_m(m)) {
        r = m;
      } else {
        l = m;
      }
    }
    RelaxAnswer(r);
  }

  cout << ans << endl;
  return 0;
}

Compiler output:
a.cc: In lambda function:
a.cc:62:20: error: inconsistent types 'long long int' and 'long int' deduced for lambda return type
   62 |     return sums[r] - ll;
a.cc: In lambda function:
a.cc:103:13: error: 'uint64_t' was not declared in this scope
  103 |             uint64_t id_sum = (v + 1) * 1ll * x;
      |             ^~~~~~~~
a.cc:7:1: note: 'uint64_t' is defined in header '<cstdint>'; this is probably fixable by adding '#include <cstdint>'
    6 | #include <map>
  +++ |+#include <cstdint>
    7 | using namespace std;
a.cc:105:15: error: 'id_sum' was not declared in this scope; did you mean 'i_sum'?
  105 |               id_sum -= (x - (check_val % x));
      |               ^~~~~~
      |               i_sum
a.cc:107:17: error: 'id_sum' was not declared in this scope; did you mean 'i_sum'?
  107 |             if (id_sum <= check_val) {
      |                 ^~~~~~
      |                 i_sum