// MIPT Shock Content (place 19)

#include <bits/stdc++.h>

#define all(a) (a).begin(), (a).end()
#define pb push_back
#define sz(a) (int)(a).size()

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef double ld;

const ll inf = 1e18;

int n;
ll s;
vector<ll> x, a, b;
vector<pll> vct;
ll k;
ll sum;

ll solve(ll second) {
    ll ans = 0;
    for (int i = 0; i < n; ++i) {
        ans += a[i] * x[i];
    }
    if (second == 0) {
        return ans;
    }
    ll curr = sum - (k - second) * s;
    //cout << sum << " " << s << " " << k << " " << second << " " << curr << "\n";
    int ptr = 0;
    while (curr) {
        if (curr >= vct[ptr].second) {
            ans += vct[ptr].second * vct[ptr].first;
            curr -= vct[ptr].second;
            ++ptr;
            continue;
        } else {
            ans += vct[ptr].first * curr;
            break;
        }
    }
    ll init = sum - (k - second) * s;
    ll rem = second * s - init;
    while (rem && ptr < sz(vct) && vct[ptr].first > 0) {
        ll z = vct[ptr].second;
        if (curr) {
            z -= curr;
            curr = 0;
        }
        if (rem >= z) {
            ans += vct[ptr].first * z;
            rem -= z;
        } else {
            ans += vct[ptr].first * rem;
            break;
        }
    }
    return ans;
}

int main() {

    //ifstream cin("input.txt");
    //ofstream cout("output.txt");
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

    cin >> n >> s;

    x.resize(n);
    a.resize(n);
    b.resize(n);
    for (int i = 0; i < n; ++i) {
        cin >> x[i] >> a[i] >> b[i];
    }

    for (int i = 0; i < n; ++i) {
        vct.pb({b[i] - a[i], x[i]});
    }
    sort(all(vct));
    reverse(all(vct));

    sum = 0;
    for (int i = 0; i < n; ++i) {
        sum += x[i];
    }
    k = (sum + s - 1) / s;

    ll lf = 0, rg = k;
    //cout << k << "\n";

    while (rg - lf > 2) {
        ll l = (lf * 2 + rg) / 3;
        ll r = (rg * 2 + lf) / 3;
        ll ansl = solve(l);
        ll ansr = solve(r);
        if (ansl > ansr) {
            rg = r;
        } else {
            lf = l;
        }
    }

    //cout << solve(3) << "\n";

    ll ans = -inf;
    for (ll z = lf; z <= rg; ++z) {
        //cout << solve(z) << "\n";
        ans = max(ans, solve(z));
    }
    cout << ans << "\n";

}


// MIPT Shock Content (place 19)

#include <bits/stdc++.h>

#define all(a) (a).begin(), (a).end()
#define pb push_back
#define sz(a) (int)(a).size()

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef double ld;

const ll inf = 1e18;

int n;
ll s;
vector<ll> x, a, b;
vector<pll> vct;
ll k;
ll sum;

ll solve(ll second) {
    ll ans = 0;
    for (int i = 0; i < n; ++i) {
        ans += a[i] * x[i];
    }
    if (second == 0) {
        return ans;
    }
    if (second == k) {
        ans = 0;
        for (int i = 0; i < n; ++i) {
            ans += b[i] * x[i];
        }
        return ans;
    }
    ll curr = sum - (k - second) * s;
    //cout << sum << " " << s << " " << k << " " << second << " " << curr << "\n";
    int ptr = 0;
    while (curr) {
        if (curr >= vct[ptr].second) {
            ans += vct[ptr].second * vct[ptr].first;
            curr -= vct[ptr].second;
            ++ptr;
            continue;
        } else {
            ans += vct[ptr].first * curr;
            break;
        }
    }
    ll init = sum - (k - second) * s;
    ll rem = second * s - init;
    while (rem && ptr < sz(vct) && vct[ptr].first > 0) {
        ll z = vct[ptr].second;
        if (curr) {
            z -= curr;
            curr = 0;
        }
        if (rem >= z) {
            ans += vct[ptr].first * z;
            rem -= z;
        } else {
            ans += vct[ptr].first * rem;
            break;
        }
    }
    return ans;
}

int main() {

    //ifstream cin("input.txt");
    //ofstream cout("output.txt");
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

    cin >> n >> s;

    x.resize(n);
    a.resize(n);
    b.resize(n);
    for (int i = 0; i < n; ++i) {
        cin >> x[i] >> a[i] >> b[i];
    }

    for (int i = 0; i < n; ++i) {
        vct.pb({b[i] - a[i], x[i]});
    }
    sort(all(vct));
    reverse(all(vct));

    sum = 0;
    for (int i = 0; i < n; ++i) {
        sum += x[i];
    }
    k = (sum + s - 1) / s;

    ll lf = 0, rg = k;
    //cout << k << "\n";

    while (rg - lf > 2) {
        ll l = (lf * 2 + rg) / 3;
        ll r = (rg * 2 + lf) / 3;
        ll ansl = solve(l);
        ll ansr = solve(r);
        if (ansl > ansr) {
            rg = r;
        } else {
            lf = l;
        }
    }

    //cout << solve(3) << "\n";

    ll ans = -inf;
    for (ll z = lf; z <= rg; ++z) {
        //cout << solve(z) << "\n";
        ans = max(ans, solve(z));
    }
    cout << ans << "\n";

}


// MIPT Shock Content (place 19)

#include <bits/stdc++.h>

#define all(a) (a).begin(), (a).end()
#define pb push_back
#define sz(a) (int)(a).size()

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef double ld;

const ll inf = 1e18;

int n;
ll s;
vector<ll> x, a, b;
vector<pll> vct;
ll k;
ll sum;

ll solve(ll second) {
    ll ans = 0;
    for (int i = 0; i < n; ++i) {
        ans += a[i] * x[i];
    }
    if (second == 0) {
        return ans;
    }
    if (second == k) {
        ans = 0;
        for (int i = 0; i < n; ++i) {
            ans += b[i] * x[i];
        }
        return ans;
    }
    ll curr = sum - (k - second) * s;
    //cout << sum << " " << s << " " << k << " " << second << " " << curr << "\n";
    int ptr = 0;
    while (curr) {
        if (curr >= vct[ptr].second) {
            ans += vct[ptr].second * vct[ptr].first;
            curr -= vct[ptr].second;
            ++ptr;
            continue;
        } else {
            ans += vct[ptr].first * curr;
            break;
        }
    }
    ll init = sum - (k - second) * s;
    ll rem = second * s - init;
    while (rem && ptr < sz(vct) && vct[ptr].first > 0) {
        ll z = vct[ptr].second;
        if (curr) {
            z -= curr;
            curr = 0;
        }
        if (rem >= z) {
            ans += vct[ptr].first * z;
            rem -= z;
            ++ptr;
        } else {
            ans += vct[ptr].first * rem;
            break;
        }
    }
    return ans;
}

int main() {

    //ifstream cin("input.txt");
    //ofstream cout("output.txt");
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

    cin >> n >> s;

    x.resize(n);
    a.resize(n);
    b.resize(n);
    for (int i = 0; i < n; ++i) {
        cin >> x[i] >> a[i] >> b[i];
    }

    for (int i = 0; i < n; ++i) {
        vct.pb({b[i] - a[i], x[i]});
    }
    sort(all(vct));

    sum = 0;
    for (int i = 0; i < n; ++i) {
        sum += x[i];
    }
    k = (sum + s - 1) / s;

    ll t = 0;
    for (int i = sz(vct) - 1; i >= 0; --i) {
        if (vct[i].first > 0) {
            t += vct[i].second;
        }
    }

    t /= s;
    reverse(all(vct));

    ll ans = -inf;
    for (ll z = t; z <= k && z <= t + 10; ++z) {
        ans = max(ans, solve(z));
    }

    cout << ans << "\n";

}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <ctype.h>
#include <deque>
#include <cstring>
#include <set>
#include <bitset>
#include <map>
#include <chrono>
#include <random>
#include <unordered_map>
#include <stdio.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef std::vector<int> vi;
typedef std::vector<bool> vb;
typedef std::vector<string> vs;
typedef std::vector<double> vd;
typedef std::vector<long long> vll;
typedef std::vector<std::vector<int> > vvi;
typedef vector<vll> vvll;
typedef std::vector<std::pair<int, int> > vpi;
typedef vector<vpi> vvpi;
typedef std::pair<int, int> pi;
typedef std::pair<ll, ll> pll;
typedef std::vector<pll> vpll;

const long long mod = 1000000007;
const unsigned gen_seed = std::chrono::system_clock::now().time_since_epoch().count();
std::mt19937_64 gen(gen_seed);

#define all(c) (c).begin(),(c).end()
#define forn(i, a, b) for(int i = a; i < b; i++)
#define read(x) scanf("%d", &x)
#define readv(x, n) vi x(n); forn(i,0,n) scanf("%d", &x[i])

#define pb push_back
#define mp make_pair

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "wt", stdout);
#endif
    int n,S;
    scanf("%d %d", &n, &S);
    vi a(n), b(n), s(n);
    forn(i,0,n) scanf("%d %d %d", &s[i], &a[i], &b[i]);
    ll sum = 0;
    forn(i,0,n) sum+=s[i];
    ll p = (sum+S-1)/S;
    ll ans = 0;
    vpll ap, bp;
    forn(i,0,n) {
        ans += s[i]*max(a[i],b[i]);
        if(a[i] >= b[i]) ap.pb(mp(a[i]-b[i], s[i]));
        else if(a[i] < b[i]) bp.pb(mp(b[i]-a[i], s[i]));
    }
    ll asum=0, bsum=-0;
    for(auto x : ap) asum+=x.second;
    for(auto x : bp) bsum+=x.second;
    vll al, bl;
    sort(all(ap));
    sort(all(bp));
    int ta = asum%S;
    int tb = bsum%S;
    int pt = 0;
    while(ta > 0) {
        int take = min(ta, (int)ap[pt].second);
        forn(i,0,take) al.pb(ap[pt].first);
        ta-=take;
        pt++;
    }
    pt = 0;
    while(tb > 0) {
        int take = min(tb, (int)bp[pt].second);
        forn(i,0,take) bl.pb(bp[pt].first);
        tb-=take;
        pt++;
    }
    if(al.size() + bl.size() > S) {
        cout<<ans;
        return 0;
    }
    ll c1 = 0, c2 = 0;
    for(auto x : al) c1+=x;
    for(auto x : bl) c2+=x;
    ans -= min(c1,c2);
    cout<<ans;

}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <ctype.h>
#include <deque>
#include <cstring>
#include <set>
#include <bitset>
#include <map>
#include <chrono>
#include <random>
#include <unordered_map>
#include <stdio.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef std::vector<int> vi;
typedef std::vector<bool> vb;
typedef std::vector<string> vs;
typedef std::vector<double> vd;
typedef std::vector<long long> vll;
typedef std::vector<std::vector<int> > vvi;
typedef vector<vll> vvll;
typedef std::vector<std::pair<int, int> > vpi;
typedef vector<vpi> vvpi;
typedef std::pair<int, int> pi;
typedef std::pair<ll, ll> pll;
typedef std::vector<pll> vpll;

const long long mod = 1000000007;
const unsigned gen_seed = std::chrono::system_clock::now().time_since_epoch().count();
std::mt19937_64 gen(gen_seed);

#define all(c) (c).begin(),(c).end()
#define forn(i, a, b) for(int i = a; i < b; i++)
#define read(x) scanf("%d", &x)
#define readv(x, n) vi x(n); forn(i,0,n) scanf("%d", &x[i])

#define pb push_back
#define mp make_pair

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "wt", stdout);
#endif
    int n,S;
    scanf("%d %d", &n, &S);
    vi a(n), b(n), s(n);
    forn(i,0,n) scanf("%d %d %d", &s[i], &a[i], &b[i]);
    ll ans = 0;
    vpll ap, bp;
    forn(i,0,n) {
        ans += ll(s[i])*ll(max(a[i],b[i]));
        if(a[i] >= b[i]) ap.pb(mp(a[i]-b[i], s[i]));
        else if(a[i] < b[i]) bp.pb(mp(b[i]-a[i], s[i]));
    }
    ll asum=0, bsum=-0;
    for(auto x : ap) asum+=x.second;
    for(auto x : bp) bsum+=x.second;
    vll al, bl;
    sort(all(ap));
    sort(all(bp));
    int ta = asum%S;
    int tb = bsum%S;
    int pt = 0;
    while(ta > 0) {
        int take = min(ta, (int)ap[pt].second);
        forn(i,0,take) al.pb(ap[pt].first);
        ta-=take;
        pt++;
    }
    pt = 0;
    while(tb > 0) {
        int take = min(tb, (int)bp[pt].second);
        forn(i,0,take) bl.pb(bp[pt].first);
        tb-=take;
        pt++;
    }
    if(al.size() + bl.size() > S) {
        cout<<ans;
        return 0;
    }
    ll c1 = 0, c2 = 0;
    for(auto x : al) c1+=x;
    for(auto x : bl) c2+=x;
    ans -= min(c1,c2);
    cout<<ans;

}


#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
using namespace std;

int main() {
	int n, s;
	cin >> n >> s;
	vector<pair<int, int> > v;
	int ans = 0;
	int sum = 0;
	for(int i=0; i<n; i++){
		int x, y, z;
		cin >> x >> y >> z;
		v.push_back(make_pair(y-z, x));
		ans += (x*y);
		sum += x;
	}
	int pizzas = ((sum-1)/s)+1;
	int left = (s*pizzas-sum);
	//cout << ans << endl;
	sort(v.begin(), v.end());
	int cura = 0;
	int curb = 0;
	while(1){
		//cout << ans << endl;
		while(cura < v.size() && curb >= v[cura].second){
			curb -= v[cura].second;
			cura++;
		}
		if(cura >= v.size()) break;
		if(v[cura].first>=0) break;
		if(v[cura].second - curb > s){
			ans += -1 * v[cura].first * s * ((v[cura].second - curb)/s);
			curb += s * ((v[cura].second - curb)/s);
			continue;
		}
		int eaten = 0;
		int howmuch = 0;
		while(eaten < s - left){
			//cout << ans << endl;
			//cout << eaten << " " << howmuch << endl;
			if(cura >= v.size()) break;
			int z = min(v[cura].second - curb, s - left - eaten);
			curb+=z;
			eaten +=z;
			howmuch+=(z) * -1 * v[cura].first;
			while(cura < v.size() && curb >= v[cura].second){
				curb -= v[cura].second;
				cura++;
			}
		}
		if(howmuch < 0) break;
		ans += howmuch;
		eaten = 0;

		while(eaten < left){
			if(cura >= v.size()) break;
			if(v[cura].first >= 0) break;
			int z = min(v[cura].second - curb, left - eaten);
			curb+=z;
			eaten += z;
			ans+=(z) * -1 * v[cura].first;
			while(cura < v.size() && curb >= v[cura].second){
				curb -= v[cura].second;
				cura++;
			}
		}
	}
	cout << ans << endl;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
using namespace std;

int main() {
	int n, s;
	cin >> n >> s;
	vector<pair<int, int> > v;
	int ans = 0;
	int sum = 0;
	for(int i=0; i<n; i++){
		int x, y, z;
		cin >> x >> y >> z;
		v.push_back(make_pair(y-z, x));
		ans += (x*y);
		sum += x;
	}
	int pizzas = ((sum-1)/s)+1;
	int left = (s*pizzas-sum);
	//cout << ans << endl;
	sort(v.begin(), v.end());
	int cura = 0;
	int curb = 0;
	while(1){
		//cout << ans << endl;
		while(cura < v.size() && curb >= v[cura].second){
			curb -= v[cura].second;
			cura++;
		}
		if(cura >= v.size()) break;
		if(v[cura].second - curb > s){
			ans += -1 * v[cura].first * s * ((v[cura].second - curb)/s);
			curb += s * ((v[cura].second - curb)/s);
			continue;
		}
		int eaten = 0;
		int howmuch = 0;
		while(eaten < s - left){
			//cout << ans << endl;
			//cout << eaten << " " << howmuch << endl;
			if(cura >= v.size()) break;
			int z = min(v[cura].second - curb, s - left - eaten);
			curb+=z;
			eaten +=z;
			howmuch+=(z) * -1 * v[cura].first;
			while(cura < v.size() && curb >= v[cura].second){
				curb -= v[cura].second;
				cura++;
			}
		}
		if(howmuch < 0) break;
		ans += howmuch;
		eaten = 0;

		while(eaten < left){
			if(cura >= v.size()) break;
			if(v[cura].first >= 0) break;
			int z = min(v[cura].second - curb, left - eaten);
			curb+=z;
			eaten += z;
			ans+=(z) * -1 * v[cura].first;
			while(cura < v.size() && curb >= v[cura].second){
				curb -= v[cura].second;
				cura++;
			}
		}
	}
	cout << ans << endl;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
using namespace std;

int main() {
	long long n, s;
	cin >> n >> s;
	vector<pair<long long , long long > > v;
	long long  ans = 0;
	long long  sum = 0;
	for(long long  i=0; i<n; i++){
		long long  x, y, z;
		cin >> x >> y >> z;
		v.push_back(make_pair(y-z, x));
		ans += (x*y);
		sum += x;
	}
	long long  pizzas = ((sum-1)/s)+1;
	long long  left = (s*pizzas-sum);
	//cout << ans << endl;
	sort(v.begin(), v.end());
	long long  cura = 0;
	long long  curb = 0;
	while(1){
		//cout << ans << endl;
		while(cura < v.size() && curb >= v[cura].second){
			curb -= v[cura].second;
			cura++;
		}
		if(cura >= v.size()) break;
		if(v[cura].first>=0) break;
		if(v[cura].second - curb > s){
			ans += -1 * v[cura].first * s * ((v[cura].second - curb)/s);
			curb += s * ((v[cura].second - curb)/s);
			continue;
		}
		long long  eaten = 0;
		long long  howmuch = 0;
		while(eaten < s - left){
			//cout << ans << endl;
			//cout << eaten << " " << howmuch << endl;
			if(cura >= v.size()) break;
			long long  z = min(v[cura].second - curb, s - left - eaten);
			curb+=z;
			eaten +=z;
			howmuch+=(z) * -1 * v[cura].first;
			while(cura < v.size() && curb >= v[cura].second){
				curb -= v[cura].second;
				cura++;
			}
		}
		if(howmuch < 0) break;
		ans += howmuch;
		eaten = 0;

		while(eaten < left){
			if(cura >= v.size()) break;
			if(v[cura].first >= 0) break;
			long long z = min(v[cura].second - curb, left - eaten);
			curb+=z;
			eaten += z;
			ans+=(z) * -1 * v[cura].first;
			while(cura < v.size() && curb >= v[cura].second){
				curb -= v[cura].second;
				cura++;
			}
		}
	}
	cout << ans << endl;
}


#include <bits/stdc++.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846264338327
#endif // M_PI
#define endl "\n"
#define S struct
#define X first
#define Y second
#define V vector
#ifndef __linux__
#define LLD "%I64d"
#else
#define LLD "%ll""d"
#endif
#define FOR(x, y, z) for (int x = (y); x < (z); ++x)
#define FORR(x, y, z) for (int x = (y); x > (z); --x)
#define GET(a, n) for (int __i = 0; __i < (n); ++__i) cin >> a[__i];
#define GETM(a, n, m) for (int __i = 0; __i < (n); ++__i) for (int __j = 0; __j < m; ++__j) cin >> a[__i][__j];
#define PRINTM(a, n, m) for (int __i = 0; __i < (n); ++__i) { for (int __j = 0; __j < m; ++__j) cout << a[__i][__j] << " ";  cout << endl; };
#define PRINT(a, n) for (int __i = 0; __i < (n); ++__i) cout << a[__i] << " ";
#define IT(a) a.begin(), a.end()
#define SQR(x) (x) * (x)
#define CASE(a, s) cout << "Case #" << a << ": " << s << endl;
#define DEB(a) cout << #a << " = " << (a) << endl; cout.flush();
#define DEBA(a) for (auto __i: a) cout << __i << " "; cout << endl; cout.flush();
#define IFDEB(b, a) if (b) { cout << #a << " = " << (a) << endl; cout.flush(); }
#define YESNO(cond) cout << ((cond) ? "Yes" : "No") << endl;
#define UMAX(left, right) left = max(left, right);
#define UMIN(left, right) left = min(left, right);
using namespace std;
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef pair <int, int> PII;
typedef pair <LL, LL> PLL;
const int MOD = 1000000007;
void yes() { cout << "Yes" << endl;}
void no() { cout << "No" << endl;}
S Sync_stdio { Sync_stdio() { cin.tie(NULL); ios_base::sync_with_stdio(false); } } _sync_stdio;
S FAIL { FAIL () { cout << "CHANGE!!!" << endl;}};

S Z
{
    LL s;
    LL a;
    LL b;
    bool operator < (const Z &other)
    {
        return a - b < other.a - other.b;
    }
};

LL s;

LL solve(LL current, const vector <Z> &v, LL one)
{
    int j = 0;
    LL j_count = 0;
    int n = v.size();
    vector <LL> ps(n + 1);
    FOR (i, 0, n) {
        ps[i + 1] = v[i].s;
    }
    FOR (i, 1, n) {
        ps[i + 1] += ps[i];
    }
    one = max(0LL, min(one, ps.back() / s));
    ps.push_back(ps.back());
    while (j < n && ps[j + 1] < s * one) {
        current += (v[j].s - j_count) * (v[j].b - v[j].a);
        j_count = 0;
        ++j;
    }
    if (s * one != ps[j]) {
        LL j_count_add = s * one - (ps[j] + j_count);
        current += j_count_add * (v[j].b - v[j].a);
        j_count += j_count_add;
    }
        //DEB(j);
        //DEB(j_count);
        //DEB(current);
    return current;
}

int main()
{
    LL n;
    cin >> n >> s;
    vector <Z> v(n);
    FOR (i, 0, n) {
        cin >> v[i].s >> v[i].a >> v[i].b;
    }
    LL pizza_count = 0;
    FOR (i, 0, n) {
        pizza_count += v[i].s;
    }
    if (pizza_count % s) {
        v.push_back({s - pizza_count % s, 0, 0});
        ++n;
    }
    sort(IT(v));
    pizza_count = (pizza_count + s - 1) / s;
    LL best = 0;
    LL current = 0;
    FOR (i, 0, n) {
        current += v[i].a * v[i].s;
    }
    LL res1 = 0;
    LL res2 = 0;
    FOR (i, 0, n) {
        if (v[i].a < v[i].b) {
            res1 += v[i].s;
        } else if (v[i].a == v[i].b) {
            res2 += v[i].s;
        }
    }
    cout << max({current,
                 solve(current, v, res1 / s),
                 solve(current, v, (res1 + s - 1) / s),
                 solve(current, v, res2 / s),
                 solve(current, v, res2 / s)});
    /*FOR (one, 1, pizza_count + 1) {
        while (j < n && ps[j + 1] < s * one) {
            current += (v[j].s - j_count) * (v[j].b - v[j].a);
            j_count = 0;
            ++j;
        }
        if (s * one != ps[j]) {
            LL j_count_add = s * one - (ps[j] + j_count);
            current += j_count_add * (v[j].b - v[j].a);
            j_count += j_count_add;
        }
        //DEB(j);
        //DEB(j_count);
        //DEB(current);
        best = max(best, current);
    }*/
    return 0;
}


#include <bits/stdc++.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846264338327
#endif // M_PI
#define endl "\n"
#define S struct
#define X first
#define Y second
#define V vector
#ifndef __linux__
#define LLD "%I64d"
#else
#define LLD "%ll""d"
#endif
#define FOR(x, y, z) for (int x = (y); x < (z); ++x)
#define FORR(x, y, z) for (int x = (y); x > (z); --x)
#define GET(a, n) for (int __i = 0; __i < (n); ++__i) cin >> a[__i];
#define GETM(a, n, m) for (int __i = 0; __i < (n); ++__i) for (int __j = 0; __j < m; ++__j) cin >> a[__i][__j];
#define PRINTM(a, n, m) for (int __i = 0; __i < (n); ++__i) { for (int __j = 0; __j < m; ++__j) cout << a[__i][__j] << " ";  cout << endl; };
#define PRINT(a, n) for (int __i = 0; __i < (n); ++__i) cout << a[__i] << " ";
#define IT(a) a.begin(), a.end()
#define SQR(x) (x) * (x)
#define CASE(a, s) cout << "Case #" << a << ": " << s << endl;
#define DEB(a) cout << #a << " = " << (a) << endl; cout.flush();
#define DEBA(a) for (auto __i: a) cout << __i << " "; cout << endl; cout.flush();
#define IFDEB(b, a) if (b) { cout << #a << " = " << (a) << endl; cout.flush(); }
#define YESNO(cond) cout << ((cond) ? "Yes" : "No") << endl;
#define UMAX(left, right) left = max(left, right);
#define UMIN(left, right) left = min(left, right);
using namespace std;
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef pair <int, int> PII;
typedef pair <LL, LL> PLL;
const int MOD = 1000000007;
void yes() { cout << "Yes" << endl;}
void no() { cout << "No" << endl;}
S Sync_stdio { Sync_stdio() { cin.tie(NULL); ios_base::sync_with_stdio(false); } } _sync_stdio;
S FAIL { FAIL () { cout << "CHANGE!!!" << endl;}};

S Z
{
    LL s;
    LL a;
    LL b;
    bool operator < (const Z &other)
    {
        return a - b < other.a - other.b;
    }
};

int main()
{
    LL n, s;
    cin >> n >> s;
    vector <Z> v(n);
    FOR (i, 0, n) {
        cin >> v[i].s >> v[i].a >> v[i].b;
    }
    LL pizza_count = 0;
    FOR (i, 0, n) {
        pizza_count += v[i].s;
    }
    if (pizza_count % s) {
        v.push_back({s - pizza_count % s, 0, 0});
        ++n;
    }
    sort(IT(v));
    pizza_count = (pizza_count + s - 1) / s;
    LL best = 0;
    LL current = 0;
    FOR (i, 0, n) {
        current += v[i].a * v[i].s;
    }
    best = current;
    int j = 0;
    LL j_count = 0;
    vector <LL> ps(n + 1);
    FOR (i, 0, n) {
        ps[i + 1] = v[i].s;
    }
    FOR (i, 1, n) {
        ps[i + 1] += ps[i];
    }
    //DEB(current);
    //DEB(pizza_count);
    //DEBA(ps);
    ps.push_back(ps.back());
    FOR (one, 1, pizza_count + 1) {
        while (j < n && ps[j + 1] < s * one) {
            current += (v[j].s - j_count) * (v[j].b - v[j].a);
            j_count = 0;
            ++j;
        }
        if (s * one != ps[j]) {
            LL j_count_add = s * one - (ps[j] + j_count);
            current += j_count_add * (v[j].b - v[j].a);
            j_count += j_count_add;
        }
        //DEB(j);
        //DEB(j_count);
        //DEB(current);
        best = max(best, current);
    }
    cout << best;
    return 0;
}


#include <bits/stdc++.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846264338327
#endif // M_PI
#define endl "\n"
#define S struct
#define X first
#define Y second
#define V vector
#ifndef __linux__
#define LLD "%I64d"
#else
#define LLD "%ll""d"
#endif
#define FOR(x, y, z) for (int x = (y); x < (z); ++x)
#define FORR(x, y, z) for (int x = (y); x > (z); --x)
#define GET(a, n) for (int __i = 0; __i < (n); ++__i) cin >> a[__i];
#define GETM(a, n, m) for (int __i = 0; __i < (n); ++__i) for (int __j = 0; __j < m; ++__j) cin >> a[__i][__j];
#define PRINTM(a, n, m) for (int __i = 0; __i < (n); ++__i) { for (int __j = 0; __j < m; ++__j) cout << a[__i][__j] << " ";  cout << endl; };
#define PRINT(a, n) for (int __i = 0; __i < (n); ++__i) cout << a[__i] << " ";
#define IT(a) a.begin(), a.end()
#define SQR(x) (x) * (x)
#define CASE(a, s) cout << "Case #" << a << ": " << s << endl;
#define DEB(a) cout << #a << " = " << (a) << endl; cout.flush();
#define DEBA(a) for (auto __i: a) cout << __i << " "; cout << endl; cout.flush();
#define IFDEB(b, a) if (b) { cout << #a << " = " << (a) << endl; cout.flush(); }
#define YESNO(cond) cout << ((cond) ? "Yes" : "No") << endl;
#define UMAX(left, right) left = max(left, right);
#define UMIN(left, right) left = min(left, right);
using namespace std;
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef pair <int, int> PII;
typedef pair <LL, LL> PLL;
const int MOD = 1000000007;
void yes() { cout << "Yes" << endl;}
void no() { cout << "No" << endl;}
S Sync_stdio { Sync_stdio() { cin.tie(NULL); ios_base::sync_with_stdio(false); } } _sync_stdio;
S FAIL { FAIL () { cout << "CHANGE!!!" << endl;}};

S Z
{
    LL s;
    LL a;
    LL b;
    bool operator < (const Z &other)
    {
        return a - b < other.a - other.b;
    }
};

int main()
{
    LL n, s;
    cin >> n >> s;
    vector <Z> v(n);
    FOR (i, 0, n) {
        cin >> v[i].s >> v[i].a >> v[i].b;
    }
    LL pizza_count = 0;
    FOR (i, 0, n) {
        pizza_count += v[i].s;
    }
    if (pizza_count % s) {
        v.push_back({s - pizza_count % s, 0, 0});
        ++n;
    }
    sort(IT(v));
    pizza_count = (pizza_count + s - 1) / s;
    LL best = 0;
    LL current = 0;
    FOR (i, 0, n) {
        current += v[i].a * v[i].s;
    }
    best = current;
    int j = 0;
    LL j_count = 0;
    vector <LL> ps(n + 1);
    FOR (i, 0, n) {
        ps[i + 1] = v[i].s;
    }
    FOR (i, 1, n) {
        ps[i + 1] += ps[i];
    }
    //DEB(current);
    //DEB(pizza_count);
    //DEBA(ps);
    ps.push_back(ps.back());
    FOR (one, 1, pizza_count + 1) {
        while (j < n && ps[j + 1] < s * one) {
            current += (v[j].s - j_count) * (v[j].b - v[j].a);
            j_count = 0;
            ++j;
        }
        if (s * one != ps[j]) {
            j_count = s * one - ps[j];
            current += j_count * (v[j].b - v[j].a);
        }
        //DEB(j);
        //DEB(j_count);
        //DEB(current);
        best = max(best, current);
    }
    cout << best;
    return 0;
}


#include <bits/stdc++.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846264338327
#endif // M_PI
#define endl "\n"
#define S struct
#define X first
#define Y second
#define V vector
#ifndef __linux__
#define LLD "%I64d"
#else
#define LLD "%ll""d"
#endif
#define FOR(x, y, z) for (int x = (y); x < (z); ++x)
#define FORR(x, y, z) for (int x = (y); x > (z); --x)
#define GET(a, n) for (int __i = 0; __i < (n); ++__i) cin >> a[__i];
#define GETM(a, n, m) for (int __i = 0; __i < (n); ++__i) for (int __j = 0; __j < m; ++__j) cin >> a[__i][__j];
#define PRINTM(a, n, m) for (int __i = 0; __i < (n); ++__i) { for (int __j = 0; __j < m; ++__j) cout << a[__i][__j] << " ";  cout << endl; };
#define PRINT(a, n) for (int __i = 0; __i < (n); ++__i) cout << a[__i] << " ";
#define IT(a) a.begin(), a.end()
#define SQR(x) (x) * (x)
#define CASE(a, s) cout << "Case #" << a << ": " << s << endl;
#define DEB(a) cout << #a << " = " << (a) << endl; cout.flush();
#define DEBA(a) for (auto __i: a) cout << __i << " "; cout << endl; cout.flush();
#define IFDEB(b, a) if (b) { cout << #a << " = " << (a) << endl; cout.flush(); }
#define YESNO(cond) cout << ((cond) ? "Yes" : "No") << endl;
#define UMAX(left, right) left = max(left, right);
#define UMIN(left, right) left = min(left, right);
using namespace std;
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef pair <int, int> PII;
typedef pair <LL, LL> PLL;
const int MOD = 1000000007;
void yes() { cout << "Yes" << endl;}
void no() { cout << "No" << endl;}
S Sync_stdio { Sync_stdio() { cin.tie(NULL); ios_base::sync_with_stdio(false); } } _sync_stdio;
S FAIL { FAIL () { cout << "CHANGE!!!" << endl;}};

S Z
{
    LL s;
    LL a;
    LL b;
    bool operator < (const Z &other)
    {
        return a - b < other.a - other.b;
    }
};

LL s;

LL solve(LL current, const vector <Z> &v, Ll one)
{
    int j = 0;
    LL j_count = 0;
    int n = v.size();
    vector <LL> ps(n + 1);
    FOR (i, 0, n) {
        ps[i + 1] = v[i].s;
    }
    FOR (i, 1, n) {
        ps[i + 1] += ps[i];
    }
    one = max(0LL, min(one, ps.back() / s);
    ps.push_back(ps.back());
    while (j < n && ps[j + 1] < s * one) {
        current += (v[j].s - j_count) * (v[j].b - v[j].a);
        j_count = 0;
        ++j;
    }
    if (s * one != ps[j]) {
        LL j_count_add = s * one - (ps[j] + j_count);
        current += j_count_add * (v[j].b - v[j].a);
        j_count += j_count_add;
    }
        //DEB(j);
        //DEB(j_count);
        //DEB(current);
    return current;
}

int main()
{
    LL n;
    cin >> n >> s;
    vector <Z> v(n);
    FOR (i, 0, n) {
        cin >> v[i].s >> v[i].a >> v[i].b;
    }
    LL pizza_count = 0;
    FOR (i, 0, n) {
        pizza_count += v[i].s;
    }
    if (pizza_count % s) {
        v.push_back({s - pizza_count % s, 0, 0});
        ++n;
    }
    sort(IT(v));
    pizza_count = (pizza_count + s - 1) / s;
    LL best = 0;
    LL current = 0;
    FOR (i, 0, n) {
        current += v[i].a * v[i].s;
    }
    LL res1 = 0;
    LL res2 = 0;
    FOR (i, 0, n) {
        if (v[i].a < v[i].b) {
            res1 += v[i].s;
        } else if (v[i].a == v[i].b) {
            res2 += v[i].s;
        }
    }
    cout << max({current,
                 solve(current, v, res1 / s),
                 solve(current, v, (res1 + s - 1) / s),
                 solve(current, v, res2 / s),
                 solve(current, v, res2 / s)});
    /*FOR (one, 1, pizza_count + 1) {
        while (j < n && ps[j + 1] < s * one) {
            current += (v[j].s - j_count) * (v[j].b - v[j].a);
            j_count = 0;
            ++j;
        }
        if (s * one != ps[j]) {
            LL j_count_add = s * one - (ps[j] + j_count);
            current += j_count_add * (v[j].b - v[j].a);
            j_count += j_count_add;
        }
        //DEB(j);
        //DEB(j_count);
        //DEB(current);
        best = max(best, current);
    }*/
    return 0;
}


#include <bits/stdc++.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846264338327
#endif // M_PI
#define endl "\n"
#define S struct
#define X first
#define Y second
#define V vector
#ifndef __linux__
#define LLD "%I64d"
#else
#define LLD "%ll""d"
#endif
#define FOR(x, y, z) for (int x = (y); x < (z); ++x)
#define FORR(x, y, z) for (int x = (y); x > (z); --x)
#define GET(a, n) for (int __i = 0; __i < (n); ++__i) cin >> a[__i];
#define GETM(a, n, m) for (int __i = 0; __i < (n); ++__i) for (int __j = 0; __j < m; ++__j) cin >> a[__i][__j];
#define PRINTM(a, n, m) for (int __i = 0; __i < (n); ++__i) { for (int __j = 0; __j < m; ++__j) cout << a[__i][__j] << " ";  cout << endl; };
#define PRINT(a, n) for (int __i = 0; __i < (n); ++__i) cout << a[__i] << " ";
#define IT(a) a.begin(), a.end()
#define SQR(x) (x) * (x)
#define CASE(a, s) cout << "Case #" << a << ": " << s << endl;
#define DEB(a) cout << #a << " = " << (a) << endl; cout.flush();
#define DEBA(a) for (auto __i: a) cout << __i << " "; cout << endl; cout.flush();
#define IFDEB(b, a) if (b) { cout << #a << " = " << (a) << endl; cout.flush(); }
#define YESNO(cond) cout << ((cond) ? "Yes" : "No") << endl;
#define UMAX(left, right) left = max(left, right);
#define UMIN(left, right) left = min(left, right);
using namespace std;
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef pair <int, int> PII;
typedef pair <LL, LL> PLL;
const int MOD = 1000000007;
void yes() { cout << "Yes" << endl;}
void no() { cout << "No" << endl;}
S Sync_stdio { Sync_stdio() { cin.tie(NULL); ios_base::sync_with_stdio(false); } } _sync_stdio;
S FAIL { FAIL () { cout << "CHANGE!!!" << endl;}};

S Z
{
    LL s;
    LL a;
    LL b;
    bool operator < (const Z &other)
    {
        return a - b < other.a - other.b;
    }
};

int main()
{
    int n, s;
    cin >> n >> s;
    vector <Z> v(n);
    FOR (i, 0, n) {
        cin >> v[i].s >> v[i].a >> v[i].b;
    }
    LL pizza_count = 0;
    FOR (i, 0, n) {
        pizza_count += v[i].s;
    }
    if (pizza_count % s) {
        v.push_back({s - pizza_count % s, 0, 0});
        ++n;
    }
    sort(IT(v));
    pizza_count = (pizza_count + s - 1) / s;
    LL best = 0;
    LL current = 0;
    FOR (i, 0, n) {
        current += v[i].a * v[i].s;
    }
    best = current;
    int j = 0;
    LL j_count = 0;
    vector <LL> ps(n + 1);
    FOR (i, 0, n) {
        ps[i + 1] = v[i].s;
    }
    FOR (i, 1, n + 1) {
        ps[i + 1] += ps[i];
    }
    //DEB(current);
    //DEB(pizza_count);
    //DEBA(ps);
    ps.push_back(ps.back());
    FOR (one, 1, pizza_count + 1) {
        while (j < n && ps[j + 1] < s * one) {
            current += (v[j].s - j_count) * (v[j].b - v[j].a);
            j_count = 0;
            ++j;
        }
        if (s * one != ps[j]) {
            j_count = s * one - ps[j];
            current += j_count * (v[j].b - v[j].a);
        }
        //DEB(j);
        //DEB(j_count);
        //DEB(current);
        best = max(best, current);
    }
    cout << best;
    return 0;
}


#include <bits/stdc++.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846264338327
#endif // M_PI
#define endl "\n"
#define S struct
#define X first
#define Y second
#define V vector
#ifndef __linux__
#define LLD "%I64d"
#else
#define LLD "%ll""d"
#endif
#define FOR(x, y, z) for (int x = (y); x < (z); ++x)
#define FORR(x, y, z) for (int x = (y); x > (z); --x)
#define GET(a, n) for (int __i = 0; __i < (n); ++__i) cin >> a[__i];
#define GETM(a, n, m) for (int __i = 0; __i < (n); ++__i) for (int __j = 0; __j < m; ++__j) cin >> a[__i][__j];
#define PRINTM(a, n, m) for (int __i = 0; __i < (n); ++__i) { for (int __j = 0; __j < m; ++__j) cout << a[__i][__j] << " ";  cout << endl; };
#define PRINT(a, n) for (int __i = 0; __i < (n); ++__i) cout << a[__i] << " ";
#define IT(a) a.begin(), a.end()
#define SQR(x) (x) * (x)
#define CASE(a, s) cout << "Case #" << a << ": " << s << endl;
#define DEB(a) cout << #a << " = " << (a) << endl; cout.flush();
#define DEBA(a) for (auto __i: a) cout << __i << " "; cout << endl; cout.flush();
#define IFDEB(b, a) if (b) { cout << #a << " = " << (a) << endl; cout.flush(); }
#define YESNO(cond) cout << ((cond) ? "Yes" : "No") << endl;
#define UMAX(left, right) left = max(left, right);
#define UMIN(left, right) left = min(left, right);
using namespace std;
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef pair <int, int> PII;
typedef pair <LL, LL> PLL;
const int MOD = 1000000007;
void yes() { cout << "Yes" << endl;}
void no() { cout << "No" << endl;}
S Sync_stdio { Sync_stdio() { cin.tie(NULL); ios_base::sync_with_stdio(false); } } _sync_stdio;
S FAIL { FAIL () { cout << "CHANGE!!!" << endl;}};

S Z
{
    LL s;
    LL a;
    LL b;
    bool operator < (const Z &other)
    {
        return a - b < other.a - other.b;
    }
};

int main()
{
    int n, s;
    cin >> n >> s;
    vector <Z> v(n);
    FOR (i, 0, n) {
        cin >> v[i].s >> v[i].a >> v[i].b;
    }
    LL pizza_count = 0;
    FOR (i, 0, n) {
        pizza_count += v[i].s;
    }
    if (pizza_count % s) {
        v.push_back({s - pizza_count % s, 0, 0});
        ++n;
    }
    sort(IT(v));
    pizza_count = (pizza_count + s - 1) / s;
    LL best = 0;
    LL current = 0;
    FOR (i, 0, n) {
        current += v[i].a * v[i].s;
    }
    best = current;
    int j = 0;
    LL j_count = 0;
    vector <LL> ps(n + 1);
    FOR (i, 0, n) {
        ps[i + 1] = v[i].s;
    }
    FOR (i, 1, n) {
        ps[i + 1] += ps[i];
    }
    //DEB(current);
    //DEB(pizza_count);
    //DEBA(ps);
    ps.push_back(ps.back());
    FOR (one, 1, pizza_count + 1) {
        while (j < n && ps[j + 1] < s * one) {
            current += (v[j].s - j_count) * (v[j].b - v[j].a);
            j_count = 0;
            ++j;
        }
        if (s * one != ps[j]) {
            j_count = s * one - ps[j];
            current += j_count * (v[j].b - v[j].a);
        }
        //DEB(j);
        //DEB(j_count);
        //DEB(current);
        best = max(best, current);
    }
    cout << best;
    return 0;
}


#include <bits/stdc++.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846264338327
#endif // M_PI
#define endl "\n"
#define S struct
#define X first
#define Y second
#define V vector
#ifndef __linux__
#define LLD "%I64d"
#else
#define LLD "%ll""d"
#endif
#define FOR(x, y, z) for (int x = (y); x < (z); ++x)
#define FORR(x, y, z) for (int x = (y); x > (z); --x)
#define GET(a, n) for (int __i = 0; __i < (n); ++__i) cin >> a[__i];
#define GETM(a, n, m) for (int __i = 0; __i < (n); ++__i) for (int __j = 0; __j < m; ++__j) cin >> a[__i][__j];
#define PRINTM(a, n, m) for (int __i = 0; __i < (n); ++__i) { for (int __j = 0; __j < m; ++__j) cout << a[__i][__j] << " ";  cout << endl; };
#define PRINT(a, n) for (int __i = 0; __i < (n); ++__i) cout << a[__i] << " ";
#define IT(a) a.begin(), a.end()
#define SQR(x) (x) * (x)
#define CASE(a, s) cout << "Case #" << a << ": " << s << endl;
#define DEB(a) cout << #a << " = " << (a) << endl; cout.flush();
#define DEBA(a) for (auto __i: a) cout << __i << " "; cout << endl; cout.flush();
#define IFDEB(b, a) if (b) { cout << #a << " = " << (a) << endl; cout.flush(); }
#define YESNO(cond) cout << ((cond) ? "Yes" : "No") << endl;
#define UMAX(left, right) left = max(left, right);
#define UMIN(left, right) left = min(left, right);
using namespace std;
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef pair <int, int> PII;
typedef pair <LL, LL> PLL;
const int MOD = 1000000007;
void yes() { cout << "Yes" << endl;}
void no() { cout << "No" << endl;}
S Sync_stdio { Sync_stdio() { cin.tie(NULL); ios_base::sync_with_stdio(false); } } _sync_stdio;
S FAIL { FAIL () { cout << "CHANGE!!!" << endl;}};

S Z
{
    LL s;
    LL a;
    LL b;
    bool operator < (const Z &other)
    {
        return a - b < other.a - other.b;
    }
};

LL s;

LL solve(LL current, const vector <Z> &v, LL one)
{
    int j = 0;
    LL j_count = 0;
    int n = v.size();
    vector <LL> ps(n + 1);
    FOR (i, 0, n) {
        ps[i + 1] = v[i].s;
    }
    FOR (i, 1, n) {
        ps[i + 1] += ps[i];
    }
    one = max(0LL, min(one, ps.back() / s);
    ps.push_back(ps.back());
    while (j < n && ps[j + 1] < s * one) {
        current += (v[j].s - j_count) * (v[j].b - v[j].a);
        j_count = 0;
        ++j;
    }
    if (s * one != ps[j]) {
        LL j_count_add = s * one - (ps[j] + j_count);
        current += j_count_add * (v[j].b - v[j].a);
        j_count += j_count_add;
    }
        //DEB(j);
        //DEB(j_count);
        //DEB(current);
    return current;
}

int main()
{
    LL n;
    cin >> n >> s;
    vector <Z> v(n);
    FOR (i, 0, n) {
        cin >> v[i].s >> v[i].a >> v[i].b;
    }
    LL pizza_count = 0;
    FOR (i, 0, n) {
        pizza_count += v[i].s;
    }
    if (pizza_count % s) {
        v.push_back({s - pizza_count % s, 0, 0});
        ++n;
    }
    sort(IT(v));
    pizza_count = (pizza_count + s - 1) / s;
    LL best = 0;
    LL current = 0;
    FOR (i, 0, n) {
        current += v[i].a * v[i].s;
    }
    LL res1 = 0;
    LL res2 = 0;
    FOR (i, 0, n) {
        if (v[i].a < v[i].b) {
            res1 += v[i].s;
        } else if (v[i].a == v[i].b) {
            res2 += v[i].s;
        }
    }
    cout << max({current,
                 solve(current, v, res1 / s),
                 solve(current, v, (res1 + s - 1) / s),
                 solve(current, v, res2 / s),
                 solve(current, v, res2 / s)});
    /*FOR (one, 1, pizza_count + 1) {
        while (j < n && ps[j + 1] < s * one) {
            current += (v[j].s - j_count) * (v[j].b - v[j].a);
            j_count = 0;
            ++j;
        }
        if (s * one != ps[j]) {
            LL j_count_add = s * one - (ps[j] + j_count);
            current += j_count_add * (v[j].b - v[j].a);
            j_count += j_count_add;
        }
        //DEB(j);
        //DEB(j_count);
        //DEB(current);
        best = max(best, current);
    }*/
    return 0;
}


#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <cmath>
#include <deque>
#include <algorithm>
#include <random>
#include <ctime>
using namespace std;

#define int int64_t
#define pb push_back
#define pbb pop_back
#define pf pop_front
#define pff push_front
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
typedef long double ld;
typedef long long ll;

const int INF = 1e9, MOD = 1e9 + 7, MAXN = 105;
const ld EPS = 1e-9, PI = acos(-1);

struct student
{
    int s, a, b;

    student()
    {
        s = 0, a = 0, b = 0;
    }
    student(int x, int y, int z)
    {
        s = x, a = y, b = z;
    }

    bool operator <(const student &other) const
    {
        return abs(a - b) < abs(other.a - other.b);
    }
};

istream &operator >>(istream &in, student &s)
{
    in >> s.s >> s.a >> s.b;
    return in;
}

int check(const vector<student> &part, int a, int b)
{
    int ans = 0;
    //cout << a << ' ' << b << endl;
    for (auto &elem : part) {
        //cout << elem.s << " " << elem.a << ' ' << elem.b << endl;
        if (elem.a > elem.b) {
            int took_a = min(elem.s, a), took_b = elem.s - took_a;
            ans += took_a * elem.a + took_b * elem.b;
            a -= took_a, b -= took_b;
        }
        else {
            int took_b = min(elem.s, b), took_a = elem.s - took_b;
            ans += took_a * elem.a + took_b * elem.b;
            a -= took_a, b -= took_b;
        }
        //cout << a << " " << b << endl;
    }

    return ans;
}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int n, s, all = 0;
    cin >> n >> s;
    vector<student> part(n);
    for (int i = 0; i < n; ++i) {
        cin >> part[i];
        all += part[i].s;
    }
    sort(rall(part));

    //cout << endl;
    //for (auto elem : part)
    //    cout << elem.s << " " << elem.a << " " << elem.b << endl;

    int need = (all + s - 1) / s;
    int left = 0, right = need;
    while (right - left > 2) {
        int m1 = (2 * left + right) / 3, m2 = (left + 2 * right) / 3;
        //cout << m1 << " " << m2 << endl;
        if (check(part, m1 * s, (need - m1) * s) > check(part, m2 * s, (need - m2) * s))
            right = m2;
        else
            left = m1;
    }

    cout << max(max(check(part, left * s, (need - left) * s), check(part, (left + 1) * s, (need - left - 1) * s)),
                check(part, (left + 2) * s, (need - left - 2) * s)) << endl;

    return 0;
}


#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <cmath>
#include <deque>
#include <algorithm>
#include <random>
#include <ctime>
using namespace std;

#define int int64_t
#define pb push_back
#define pbb pop_back
#define pf pop_front
#define pff push_front
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
typedef long double ld;
typedef long long ll;

const int INF = 1e9, MOD = 1e9 + 7, MAXN = 105;
const ld EPS = 1e-9, PI = acos(-1);

struct student
{
    int s, a, b;

    student()
    {
        s = 0, a = 0, b = 0;
    }
    student(int x, int y, int z)
    {
        s = x, a = y, b = z;
    }

    bool operator <(const student &other) const
    {
        int num1 = max(a, b) * other.s, num2 = max(other.a, other.b) * s;
        return (num1 < num2) || ((num1 == num2) && s < other.s);
    }
};

istream &operator >>(istream &in, student &s)
{
    in >> s.s >> s.a >> s.b;
    return in;
}

int check(const vector<student> &part, int a, int b)
{
    int ans = 0;
    //cout << a << ' ' << b << endl;
    for (auto &elem : part) {
        //cout << elem.s << " " << elem.a << ' ' << elem.b << endl;
        if (elem.a > elem.b) {
            int took_a = min(elem.s, a), took_b = elem.s - took_a;
            ans += took_a * elem.a + took_b * elem.b;
            a -= took_a, b -= took_b;
        }
        else {
            int took_b = min(elem.s, b), took_a = elem.s - took_b;
            ans += took_a * elem.a + took_b * elem.b;
            a -= took_a, b -= took_b;
        }
        //cout << a << " " << b << endl;
    }

    return ans;
}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int n, s, all = 0;
    cin >> n >> s;
    vector<student> part(n);
    for (int i = 0; i < n; ++i) {
        cin >> part[i];
        all += part[i].s;
    }
    sort(rall(part));

    //cout << endl;
    //for (auto elem : part)
    //    cout << elem.s << " " << elem.a << " " << elem.b << endl;

    int need = (all + s - 1) / s;
    int left = 0, right = need;
    while (right - left > 2) {
        int m1 = (2 * left + right) / 3, m2 = (left + 2 * right) / 3;
        //cout << m1 << " " << m2 << endl;
        if (check(part, m1 * s, (need - m1) * s) > check(part, m2 * s, (need - m2) * s))
            right = m2;
        else
            left = m1;
    }

    cout << max(max(check(part, left * s, (need - left) * s), check(part, (left + 1) * s, (need - left - 1) * s)),
                check(part, (left + 1) * s, (need - left - 2) * s)) << endl;

    return 0;
}


#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <cmath>
#include <deque>
#include <algorithm>
#include <random>
#include <ctime>
using namespace std;

#define int int64_t
#define pb push_back
#define pbb pop_back
#define pf pop_front
#define pff push_front
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
typedef long double ld;
typedef long long ll;

const int INF = 1e9, MOD = 1e9 + 7, MAXN = 105;
const ld EPS = 1e-9, PI = acos(-1);

struct student
{
    int s, a, b;

    student()
    {
        s = 0, a = 0, b = 0;
    }
    student(int x, int y, int z)
    {
        s = x, a = y, b = z;
    }

    bool operator <(const student &other) const
    {
        int num1 = max(a, b) * s, num2 = max(other.a, other.b) * other.s;
        return num1 < num2 || (num1 == num2 && s < other.s);
    }
};

istream &operator >>(istream &in, student &s)
{
    in >> s.s >> s.a >> s.b;
    return in;
}

int check(const vector<student> &part, int a, int b)
{
    int ans = 0;
    //cout << a << ' ' << b << endl;
    for (auto &elem : part) {
        //cout << elem.s << " " << elem.a << ' ' << elem.b << endl;
        if (elem.a > elem.b) {
            int took_a = min(elem.s, a), took_b = elem.s - took_a;
            ans += took_a * elem.a + took_b * elem.b;
            a -= took_a, b -= took_b;
        }
        else {
            int took_b = min(elem.s, b), took_a = elem.s - took_b;
            ans += took_a * elem.a + took_b * elem.b;
            a -= took_a, b -= took_b;
        }
        //cout << a << " " << b << endl;
    }

    return ans;
}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int n, s, all = 0;
    cin >> n >> s;
    vector<student> part(n);
    for (int i = 0; i < n; ++i) {
        cin >> part[i];
        all += part[i].s;
    }

    sort(rall(part));
    int need = (all + s - 1) / s;
    int left = 0, right = need + 1;
    while (right - left > 2) {
        int m1 = (2 * left + right) / 3, m2 = (left + 2 * right) / 3;
        //cout << m1 << " " << m2 << endl;
        if (check(part, m1 * s, (need - m1) * s) > check(part, m2 * s, (need - m2) * s))
            right = m2;
        else
            left = m1;
    }

    cout << max(check(part, left * s, (need - left) * s), check(part, (left + 1) * s, (need - left - 1) * s)) << endl;

    return 0;
}


#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <cmath>
#include <deque>
#include <algorithm>
#include <random>
#include <ctime>
using namespace std;

#define int int64_t
#define pb push_back
#define pbb pop_back
#define pf pop_front
#define pff push_front
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
typedef long double ld;
typedef long long ll;

const int INF = 1e9, MOD = 1e9 + 7, MAXN = 105;
const ld EPS = 1e-9, PI = acos(-1);

struct student
{
    int s, a, b;

    student()
    {
        s = 0, a = 0, b = 0;
    }
    student(int x, int y, int z)
    {
        s = x, a = y, b = z;
    }

    bool operator <(const student &other) const
    {
        return (a == b && other.a != other.b) || max(a, b) < max(other.a, other.b);
    }
};

istream &operator >>(istream &in, student &s)
{
    in >> s.s >> s.a >> s.b;
    return in;
}

int check(const vector<student> &part, int a, int b)
{
    int ans = 0;
    //cout << a << ' ' << b << endl;
    for (auto &elem : part) {
        //cout << elem.s << " " << elem.a << ' ' << elem.b << endl;
        if (elem.a > elem.b) {
            int took_a = min(elem.s, a), took_b = elem.s - took_a;
            ans += took_a * elem.a + took_b * elem.b;
            a -= took_a, b -= took_b;
        }
        else {
            int took_b = min(elem.s, b), took_a = elem.s - took_b;
            ans += took_a * elem.a + took_b * elem.b;
            a -= took_a, b -= took_b;
        }
        //cout << a << " " << b << endl;
    }

    return ans;
}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int n, s, all = 0;
    cin >> n >> s;
    vector<student> part(n);
    for (int i = 0; i < n; ++i) {
        cin >> part[i];
        all += part[i].s;
    }

    sort(rall(part));
    int need = (all + s - 1) / s;
    int left = 0, right = need + 1;
    while (right - left > 2) {
        int m1 = (2 * left + right) / 3, m2 = (left + 2 * right) / 3;
        //cout << m1 << " " << m2 << endl;
        if (check(part, m1 * s, (need - m1) * s) > check(part, m2 * s, (need - m2) * s))
            right = m2;
        else
            left = m1;
    }

    cout << max(check(part, left * s, (need - left) * s), check(part, (left + 1) * s, (need - left - 1) * s)) << endl;

    return 0;
}


#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <cmath>
#include <deque>
#include <algorithm>
#include <random>
#include <ctime>
using namespace std;

#define int int64_t
#define pb push_back
#define pbb pop_back
#define pf pop_front
#define pff push_front
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
typedef long double ld;
typedef long long ll;

const int INF = 1e9, MOD = 1e9 + 7, MAXN = 105;
const ld EPS = 1e-9, PI = acos(-1);

struct student
{
    int s, a, b;

    student()
    {
        s = 0, a = 0, b = 0;
    }
    student(int x, int y, int z)
    {
        s = x, a = y, b = z;
    }

    bool operator <(const student &other) const
    {
        int num1 = max(a, b) * other.s, num2 = max(other.a, other.b) * s;
        return (num1 < num2) || ((num1 == num2) && s < other.s);
    }
};

istream &operator >>(istream &in, student &s)
{
    in >> s.s >> s.a >> s.b;
    return in;
}

int check(const vector<student> &part, int a, int b)
{
    int ans = 0;
    //cout << a << ' ' << b << endl;
    for (auto &elem : part) {
        //cout << elem.s << " " << elem.a << ' ' << elem.b << endl;
        if (elem.a > elem.b) {
            int took_a = min(elem.s, a), took_b = elem.s - took_a;
            ans += took_a * elem.a + took_b * elem.b;
            a -= took_a, b -= took_b;
        }
        else {
            int took_b = min(elem.s, b), took_a = elem.s - took_b;
            ans += took_a * elem.a + took_b * elem.b;
            a -= took_a, b -= took_b;
        }
        //cout << a << " " << b << endl;
    }

    return ans;
}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int n, s, all = 0;
    cin >> n >> s;
    vector<student> part(n);
    for (int i = 0; i < n; ++i) {
        cin >> part[i];
        all += part[i].s;
    }
    sort(rall(part));

    //cout << endl;
    //for (auto elem : part)
    //    cout << elem.s << " " << elem.a << " " << elem.b << endl;

    int need = (all + s - 1) / s;
    int left = 0, right = need + 1;
    while (right - left > 2) {
        int m1 = (2 * left + right) / 3, m2 = (left + 2 * right) / 3;
        //cout << m1 << " " << m2 << endl;
        if (check(part, m1 * s, (need - m1) * s) > check(part, m2 * s, (need - m2) * s))
            right = m2;
        else
            left = m1;
    }

    cout << max(check(part, left * s, (need - left) * s), check(part, (left + 1) * s, (need - left - 1) * s)) << endl;

    return 0;
}


#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <cmath>
#include <deque>
#include <algorithm>
#include <random>
#include <ctime>
using namespace std;

#define int int64_t
#define pb push_back
#define pbb pop_back
#define pf pop_front
#define pff push_front
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
typedef long double ld;
typedef long long ll;

const int INF = 1e9, MOD = 1e9 + 7, MAXN = 105;
const ld EPS = 1e-9, PI = acos(-1);

struct student
{
    int s, a, b;

    student()
    {
        s = 0, a = 0, b = 0;
    }
    student(int x, int y, int z)
    {
        s = x, a = y, b = z;
    }

    bool operator <(const student &other) const
    {
        return max(a, b) * s < max(other.a, other.b) * other.s;
    }
};

istream &operator >>(istream &in, student &s)
{
    in >> s.s >> s.a >> s.b;
    return in;
}

int check(const vector<student> &part, int a, int b)
{
    int ans = 0;
    //cout << a << ' ' << b << endl;
    for (auto &elem : part) {
        //cout << elem.s << " " << elem.a << ' ' << elem.b << endl;
        if (elem.a > elem.b) {
            int took_a = min(elem.s, a), took_b = elem.s - took_a;
            ans += took_a * elem.a + took_b * elem.b;
            a -= took_a, b -= took_b;
        }
        else {
            int took_b = min(elem.s, b), took_a = elem.s - took_b;
            ans += took_a * elem.a + took_b * elem.b;
            a -= took_a, b -= took_b;
        }
        //cout << a << " " << b << endl;
    }

    return ans;
}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int n, s, all = 0;
    cin >> n >> s;
    vector<student> part(n);
    for (int i = 0; i < n; ++i) {
        cin >> part[i];
        all += part[i].s;
    }

    sort(rall(part));
    int need = (all + s - 1) / s;
    int left = 0, right = need + 1;
    while (right - left > 2) {
        int m1 = (2 * left + right) / 3, m2 = (left + 2 * right) / 3;
        //cout << m1 << " " << m2 << endl;
        if (check(part, m1 * s, (need - m1) * s) > check(part, m2 * s, (need - m2) * s))
            right = m2;
        else
            left = m1;
    }

    cout << max(check(part, left * s, (need - left) * s), check(part, (left + 1) * s, (need - left - 1) * s)) << endl;

    return 0;
}


#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <cmath>
#include <deque>
#include <algorithm>
#include <random>
#include <ctime>
using namespace std;

#define int int64_t
#define pb push_back
#define pbb pop_back
#define pf pop_front
#define pff push_front
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
typedef long double ld;
typedef long long ll;

const int INF = 1e9, MOD = 1e9 + 7, MAXN = 105;
const ld EPS = 1e-9, PI = acos(-1);

struct student
{
    int s, a, b;

    student()
    {
        s = 0, a = 0, b = 0;
    }
    student(int x, int y, int z)
    {
        s = x, a = y, b = z;
    }

    bool operator <(const student &other) const
    {
        return (a == b && other.a != other.b) || max(a, b) * s < max(other.a, other.b) * other.s;
    }
};

istream &operator >>(istream &in, student &s)
{
    in >> s.s >> s.a >> s.b;
    return in;
}

int check(const vector<student> &part, int a, int b)
{
    int ans = 0;
    //cout << a << ' ' << b << endl;
    for (auto &elem : part) {
        //cout << elem.s << " " << elem.a << ' ' << elem.b << endl;
        if (elem.a > elem.b) {
            int took_a = min(elem.s, a), took_b = elem.s - took_a;
            ans += took_a * elem.a + took_b * elem.b;
            a -= took_a, b -= took_b;
        }
        else {
            int took_b = min(elem.s, b), took_a = elem.s - took_b;
            ans += took_a * elem.a + took_b * elem.b;
            a -= took_a, b -= took_b;
        }
        //cout << a << " " << b << endl;
    }

    return ans;
}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int n, s, all = 0;
    cin >> n >> s;
    vector<student> part(n);
    for (int i = 0; i < n; ++i) {
        cin >> part[i];
        all += part[i].s;
    }

    sort(rall(part));
    int need = (all + s - 1) / s;
    int left = 0, right = need + 1;
    while (right - left > 2) {
        int m1 = (2 * left + right) / 3, m2 = (left + 2 * right) / 3;
        //cout << m1 << " " << m2 << endl;
        if (check(part, m1 * s, (need - m1) * s) > check(part, m2 * s, (need - m2) * s))
            right = m2;
        else
            left = m1;
    }

    cout << max(check(part, left * s, (need - left) * s), check(part, (left + 1) * s, (need - left - 1) * s)) << endl;

    return 0;
}


#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <cmath>
#include <deque>
#include <algorithm>
#include <random>
#include <ctime>
using namespace std;

#define int int64_t
#define pb push_back
#define pbb pop_back
#define pf pop_front
#define pff push_front
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
typedef long double ld;
typedef long long ll;

const int INF = 1e9, MOD = 1e9 + 7, MAXN = 105;
const ld EPS = 1e-9, PI = acos(-1);

struct student
{
    int s, a, b;

    student()
    {
        s = 0, a = 0, b = 0;
    }
    student(int x, int y, int z)
    {
        s = x, a = y, b = z;
    }

    bool operator <(const student &other) const
    {
        return abs(a - b) < abs(other.a - other.b);
    }
};

istream &operator >>(istream &in, student &s)
{
    in >> s.s >> s.a >> s.b;
    return in;
}

int check(const vector<student> &part, int a, int b)
{
    int ans = 0;
    //cout << a << ' ' << b << endl;
    for (auto &elem : part) {
        //cout << elem.s << " " << elem.a << ' ' << elem.b << endl;
        if (elem.a > elem.b) {
            int took_a = min(elem.s, a), took_b = elem.s - took_a;
            ans += took_a * elem.a + took_b * elem.b;
            a -= took_a, b -= took_b;
        }
        else {
            int took_b = min(elem.s, b), took_a = elem.s - took_b;
            ans += took_a * elem.a + took_b * elem.b;
            a -= took_a, b -= took_b;
        }
        //cout << a << " " << b << endl;
    }

    return ans;
}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int n, s, all = 0;
    cin >> n >> s;
    vector<student> part(n);
    for (int i = 0; i < n; ++i) {
        cin >> part[i];
        all += part[i].s;
    }
    sort(all(part));

    //cout << endl;
    //for (auto elem : part)
    //    cout << elem.s << " " << elem.a << " " << elem.b << endl;

    int need = (all + s - 1) / s;
    int left = 0, right = need;
    while (right - left > 2) {
        int m1 = (2 * left + right) / 3, m2 = (left + 2 * right) / 3;
        //cout << m1 << " " << m2 << endl;
        if (check(part, m1 * s, (need - m1) * s) > check(part, m2 * s, (need - m2) * s))
            right = m2;
        else
            left = m1;
    }

    cout << max(max(check(part, left * s, (need - left) * s), check(part, (left + 1) * s, (need - left - 1) * s)),
                check(part, (left + 1) * s, (need - left - 2) * s)) << endl;

    return 0;
}


#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <cmath>
#include <deque>
#include <algorithm>
#include <random>
#include <ctime>
using namespace std;

#define int int64_t
#define pb push_back
#define pbb pop_back
#define pf pop_front
#define pff push_front
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
typedef long double ld;
typedef long long ll;

const int INF = 1e9, MOD = 1e9 + 7, MAXN = 105;
const ld EPS = 1e-9, PI = acos(-1);

struct student
{
    int s, a, b;

    student()
    {
        s = 0, a = 0, b = 0;
    }
    student(int x, int y, int z)
    {
        s = x, a = y, b = z;
    }

    bool operator <(const student &other) const
    {
        return abs(a - b) < abs(other.a - other.b);
    }
};

istream &operator >>(istream &in, student &s)
{
    in >> s.s >> s.a >> s.b;
    return in;
}

int check(const vector<student> &part, int a, int b)
{
    int ans = 0;
    //cout << a << ' ' << b << endl;
    for (auto &elem : part) {
        //cout << elem.s << " " << elem.a << ' ' << elem.b << endl;
        if (elem.a > elem.b) {
            int took_a = min(elem.s, a), took_b = elem.s - took_a;
            ans += took_a * elem.a + took_b * elem.b;
            a -= took_a, b -= took_b;
        }
        else {
            int took_b = min(elem.s, b), took_a = elem.s - took_b;
            ans += took_a * elem.a + took_b * elem.b;
            a -= took_a, b -= took_b;
        }
        //cout << a << " " << b << endl;
    }

    return ans;
}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int n, s, all = 0;
    cin >> n >> s;
    vector<student> part(n);
    for (int i = 0; i < n; ++i) {
        cin >> part[i];
        all += part[i].s;
    }
    sort(rall(part));

    //cout << endl;
    //for (auto elem : part)
    //    cout << elem.s << " " << elem.a << " " << elem.b << endl;

    int need = (all + s - 1) / s;
    int left = 0, right = need;
    while (right - left > 2) {
        int m1 = (2 * left + right) / 3, m2 = (left + 2 * right) / 3;
        //cout << m1 << " " << m2 << endl;
        if (check(part, m1 * s, (need - m1) * s) > check(part, m2 * s, (need - m2) * s))
            right = m2;
        else
            left = m1;
    }

    cout << max(max(check(part, left * s, (need - left) * s), check(part, (left + 1) * s, (need - left - 1) * s)),
                check(part, (left + 1) * s, (need - left - 2) * s)) << endl;

    return 0;
}


#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <cmath>
#include <deque>
#include <algorithm>
#include <random>
#include <ctime>
using namespace std;

#define int int64_t
#define pb push_back
#define pbb pop_back
#define pf pop_front
#define pff push_front
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
typedef long double ld;
typedef long long ll;

const int INF = 1e9, MOD = 1e9 + 7, MAXN = 105;
const ld EPS = 1e-9, PI = acos(-1);

struct student
{
    int s, a, b;

    student()
    {
        s = 0, a = 0, b = 0;
    }
    student(int x, int y, int z)
    {
        s = x, a = y, b = z;
    }

    bool operator <(const student &other) const
    {
        int num1 = max(a, b) * other.s, num2 = max(other.a, other.b) * s;
        return (num1 < num2) || ((num1 == num2) && s < other.s);
    }
};

istream &operator >>(istream &in, student &s)
{
    in >> s.s >> s.a >> s.b;
    return in;
}

int check(const vector<student> &part, int a, int b)
{
    int ans = 0;
    //cout << a << ' ' << b << endl;
    for (auto &elem : part) {
        //cout << elem.s << " " << elem.a << ' ' << elem.b << endl;
        if (elem.a > elem.b) {
            int took_a = min(elem.s, a), took_b = elem.s - took_a;
            ans += took_a * elem.a + took_b * elem.b;
            a -= took_a, b -= took_b;
        }
        else {
            int took_b = min(elem.s, b), took_a = elem.s - took_b;
            ans += took_a * elem.a + took_b * elem.b;
            a -= took_a, b -= took_b;
        }
        //cout << a << " " << b << endl;
    }

    return ans;
}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int n, s, all = 0;
    cin >> n >> s;
    vector<student> part(n);
    for (int i = 0; i < n; ++i) {
        cin >> part[i];
        all += part[i].s;
    }
    sort(rall(part));

    //cout << endl;
    //for (auto elem : part)
    //    cout << elem.s << " " << elem.a << " " << elem.b << endl;

    int need = (all + s - 1) / s;
    int left = 0, right = need + 1;
    while (right - left > 2) {
        int m1 = (2 * left + right) / 3, m2 = (left + 2 * right) / 3;
        cout << m1 << " " << m2 << endl;
        if (check(part, m1 * s, (need - m1) * s) > check(part, m2 * s, (need - m2) * s))
            right = m2;
        else
            left = m1;
    }

    int ans = max(check(part, left * s, (need - left) * s), check(part, (left + 1) * s, (need - left - 1) * s));
    ans = max(check(part, need * s, 0), ans);
    cout << ans << endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N,S;
ll V[101010],A[101010],B[101010],V2[101010];
ll sum[3];
vector<pair<int,int>> T[3];

void solve() {
	int i,j,k,l,r,x,y; string s;

	cin>>N>>S;
	ll ret=0;
	FOR(i,N) {
		cin>>V[i]>>A[i]>>B[i];
		if(A[i]>B[i]) T[0].push_back({A[i]-B[i],i}), sum[0]+=V[i];
		if(A[i]<B[i]) T[1].push_back({B[i]-A[i],i}), sum[1]+=V[i];
		if(A[i]==B[i]) sum[2]+=V[i], ret+=V[i]*A[i];
	}

	FOR(i,2) {
		sort(ALL(T[i]));
		ll left=sum[i]/S*S;
		sum[i]-=left;
		while(left>0) {
			x=T[i].back().second;
			ll del=min(left,V[x]);
			if(i<1) ret+=del*A[x];
			else ret+=del*B[x];

			V[x]-=del;
			left-=del;
			if(V[x]==0) T[i].pop_back();
		}
	}
	if(sum[2]>=2*S) sum[2]=2*S+sum[2]%S;

	sum[2]%=2*S;
	int cnt=(sum[0]+sum[1]+sum[2]+S-1)/S;
	ll ma=0;
	memmove(V2,V,sizeof(V));
	for(int a=0;a<=cnt;a++) {
		memmove(V,V2,sizeof(V));
		ll NA[2]={a*S,(cnt-a)*S};
		ll tmp=0;
		vector<pair<int,int>> T2[2];
		FOR(i,2) {
			T2[i]=T[i];
			while(T2[i].size() && NA[i]) {
				x=T2[i].back().second;
				ll del=min(NA[i],V[x]);
				if(i<1) tmp+=del*A[x];
				else tmp+=del*B[x];

				V[x]-=del;
				NA[i]-=del;
				if(V[x]==0) T2[i].pop_back();
			}
			FORR(t,T2[i]) {
				if(i<1) tmp+=V[t.second]*B[t.second];
				else tmp+=V[t.second]*A[t.second];
			}
		}
		ma=max(ma,tmp);
	}

	cout<<ret+ma<<endl;

}

int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N,S;
ll V[101010],A[101010],B[101010],V2[101010];
ll sum[3];
vector<pair<int,int>> T[3];

void solve() {
	int i,j,k,l,r,x,y; string s;

	cin>>N>>S;
	ll ret=0;
	FOR(i,N) {
		cin>>V[i]>>A[i]>>B[i];
		if(A[i]>=B[i]) T[0].push_back({A[i]-B[i],i}), sum[0]+=V[i];
		if(A[i]<B[i]) T[1].push_back({B[i]-A[i],i}), sum[1]+=V[i];
	}

	FOR(i,2) {
		sort(ALL(T[i]));
		ll left=sum[i]/S*S;
		sum[i]-=left;
		while(left>0) {
			x=T[i].back().second;
			ll del=min(left,V[x]);
			if(i<1) ret+=del*A[x];
			else ret+=del*B[x];

			V[x]-=del;
			left-=del;
			if(V[x]==0) T[i].pop_back();
		}
	}

	int cnt=(sum[0]+sum[1]+S-1)/S;
	ll ma=0;
	memmove(V2,V,sizeof(V));
	for(int a=0;a<=cnt;a++) {
		memmove(V,V2,sizeof(V));
		ll NA[2]={a*S,(cnt-a)*S};
		ll tmp=0;
		vector<pair<int,int>> T2[2];
		FOR(i,2) {
			T2[i]=T[i];
			while(T2[i].size() && NA[i]) {
				x=T2[i].back().second;
				ll del=min(NA[i],V[x]);
				if(i<1) tmp+=del*A[x];
				else tmp+=del*B[x];

				V[x]-=del;
				NA[i]-=del;
				if(V[x]==0) T2[i].pop_back();
			}
			FORR(t,T2[i]) {
				if(i<1) tmp+=V[t.second]*B[t.second];
				else tmp+=V[t.second]*A[t.second];
			}
		}
		ma=max(ma,tmp);
	}

	cout<<ret+ma<<endl;

}

int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N,S;
ll V[101010],A[101010],B[101010],V2[101010];
ll sum[3];
vector<pair<int,int>> T[3];

void solve() {
	int i,j,k,l,r,x,y; string s;

	cin>>N>>S;
	ll ret=0;
	FOR(i,N) {
		cin>>V[i]>>A[i]>>B[i];
		if(A[i]>B[i]) T[0].push_back({A[i]-B[i],i}), sum[0]+=V[i];
		if(A[i]<B[i]) T[1].push_back({B[i]-A[i],i}), sum[1]+=V[i];
		if(A[i]==B[i]) sum[2]+=V[i], ret+=V[i]*A[i];
	}

	FOR(i,2) {
		sort(ALL(T[i]));
		ll left=sum[i]/S*S;
		sum[i]-=left;
		while(left>0) {
			x=T[i].back().second;
			ll del=min(left,V[x]);
			if(i<1) ret+=del*A[x];
			else ret+=del*B[x];

			V[x]-=del;
			left-=del;
			if(V[x]==0) T[i].pop_back();
		}
	}

	int cnt=(sum[0]+sum[1]+sum[2]+S-1)/S;
	ll ma=0;
	memmove(V2,V,sizeof(V));
	for(int a=0;a<=cnt;a++) {
		memmove(V,V2,sizeof(V));
		ll NA[2]={a*S,(cnt-a)*S};
		ll tmp=0;
		vector<pair<int,int>> T2[2];
		FOR(i,2) {
			T2[i]=T[i];
			while(T2[i].size() && NA[i]) {
				x=T2[i].back().second;
				ll del=min(NA[i],V[x]);
				if(i<1) tmp+=del*A[x];
				else tmp+=del*B[x];

				V[x]-=del;
				NA[i]-=del;
				if(V[x]==0) T2[i].pop_back();
			}
			FORR(t,T2[i]) {
				if(i<1) tmp+=V[t.second]*B[t.second];
				else tmp+=V[t.second]*A[t.second];
			}
		}
		ma=max(ma,tmp);
	}

	cout<<ret+ma<<endl;

}

int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N,S;
ll V[101010],A[101010],B[101010],V2[101010];
ll sum[3];
vector<pair<int,int>> T[3];

void solve() {
	int i,j,k,l,r,x,y; string s;

	cin>>N>>S;
	ll ret=0;
	FOR(i,N) {
		cin>>V[i]>>A[i]>>B[i];
		if(A[i]>B[i]) T[0].push_back({A[i]-B[i],i}), sum[0]+=V[i];
		if(A[i]<B[i]) T[1].push_back({B[i]-A[i],i}), sum[1]+=V[i];
		if(A[i]==B[i]) sum[2]+=V[i], ret+=V[i]*A[i];
	}

	FOR(i,2) {
		sort(ALL(T[i]));
		ll left=sum[i]/S*S;
		sum[i]-=left;
		while(left>0) {
			x=T[i].back().second;
			ll del=min(left,V[x]);
			if(i<1) ret+=del*A[x];
			else ret+=del*B[x];

			V[x]-=del;
			left-=del;
			if(V[x]==0) T[i].pop_back();
		}
	}
	sum[2]%=S;
	int cnt=(sum[0]+sum[1]+sum[2]+S-1)/S;
	ll ma=0;
	memmove(V2,V,sizeof(V));
	for(int a=0;a<=cnt;a++) {
		memmove(V,V2,sizeof(V));
		ll NA[2]={a*S,(cnt-a)*S};
		ll tmp=0;
		vector<pair<int,int>> T2[2];
		FOR(i,2) {
			T2[i]=T[i];
			while(T2[i].size() && NA[i]) {
				x=T2[i].back().second;
				ll del=min(NA[i],V[x]);
				if(i<1) tmp+=del*A[x];
				else tmp+=del*B[x];

				V[x]-=del;
				NA[i]-=del;
				if(V[x]==0) T2[i].pop_back();
			}
			FORR(t,T2[i]) {
				if(i<1) tmp+=V[t.second]*B[t.second];
				else tmp+=V[t.second]*A[t.second];
			}
		}
		ma=max(ma,tmp);
	}

	cout<<ret+ma<<endl;

}

int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N,S;
ll V[101010],A[101010],B[101010],V2[101010];
ll sum[3];
vector<pair<int,int>> T[3];

void solve() {
	int i,j,k,l,r,x,y; string s;

	cin>>N>>S;
	ll ret=0;
	FOR(i,N) {
		cin>>V[i]>>A[i]>>B[i];
		if(A[i]>B[i]) T[0].push_back({A[i]-B[i],i}), sum[0]+=V[i];
		if(A[i]<B[i]) T[1].push_back({B[i]-A[i],i}), sum[1]+=V[i];
		if(A[i]==B[i]) sum[2]+=V[i], ret+=V[i]*A[i];
	}

	FOR(i,2) {
		sort(ALL(T[i]));
		ll left=sum[i]/S*S;
		sum[i]-=left;
		while(left>0) {
			x=T[i].back().second;
			ll del=min(left,V[x]);
			if(i<1) ret+=del*A[x];
			else ret+=del*B[x];

			V[x]-=del;
			left-=del;
			if(V[x]==0) T[i].pop_back();
		}
	}
	if(sum[2]>=2*S) sum[2]=2*S+sum[2]%S;

	int cnt=(sum[0]+sum[1]+sum[2]+S-1)/S;
	ll ma=0;
	memmove(V2,V,sizeof(V));
	for(int a=0;a<=cnt;a++) {
		memmove(V,V2,sizeof(V));
		ll NA[2]={a*S,(cnt-a)*S};
		ll tmp=0;
		vector<pair<int,int>> T2[2];
		FOR(i,2) {
			T2[i]=T[i];
			while(T2[i].size() && NA[i]) {
				x=T2[i].back().second;
				ll del=min(NA[i],V[x]);
				if(i<1) tmp+=del*A[x];
				else tmp+=del*B[x];

				V[x]-=del;
				NA[i]-=del;
				if(V[x]==0) T2[i].pop_back();
			}
			FORR(t,T2[i]) {
				if(i<1) tmp+=V[t.second]*B[t.second];
				else tmp+=V[t.second]*A[t.second];
			}
		}
		ma=max(ma,tmp);
	}

	cout<<ret+ma<<endl;

}

int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}


#include <bits/stdc++.h>

#define FOR(i,b,e) for(int i=(b); i <= (e); ++i)
#define FORD(i,b,e) for(int i=(b); i >= (e); --i)
#define SIZE(c) (int) (c).size()
#define FORE(i,c) FOR(i,0,SIZE(c)-1)
#define FORDE(i,c) FORD(i,SIZE(c)-1,0)

#define pb push_back
#define mp make_pair
#define st first
#define nd second

using namespace std;

typedef long long ll;
typedef pair <int,int> pii;
typedef pair <ll,ll> pll;

typedef vector <int> VI;
typedef vector <bool> VB;
typedef vector <pii> VP;
typedef vector <ll> VL;
typedef vector <pll> VPL;

typedef vector <VI> VVI;
typedef vector <VL> VVL;
typedef vector <VB> VVB;
typedef vector <VP> VVP;

const int MOD = 1000000007;
const int INF = 1000000001;
const ll LINF = 1000000000000000001LL;

/*************************************************************************/

int main() {
    ios_base::sync_with_stdio(0);

    int n, s;
    cin >> n >> s;

    struct person { ll s, a, b; };

    vector <person> inp(n);

    FOR(i,0,n-1) {
        cin >> inp[i].s >> inp[i].a >> inp[i].b;
    }

    ll totalSlices = 0;
    FOR(i,0,n-1) totalSlices += inp[i].s;

    ll pizzasNeeded = (totalSlices + s - 1) / s;

    sort(inp.begin(), inp.end(), [](const person &a, const person &b) {
        return a.a - a.b > b.a - b.b;
    });

    int lastPositive = -1;
    while (lastPositive < n-1 && inp[lastPositive + 1].a > inp[lastPositive + 1].b) {
        lastPositive++;
    }

    ll sumPref = 0;
    FOR(i,0,lastPositive) sumPref += inp[i].s;

    ll pizzasNeededType1 = sumPref / s;
    ll ans = 0;

    FOR(type1,pizzasNeededType1,pizzasNeededType1+1) {
        if (type1 > pizzasNeeded) continue;

        ll here = 0;

        ll type1Left = type1 * s;

        FOR(i,0,n-1) {
            ll getType1 = min(type1Left, inp[i].s);
            ll getType2 = inp[i].s - getType1;

            here += inp[i].a * getType1 + inp[i].b * getType2;

            type1Left -= getType1;
        }

        ans = max(ans, here);
    }

    cout << ans;

    return 0;
}

/*************************************************************************/


#include <bits/stdc++.h>

#define FOR(i,b,e) for(int i=(b); i <= (e); ++i)
#define FORD(i,b,e) for(int i=(b); i >= (e); --i)
#define SIZE(c) (int) (c).size()
#define FORE(i,c) FOR(i,0,SIZE(c)-1)
#define FORDE(i,c) FORD(i,SIZE(c)-1,0)

#define pb push_back
#define mp make_pair
#define st first
#define nd second

using namespace std;

typedef long long ll;
typedef pair <int,int> pii;
typedef pair <ll,ll> pll;

typedef vector <int> VI;
typedef vector <bool> VB;
typedef vector <pii> VP;
typedef vector <ll> VL;
typedef vector <pll> VPL;

typedef vector <VI> VVI;
typedef vector <VL> VVL;
typedef vector <VB> VVB;
typedef vector <VP> VVP;

const int MOD = 1000000007;
const int INF = 1000000001;
const ll LINF = 1000000000000000001LL;

/*************************************************************************/

int main() {
    ios_base::sync_with_stdio(0);

    int n; ll s;
    cin >> n >> s;

    struct person { ll s, a, b; };

    vector <person> inp(n);

    FOR(i,0,n-1) {
        cin >> inp[i].s >> inp[i].a >> inp[i].b;
    }

    ll totalSlices = 0;
    FOR(i,0,n-1) totalSlices += inp[i].s;

    ll pizzasNeeded = (totalSlices + s - 1) / s;

    sort(inp.begin(), inp.end(), [](const person &a, const person &b) {
        return a.a - a.b > b.a - b.b;
    });

    int lastPositive = -1;
    while (lastPositive < n-1 && inp[lastPositive + 1].a > inp[lastPositive + 1].b) {
        lastPositive++;
    }

    ll sumPref = 0;
    FOR(i,0,lastPositive) sumPref += inp[i].s;

    ll pizzasNeededType1 = sumPref / s;
    ll ans = 0;

    sort(inp.begin(), inp.end(), [](const person &a, const person &b) {
        return abs(a.a - a.b) > abs(b.a - b.b);
    });

    FOR(type1,pizzasNeededType1,pizzasNeededType1+1) {
        if (type1 > pizzasNeeded) continue;
        int type2 = pizzasNeeded - type1;

        ll here = 0;

        ll type1Left = type1 * s;
        ll type2Left = type2 * s;

        FOR(i,0,n-1) {
            ll getType1, getType2;

            if (inp[i].a > inp[i].b) {
                getType1 = min(type1Left, inp[i].s);
                getType2 = inp[i].s - getType1;
            } else {
                getType2 = min(type2Left, inp[i].s);
                getType1 = inp[i].s - getType2;
            }

            type1Left -= getType1;
            type2Left -= getType2;

            here += getType1 * inp[i].a + getType2 * inp[i].b;
        }

        ans = max(ans, here);
    }

    cout << ans;

    return 0;
}

/*************************************************************************/


#include <bits/stdc++.h>

#define FOR(i,b,e) for(int i=(b); i <= (e); ++i)
#define FORD(i,b,e) for(int i=(b); i >= (e); --i)
#define SIZE(c) (int) (c).size()
#define FORE(i,c) FOR(i,0,SIZE(c)-1)
#define FORDE(i,c) FORD(i,SIZE(c)-1,0)

#define pb push_back
#define mp make_pair
#define st first
#define nd second

using namespace std;

typedef long long ll;
typedef pair <int,int> pii;
typedef pair <ll,ll> pll;

typedef vector <int> VI;
typedef vector <bool> VB;
typedef vector <pii> VP;
typedef vector <ll> VL;
typedef vector <pll> VPL;

typedef vector <VI> VVI;
typedef vector <VL> VVL;
typedef vector <VB> VVB;
typedef vector <VP> VVP;

const int MOD = 1000000007;
const int INF = 1000000001;
const ll LINF = 1000000000000000001LL;

/*************************************************************************/

int main() {
    ios_base::sync_with_stdio(0);

    int n; ll s;
    cin >> n >> s;

    struct person { ll s, a, b; };

    vector <person> inp(n);

    FOR(i,0,n-1) {
        cin >> inp[i].s >> inp[i].a >> inp[i].b;
    }

    ll totalSlices = 0;
    FOR(i,0,n-1) totalSlices += inp[i].s;

    ll pizzasNeeded = (totalSlices + s - 1) / s;

    sort(inp.begin(), inp.end(), [](const person &a, const person &b) {
        return a.a - a.b > b.a - b.b;
    });

    int lastPositive = -1;
    while (lastPositive < n-1 && inp[lastPositive + 1].a > inp[lastPositive + 1].b) {
        lastPositive++;
    }

    ll sumPref = 0;
    FOR(i,0,lastPositive) sumPref += inp[i].s;

    ll pizzasNeededType1 = sumPref / s;
    ll ans = 0;

    sort(inp.begin(), inp.end(), [](const person &a, const person &b) {
        return abs(a.a - a.b) > abs(b.a - b.b);
    });

    for (ll type1 = pizzasNeededType1; type1 <= pizzasNeededType1+1; type1++) {
        if (type1 > pizzasNeeded) continue;
        int type2 = pizzasNeeded - type1;

        ll here = 0;

        ll type1Left = type1 * s;
        ll type2Left = type2 * s;

        FOR(i,0,n-1) {
            ll getType1, getType2;

            if (inp[i].a > inp[i].b) {
                getType1 = min(type1Left, inp[i].s);
                getType2 = inp[i].s - getType1;
            } else {
                getType2 = min(type2Left, inp[i].s);
                getType1 = inp[i].s - getType2;
            }

            type1Left -= getType1;
            type2Left -= getType2;

            here += getType1 * inp[i].a + getType2 * inp[i].b;
        }

        ans = max(ans, here);
    }

    cout << ans;

    return 0;
}

/*************************************************************************/


#include <bits/stdc++.h>

#define FOR(i,b,e) for(int i=(b); i <= (e); ++i)
#define FORD(i,b,e) for(int i=(b); i >= (e); --i)
#define SIZE(c) (int) (c).size()
#define FORE(i,c) FOR(i,0,SIZE(c)-1)
#define FORDE(i,c) FORD(i,SIZE(c)-1,0)

#define pb push_back
#define mp make_pair
#define st first
#define nd second

using namespace std;

typedef long long ll;
typedef pair <int,int> pii;
typedef pair <ll,ll> pll;

typedef vector <int> VI;
typedef vector <bool> VB;
typedef vector <pii> VP;
typedef vector <ll> VL;
typedef vector <pll> VPL;

typedef vector <VI> VVI;
typedef vector <VL> VVL;
typedef vector <VB> VVB;
typedef vector <VP> VVP;

const int MOD = 1000000007;
const int INF = 1000000001;
const ll LINF = 1000000000000000001LL;

/*************************************************************************/

int main() {
    ios_base::sync_with_stdio(0);

    int n; ll s;
    cin >> n >> s;

    struct person { ll s, a, b; };

    vector <person> inp(n);

    FOR(i,0,n-1) {
        cin >> inp[i].s >> inp[i].a >> inp[i].b;
    }

    ll totalSlices = 0;
    FOR(i,0,n-1) totalSlices += inp[i].s;

    ll pizzasNeeded = (totalSlices + s - 1) / s;

    sort(inp.begin(), inp.end(), [](const person &a, const person &b) {
        return a.a - a.b > b.a - b.b;
    });

    int lastPositive = -1;
    while (lastPositive < n-1 && inp[lastPositive + 1].a > inp[lastPositive + 1].b) {
        lastPositive++;
    }

    ll sumPref = 0;
    FOR(i,0,lastPositive) sumPref += inp[i].s;

    ll pizzasNeededType1 = sumPref / s;
    ll ans = 0;

    sort(inp.begin(), inp.end(), [](const person &a, const person &b) {
        return abs(a.a - a.b) > abs(b.a - b.b);
    });

    for (ll type1 = pizzasNeededType1; type1 <= pizzasNeededType1+1; type1++) {
        if (type1 > pizzasNeeded) continue;
        ll type2 = pizzasNeeded - type1;

        ll here = 0;

        ll type1Left = type1 * s;
        ll type2Left = type2 * s;

        FOR(i,0,n-1) {
            ll getType1, getType2;

            if (inp[i].a > inp[i].b) {
                getType1 = min(type1Left, inp[i].s);
                getType2 = inp[i].s - getType1;
            } else {
                getType2 = min(type2Left, inp[i].s);
                getType1 = inp[i].s - getType2;
            }

            type1Left -= getType1;
            type2Left -= getType2;

            here += getType1 * inp[i].a + getType2 * inp[i].b;
        }

        ans = max(ans, here);
    }

    cout << ans;

    return 0;
}

/*************************************************************************/


#include <bits/stdc++.h>

using namespace std;

#define int long long

struct man {
	int s, a, b;
};

int n, s;
long long tot;
man a[100000];
vector<man> fi, se;
int A, B;
long long ans;

int get(int A, int B) {
	if(A < 0 || B < 0) return 0;
	int ret = 0;
	int pc1 = A * s, pc2 = B * s;
	for(int i = 0; i < fi.size(); i++) {
		if(fi[i].s <= pc1) {
			ret += 1LL * fi[i].s * fi[i].a;
			pc1 -= fi[i].s;
		} else {
			ret += 1LL * pc1 * fi[i].a + 1LL * (fi[i].s - pc1) * fi[i].b;
			pc1 = 0;
		}
	}
	for(int i = 0; i < se.size(); i++) {
		if(se[i].s <= pc2) {
			ret += 1LL * se[i].s * se[i].b;
			pc2 -= se[i].s;
		} else {
			ret += 1LL * pc2 * se[i].b + 1LL * (se[i].s - pc2) * se[i].a;
			pc2 = 0;
		}
	}
	return ret;
}

int32_t main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> n >> s;
	for(int i = 0; i < n; i++) {
		cin >> a[i].s >> a[i].a >> a[i].b;
		if(a[i].a < a[i].b) se.push_back(a[i]);
		else fi.push_back(a[i]);
		tot += a[i].s;
	}
	tot = (tot + s - 1) / s;
	sort(fi.begin(), fi.end(), [](man a, man b){ return a.a > b.a; });
	sort(se.begin(), se.end(), [](man a, man b){ return a.b > b.b; });
	for(int i = 0; i < fi.size(); i++) A += fi[i].s;
	A = (A + s - 1) / s;
	cout << max({get(A, tot - A), get(A - 1, tot - A + 1), get(A + 1, tot - A - 1)});
}


#include <bits/stdc++.h>

using namespace std;

#define int long long

struct man {
	int s, a, b;
};

int n, s;
long long tot;
man a[100000];
vector<man> fi, se;
int A, B;
long long ans;

int get(int A, int B) {
	if(A < 0 || B < 0) return 0;
	int ret = 0;
	int pc1 = A * s, pc2 = B * s;
	for(int i = 0; i < fi.size(); i++) {
		if(fi[i].s <= pc1) {
			ret += 1LL * fi[i].s * fi[i].a;
			pc1 -= fi[i].s;
		} else {
			ret += 1LL * pc1 * fi[i].a + 1LL * (fi[i].s - pc1) * fi[i].b;
			pc1 = 0;
		}
	}
	for(int i = 0; i < se.size(); i++) {
		if(se[i].s <= pc2) {
			ret += 1LL * se[i].s * se[i].b;
			pc2 -= se[i].s;
		} else {
			ret += 1LL * pc2 * se[i].b + 1LL * (se[i].s - pc2) * se[i].a;
			pc2 = 0;
		}
	}
	return ret;
}

int32_t main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> n >> s;
	for(int i = 0; i < n; i++) {
		cin >> a[i].s >> a[i].a >> a[i].b;
		if(a[i].a <= a[i].b) se.push_back(a[i]);
		else fi.push_back(a[i]);
		tot += a[i].s;
	}
	tot = (tot + s - 1) / s;
	sort(fi.begin(), fi.end(), [](man a, man b){ return a.a - a.b > b.a - b.b; });
	sort(se.begin(), se.end(), [](man a, man b){ return a.b - a.a > b.b - b.a; });
	for(int i = 0; i < fi.size(); i++) A += fi[i].s;
	A = (A + s - 1) / s;
	cout << max({get(A, tot - A), get(A - 1, tot - A + 1), get(A + 1, tot - A - 1)});
}


#include <bits/stdc++.h>

using namespace std;

struct man {
	int s, a, b;
};

int n, s;
int tot;
man a[100000];
vector<man> fi, se;
int A, B;
long long ans;

int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> n >> s;
	for(int i = 0; i < n; i++) {
		cin >> a[i].s >> a[i].a >> a[i].b;
		if(a[i].a < a[i].b) se.push_back(a[i]);
		else fi.push_back(a[i]);
		tot += a[i].s;
	}
	tot = (tot + s - 1) / s;
	sort(fi.begin(), fi.end(), [](man a, man b){ return a.a < b.a; });
	sort(se.begin(), se.end(), [](man a, man b){ return a.b < b.b; });
	for(int i = 0; i < fi.size(); i++) A += fi[i].s;
	for(int i = 0; i < se.size(); i++) B += se[i].s;
	int pc1 = s * (A / s), pc2 = s * (B / s);
	int ost = tot - pc1 / s - pc2 / s;
	while(!fi.empty() && pc1 != 0)
		if(fi.back().s <= pc1) {
			ans += 1LL * fi.back().s * fi.back().a;
			pc1 -= fi.back().s;
			fi.pop_back();
		} else {
			ans += 1LL * pc1 * fi.back().a;
			fi.back().s -= pc1;
			pc1 = 0;
			break;
		}
	while(!se.empty() && pc2 != 0)
		if(se.back().s <= pc2) {
			ans += 1LL * se.back().s * se.back().b;
			pc2 -= se.back().s;
			se.pop_back();
		} else {
			ans += 1LL * pc2 * se.back().b;
			se.back().s -= pc2;
			pc2 = 0;
			break;
		}
	assert(ost <= 2);
	if(ost == 0) return cout << ans, 0;

	long long add1 = 0, add2 = 0;
	for(int i = 0; i < fi.size(); i++) {
		add1 += 1LL * fi[i].s * fi[i].a;
		add2 += 1LL * fi[i].s * fi[i].b;
	}
	for(int i = 0; i < se.size(); i++) {
		add1 += 1LL * se[i].s * se[i].a;
		add2 += 1LL * se[i].s * se[i].b;
	}
	if(ost == 1) return cout << ans + max(add1, add2), 0;
	int sost = s;
	long long add3 = 0, add4 = 0;
	for(int i = 0; i < fi.size(); i++) {
		if(fi[i].s <= sost) {
			add3 += 1LL * fi[i].s * fi[i].a;
			add4 += 1LL * fi[i].s * fi[i].b;
			sost -= fi[i].s;
		} else {
			add3 += 1LL * sost * fi[i].a + 1LL * (fi[i].s - sost) * fi[i].b;
			add4 += 1LL * sost * fi[i].b + 1LL * (fi[i].s - sost) * fi[i].a;
			sost = 0;
		}
	}
	for(int i = 0; i < se.size(); i++) {
		if(se[i].s <= sost) {
			add3 += 1LL * se[i].s * se[i].a;
			add4 += 1LL * se[i].s * se[i].b;
			sost -= se[i].s;
		} else {
			add3 += 1LL * sost * se[i].a + 1LL * (se[i].s - sost) * se[i].b;
			add4 += 1LL * sost * se[i].b + 1LL * (se[i].s - sost) * se[i].a;
			sost = 0;
		}
	}
	cout << ans + max({add1, add2, add3, add4});
}


#include <bits/stdc++.h>

using namespace std;

struct man {
	int s, a, b;
};

int n, s;
int tot;
man a[100000];
vector<man> fi, se;
int A, B;
long long ans;

int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> n >> s;
	for(int i = 0; i < n; i++) {
		cin >> a[i].s >> a[i].a >> a[i].b;
		if(a[i].a < a[i].b) se.push_back(a[i]);
		else fi.push_back(a[i]);
		tot += a[i].s;
	}
	tot = (tot + s - 1) / s;
	sort(fi.begin(), fi.end(), [](man a, man b){ return a.a < b.a; });
	sort(se.begin(), se.end(), [](man a, man b){ return a.b < b.b; });
	for(int i = 0; i < fi.size(); i++) A += fi[i].s;
	for(int i = 0; i < se.size(); i++) B += se[i].s;
	int pc1 = s * (A / s), pc2 = s * (B / s);
	int ost = tot - pc1 / s - pc2 / s;
	while(!fi.empty() && pc1 != 0)
		if(fi.back().s <= pc1) {
			ans += 1LL * fi.back().s * fi.back().a;
			pc1 -= fi.back().s;
			fi.pop_back();
		} else {
			ans += 1LL * pc1 * fi.back().a;
			fi.back().s -= pc1;
			pc1 = 0;
			break;
		}
	while(!se.empty() && pc2 != 0)
		if(se.back().s <= pc2) {
			ans += 1LL * se.back().s * se.back().b;
			pc2 -= se.back().s;
			se.pop_back();
		} else {
			ans += 1LL * pc2 * se.back().b;
			se.back().s -= pc2;
			pc2 = 0;
			break;
		}
	assert(ost <= 2);
	if(ost == 0) return cout << ans, 0;

	long long add1 = 0, add2 = 0;
	for(int i = 0; i < fi.size(); i++) {
		add1 += 1LL * fi[i].s * fi[i].a;
		add2 += 1LL * fi[i].s * fi[i].b;
	}
	for(int i = 0; i < se.size(); i++) {
		add1 += 1LL * se[i].s * se[i].a;
		add2 += 1LL * se[i].s * se[i].b;
	}
	if(ost == 1) return cout << ans + max(add1, add2), 0;
	int sost1 = s, sost2 = s;
	long long add3 = 0;
	for(int i = 0; i < fi.size(); i++) {
		if(fi[i].s <= sost1) {
			add3 += 1LL * fi[i].s * fi[i].a;
			sost1 -= fi[i].s;
		} else {
			add3 += 1LL * sost1 * fi[i].a + 1LL * (fi[i].s - sost1) * fi[i].b;
			sost1 = 0;
		}
	}
	for(int i = 0; i < se.size(); i++) {
		if(se[i].s <= sost2) {
			add3 += 1LL * se[i].s * se[i].b;
			sost2 -= se[i].s;
		} else {
			add3 += 1LL * sost2 * se[i].b + 1LL * (se[i].s - sost2) * se[i].a;
			sost2 = 0;
		}
	}

	cout << ans + max({add1, add2, add3});
}


#include <bits/stdc++.h>

using namespace std;

#define int long long

struct man {
	int s, a, b;
};

int n, s;
long long tot;
man a[100000];
vector<man> fi, se;
int A, B;
long long ans;

int get(int A, int B) {
	if(A < 0 || B < 0) return 0;
	int ret = 0;
	int pc1 = A * s, pc2 = B * s;
	for(int i = 0; i < fi.size(); i++) {
		if(fi[i].s <= pc1) {
			ret += 1LL * fi[i].s * fi[i].a;
			pc1 -= fi[i].s;
		} else {
			ret += 1LL * pc1 * fi[i].a + 1LL * (fi[i].s - pc1) * fi[i].b;
			pc1 = 0;
		}
	}
	for(int i = 0; i < se.size(); i++) {
		if(se[i].s <= pc2) {
			ret += 1LL * se[i].s * se[i].b;
			pc2 -= se[i].s;
		} else {
			ret += 1LL * pc2 * se[i].b + 1LL * (se[i].s - pc2) * se[i].a;
			pc2 = 0;
		}
	}
	return ret;
}

int32_t main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> n >> s;
	for(int i = 0; i < n; i++) {
		cin >> a[i].s >> a[i].a >> a[i].b;
		if(a[i].a <= a[i].b) se.push_back(a[i]);
		else fi.push_back(a[i]);
		tot += a[i].s;
	}
	tot = (tot + s - 1) / s;
	sort(fi.begin(), fi.end(), [](man a, man b){ return a.a > b.a; });
	sort(se.begin(), se.end(), [](man a, man b){ return a.b > b.b; });
	for(int i = 0; i < fi.size(); i++) A += fi[i].s;
	A = (A + s - 1) / s;
	cout << max({get(A, tot - A), get(A - 1, tot - A + 1), get(A + 1, tot - A - 1)});
}


#include <bits/stdc++.h>

using namespace std;

#define int long long

struct man {
	int s, a, b;
};

int n, s;
long long tot;
man a[100000];
vector<man> fi, se;
int A, B;
long long ans;

int32_t main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> n >> s;
	for(int i = 0; i < n; i++) {
		cin >> a[i].s >> a[i].a >> a[i].b;
		if(a[i].a < a[i].b) se.push_back(a[i]);
		else fi.push_back(a[i]);
		tot += a[i].s;
	}
	tot = (tot + s - 1) / s;
	sort(fi.begin(), fi.end(), [](man a, man b){ return a.a < b.a; });
	sort(se.begin(), se.end(), [](man a, man b){ return a.b < b.b; });
	for(int i = 0; i < fi.size(); i++) A += fi[i].s;
	for(int i = 0; i < se.size(); i++) B += se[i].s;
	int pc1 = s * (A / s), pc2 = s * (B / s);
	int ost = tot - pc1 / s - pc2 / s;
	while(!fi.empty() && pc1 != 0)
		if(fi.back().s <= pc1) {
			ans += 1LL * fi.back().s * fi.back().a;
			pc1 -= fi.back().s;
			fi.pop_back();
		} else {
			ans += 1LL * pc1 * fi.back().a;
			fi.back().s -= pc1;
			pc1 = 0;
			break;
		}
	while(!se.empty() && pc2 != 0)
		if(se.back().s <= pc2) {
			ans += 1LL * se.back().s * se.back().b;
			pc2 -= se.back().s;
			se.pop_back();
		} else {
			ans += 1LL * pc2 * se.back().b;
			se.back().s -= pc2;
			pc2 = 0;
			break;
		}
	assert(ost <= 2);
	if(ost == 0) return cout << ans, 0;

	long long add1 = 0, add2 = 0;
	for(int i = 0; i < fi.size(); i++) {
		add1 += 1LL * fi[i].s * fi[i].a;
		add2 += 1LL * fi[i].s * fi[i].b;
	}
	for(int i = 0; i < se.size(); i++) {
		add1 += 1LL * se[i].s * se[i].a;
		add2 += 1LL * se[i].s * se[i].b;
	}
	if(ost == 1) return cout << ans + max(add1, add2), 0;
	int sost1 = s, sost2 = s;
	long long add3 = 0;
	for(int i = 0; i < fi.size(); i++) {
		if(fi[i].s <= sost1) {
			add3 += 1LL * fi[i].s * fi[i].a;
			sost1 -= fi[i].s;
		} else {
			add3 += 1LL * sost1 * fi[i].a + 1LL * (fi[i].s - sost1) * fi[i].b;
			sost1 = 0;
		}
	}
	for(int i = 0; i < se.size(); i++) {
		if(se[i].s <= sost2) {
			add3 += 1LL * se[i].s * se[i].b;
			sost2 -= se[i].s;
		} else {
			add3 += 1LL * sost2 * se[i].b + 1LL * (se[i].s - sost2) * se[i].a;
			sost2 = 0;
		}
	}

	cout << ans + max({add1, add2, add3});
}


#include <bits/stdc++.h>

using namespace std;

#define int long long

struct man {
	int s, a, b;
};

int n, s;
long long tot;
man a[100000];
vector<man> fi, se;
int A, B;
long long ans;

int get(int A, int B) {
	int ret = 0;
	int pc1 = A * s, pc2 = B * s;
	for(int i = 0; i < fi.size(); i++) {
		if(fi[i].s <= pc1) {
			ret += 1LL * fi[i].s * fi[i].a;
			pc1 -= fi[i].s;
		} else {
			ret += 1LL * pc1 * fi[i].a + 1LL * (fi[i].s - pc1) * fi[i].b;
			pc1 = 0;
		}
	}
	for(int i = 0; i < se.size(); i++) {
		if(se[i].s <= pc2) {
			ret += 1LL * se[i].s * se[i].b;
			pc2 -= se[i].s;
		} else {
			ret += 1LL * pc2 * se[i].b + 1LL * (se[i].s - pc2) * se[i].a;
			pc2 = 0;
		}
	}
	return ret;
}

int32_t main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> n >> s;
	for(int i = 0; i < n; i++) {
		cin >> a[i].s >> a[i].a >> a[i].b;
		if(a[i].a < a[i].b) se.push_back(a[i]);
		else fi.push_back(a[i]);
		tot += a[i].s;
	}
	tot = (tot + s - 1) / s;
	sort(fi.begin(), fi.end(), [](man a, man b){ return a.a > b.a; });
	sort(se.begin(), se.end(), [](man a, man b){ return a.b > b.b; });

	int bl = 0, br = tot - 1;
	while(bl < br) {
		int bm = (bl + br) / 2;
		if(get(bm, tot - bm) < get(bm + 1, tot - bm - 1)) bl = bm + 1;
		else br = bm;
	}
	cout << max(get(bl, tot - bl), get(bl + 1, tot - bl - 1));
}


#include <bits/stdc++.h>

using namespace std;

#define int long long

struct man {
	int s, a, b;
};

int n, s;
long long tot;
man a[100000];
vector<man> fi, se;
int A, B;
long long ans;

int32_t main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> n >> s;
	for(int i = 0; i < n; i++) {
		cin >> a[i].s >> a[i].a >> a[i].b;
		if(a[i].a < a[i].b) se.push_back(a[i]);
		else fi.push_back(a[i]);
		tot += a[i].s;
	}
	tot = (tot + s - 1) / s;
	sort(fi.begin(), fi.end(), [](man a, man b){ return a.a < b.a; });
	sort(se.begin(), se.end(), [](man a, man b){ return a.b < b.b; });
	for(int i = 0; i < fi.size(); i++) A += fi[i].s;
	for(int i = 0; i < se.size(); i++) B += se[i].s;
	int pc1 = s * (A / s), pc2 = s * (B / s);
	int ost = tot - pc1 / s - pc2 / s;
	while(!fi.empty() && pc1 != 0)
		if(fi.back().s <= pc1) {
			ans += 1LL * fi.back().s * fi.back().a;
			pc1 -= fi.back().s;
			fi.pop_back();
		} else {
			ans += 1LL * pc1 * fi.back().a;
			fi.back().s -= pc1;
			pc1 = 0;
			break;
		}
	while(!se.empty() && pc2 != 0)
		if(se.back().s <= pc2) {
			ans += 1LL * se.back().s * se.back().b;
			pc2 -= se.back().s;
			se.pop_back();
		} else {
			ans += 1LL * pc2 * se.back().b;
			se.back().s -= pc2;
			pc2 = 0;
			break;
		}
	assert(ost <= 2);
	if(ost == 0) return cout << ans, 0;

	sort(fi.begin(), fi.end(), [](man a, man b){ return a.a > b.a; });
	sort(se.begin(), se.end(), [](man a, man b){ return a.b > b.b; });
	long long add1 = 0, add2 = 0;
	for(int i = 0; i < fi.size(); i++) {
		add1 += 1LL * fi[i].s * fi[i].a;
		add2 += 1LL * fi[i].s * fi[i].b;
	}
	for(int i = 0; i < se.size(); i++) {
		add1 += 1LL * se[i].s * se[i].a;
		add2 += 1LL * se[i].s * se[i].b;
	}
	if(ost == 1) return cout << ans + max(add1, add2), 0;
	int sost1 = s, sost2 = s;
	long long add3 = 0;
	for(int i = 0; i < fi.size(); i++) {
		if(fi[i].s <= sost1) {
			add3 += 1LL * fi[i].s * fi[i].a;
			sost1 -= fi[i].s;
		} else {
			add3 += 1LL * sost1 * fi[i].a + 1LL * (fi[i].s - sost1) * fi[i].b;
			sost1 = 0;
		}
	}
	for(int i = 0; i < se.size(); i++) {
		if(se[i].s <= sost2) {
			add3 += 1LL * se[i].s * se[i].b;
			sost2 -= se[i].s;
		} else {
			add3 += 1LL * sost2 * se[i].b + 1LL * (se[i].s - sost2) * se[i].a;
			sost2 = 0;
		}
	}

	cout << ans + max({add1, add2, add3});
}