func_code_string: #include <bits/stdc++.h>   using namespace std;   const int M = 1e5 + 10;   int n, S;   long long tot;   struct Node {     long long val;     long long num;     long long sum;     bool operator<(const Node &rhs) const { return num < rhs.num; }   };   vector<Node> a, b;   long long work(long long ls, vector<Node> &a) {     int id = upper_bound(a.begin(), a.end(), Node{0, ls}) - a.begin();     long long ret = 0;     if (--id >= 0) {       ret = a[id].sum;     }     if (id + 1 != a.size()) {       long long left = ls - (id == -1 ? 0 : a[id].num);       ret += left * a[id + 1].val;     }     return ret;   }   long long cal(long long x) {     long long ret = work(x * S, a);     ret += work((tot - x) * S, b);     return ret;   }   long long solve(long long l, long long r) {     while (r > l) {       long long m1 = (2 * l + r) / 3;       long long m2 = (2 * r + l + 2) / 3;       long long ret1 = cal(m1);       long long ret2 = cal(m2);       if (ret1 > ret2)         r = m2 - 1;       else         l = m1 + 1;     }     return cal(l);   }   int main() {     scanf( %d%d , &n, &S);     long long cnt = 0;     long long now = 0;     for (int i = 0; i < n; ++i) {       int x, y, z;       scanf( %d%d%d , &x, &y, &z);       cnt += x;       now += 1ll * x * min(y, z);       if (y > z)         a.emplace_back(Node{1ll * y - z, 1ll * x, 1ll * (y - z) * x});       else if (y < z)         b.emplace_back(Node{1ll * z - y, 1ll * x, 1ll * (z - y) * x});     }     sort(a.begin(), a.end(), [](Node x, Node y) { return x.val > y.val; });     sort(b.begin(), b.end(), [](Node x, Node y) { return x.val > y.val; });     tot = cnt / S + (cnt % S > 0);     for (int i = 0; i < a.size(); ++i) {       if (i) a[i].sum += a[i - 1].sum, a[i].num += a[i - 1].num;     }     for (int i = 0; i < b.size(); ++i) {       if (i) b[i].sum += b[i - 1].sum, b[i].num += b[i - 1].num;     }     printf( %I64d n , solve(0, tot) + now);     return 0;   }   