#include <bits/stdc++.h>   using namespace std;   const int seed = 131;   const long long LLMAX = 2e18;   const int MOD = 1e9 + 7;   const double eps = 1e-8;   const int MAXN = 1e5 + 10;   const int hmod1 = 0x48E2DCE7;   const int hmod2 = 0x60000005;   inline long long sqr(long long x) { return x * x; }   inline int sqr(int x) { return x * x; }   inline double sqr(double x) { return x * x; }   long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }   long long exgcd(long long a, long long b, long long &x, long long &y) {     long long d;     (b == 0 ? (x = 1, y = 0, d = a)             : (d = exgcd(b, a % b, y, x), y -= a / b * x));     return d;   }   long long qpow(long long a, long long n) {     long long sum = 1;     while (n) {       if (n & 1) sum = sum * a % MOD;       a = a * a % MOD;       n >>= 1;     }     return sum;   }   inline int dcmp(double x) {     if (fabs(x) < eps) return 0;     return (x > 0 ? 1 : -1);   }   long long need[MAXN], tim[MAXN], v[MAXN][2], cnt = 0;   struct node {     long long id, a, b;     node() {}     node(long long x, long long y, long long z) { id = x, a = y, b = z; }     bool operator<(const node &n) const {       if (a - b == n.a - n.b) return a > n.a;       return a - b > n.a - n.b;     }   } in[MAXN];   long long check(long long n, long long a, long long b) {     long long sum = 0;     for (int i = 0; i <= n; i++) tim[i] = need[i];     for (int i = 0; i < cnt; i++) {       if (tim[in[i].id]) {         long long minn = min(tim[in[i].id], a);         sum += in[i].a * minn;         tim[in[i].id] -= minn;         a -= minn;       }       if (tim[in[i].id]) {         long long minn = min(tim[in[i].id], b);         sum += in[i].b * minn;         tim[in[i].id] -= minn;         b -= minn;       }     }     return sum;   }   int main(void) {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long n, s, sum = 0, ans = 0;     cin >> n >> s;     for (int i = 0; i < n; i++) {       cin >> need[i] >> v[i][0] >> v[i][1];       sum += need[i];       in[cnt++] = node(i, v[i][0], v[i][1]);     }     long long all = (sum + s - 1) / s, l = 0, r = all;     need[n] = all * s - sum;     in[cnt++] = node(n, 0, 0);     sort(in, in + cnt);     while (l + 3 < r) {       long long m1 = (l + r) / 2, m2 = (m1 + r) / 2;       if (check(n, m1 * s, (all - m1) * s) > check(n, m2 * s, (all - m2) * s))         r = m2;       else         l = m1;     }     for (long long i = l; i <= r; i++)       ans = max(ans, check(n, i * s, (all - i) * s));     cout << ans << endl;     return 0;   }   