Problem:
Score : 800 points

Problem Statement

Takahashi and Aoki will take N exams numbered 1 to N.
They have decided to compete in these exams.
The winner will be determined as follows:

For each exam i, Takahashi decides its importance c_i, which must be an integer between l_i and u_i (inclusive).

Let A be \sum_{i=1}^{N} c_i \times (Takahashi's score on Exam i), and B be \sum_{i=1}^{N} c_i \times (Aoki's score on Exam i). Takahashi wins if A \geq B, and Aoki wins if A < B.

Takahashi knows that Aoki will score b_i on Exam i, with his supernatural power.

Takahashi himself, on the other hand, will score 0 on all the exams without studying more. For each hour of study, he can increase his score on some exam by 1. (He can only study for an integer number of hours.)
However, he cannot score more than X on an exam, since the perfect score for all the exams is X.

Print the minimum number of study hours required for Takahashi to win.

Constraints

1 \leq N \leq 10^5

1 \leq X \leq 10^5

0 \leq b_i \leq X (1 \leq i \leq N)

1 \leq l_i \leq u_i \leq 10^5 (1 \leq i \leq N)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X
b_1 l_1 u_1
b_2 l_2 u_2
:
b_N l_N u_N

Output

Print the minimum number of study hours required for Takahashi to win.

Sample Input 1

2 100
85 2 3
60 1 1

Sample Output 1

115

One optimal strategy is as follows:

Choose c_1 = 3, c_2 = 1.

Study to score 100 on Exam 1 and 15 on Exam 2.

Then, A = 3 \times 100 + 1 \times 15 = 315, B = 3 \times 85 + 1 \times 60 = 315 and Takahashi will win.

Sample Input 2

2 100
85 2 3
60 10 10

Sample Output 2

77

Sample Input 3

1 100000
31415 2718 2818

Sample Output 3

31415

Sample Input 4

10 1000
451 4593 6263
324 310 6991
378 1431 7068
71 1757 9218
204 3676 4328
840 6221 9080
684 1545 8511
709 5467 8674
862 6504 9835
283 4965 9980

Sample Output 4

2540

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
// #define int ll
using PII = pair<ll, ll>;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()

template<typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template<typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template<typename T> bool IN(T a, T b, T x) { return a<=x&&x<b; }
template<typename T> T ceil(T a, T b) { return a/b + !!(a%b); }

template<typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts) {
    return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}
template<typename T,typename V> typename enable_if<is_class<T>::value==0>::type
fill_v(T &t, const V &v) { t=v; }
template<typename T,typename V> typename enable_if<is_class<T>::value!=0>::type
fill_v(T &t, const V &v ) { for(auto &e:t) fill_v(e,v); }

template<class S,class T>
ostream &operator <<(ostream& out,const pair<S,T>& a) {
    out<<'('<<a.first<<','<<a.second<<')'; return out;
}
template<class T>
ostream &operator <<(ostream& out,const vector<T>& a){
    out<<'[';
    for(const T &i: a) out<<i<<',';
    out<<']';
    return out;
}
template<class T>
ostream &operator <<(ostream& out, const set<T>& a) {
    out<<'{';
    for(const T &i: a) out<<i<<',';
    out<<'}';
    return out;
}
template<class T, class S>
ostream &operator <<(ostream& out, const map<T,S>& a) {
    out<<'{';
    for(auto &i: a) out<<i<<',';
    out<<'}';
    return out;
}

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}; // DRUL
const int INF = 1<<30;
const ll LLINF = 1LL<<60;
const ll MOD = 1000000007;

signed main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    ll n, X;
    cin >> n >> X;
    vector<pair<PII,PII>> vec(n);
    REP(i, n) {
        ll b, l, u;
        cin >> b >> l >> u;
        vec[i] = make_pair(PII((X-b)*u, -b), PII(l, u));
    }

    sort(ALL(vec), greater<>());
    vector<ll> b(n), l(n), u(n);
    REP(i, n) {
        b[i] = -vec[i].first.second;
        l[i] = vec[i].second.first;
        u[i] = vec[i].second.second;
    }

    // cout << b << endl << l << endl << u << endl;
    // cout << endl;

    auto f = [&](ll i) {
        ll bsum = 0, asum = 0;
        vector<ll> c(n);
        REP(j, n) {
            if(j <= i) {
                // u[i]
                c[j] = u[j];
                bsum += u[j] * b[j];
            } else {
                // l[i]
                c[j] = l[j];
                bsum += l[j] * b[j];
            }
        }
        sort(ALL(c), greater<>());
        ll cnt = 0;
        REP(j, n) {
            if(asum + c[j]*X <= bsum) {
                asum += c[j]*X;
                cnt += X;
            } else {
                cnt += (bsum - asum + c[j] - 1) / c[j];
                break;
            }
        }
        return cnt;
    };

    ll x0 = 0, x3 = n-1;
    while(x3-x0>2) {
        ll x1 = (x0*2+x3) / 3, x2 = (x0+x3*2) / 3;
        // cout << x0 << " " << x1 << " " << x2 << " " << x3 << endl;
        ll vl = f(x1), vr = f(x2);
        if(vl > vr) x0 = x1;
        else x3 = x2;
    }

    // cout << x0 << " " << x3 << endl;

    ll ret = LLINF;
    for(ll x=max(0LL, x0-20); x<min(n, x0+20); ++x) {
        // cout << "x=" << x << endl;
        chmin(ret, f(x));
    }
    cout << ret << endl;

    return 0;
}