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 Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.io.*;
import java.util.*;

public class Main {
    FastScanner in;
    PrintWriter out;


    class Exam implements Comparable<Exam> {
        int hisScore;
        int minCost, maxCost;
        long totalAdd;

        @Override
        public String toString() {
            return "Exam{" +
                    "hisScore=" + hisScore +
                    ", minCost=" + minCost +
                    ", maxCost=" + maxCost +
                    ", totalAdd=" + totalAdd +
                    '}';
        }

        public Exam(int hisScore, int minCost, int maxCost, int maxScore) {
            this.hisScore = hisScore;
            this.minCost = minCost;
            this.maxCost = maxCost;
            totalAdd = hisScore * 1L * minCost + (maxScore - hisScore) * 1L * maxCost;
        }

        @Override
        public int compareTo(Exam exam) {
            if (totalAdd != exam.totalAdd) {
                return -Long.compare(totalAdd, exam.totalAdd);
            }
            return -Integer.compare(minCost, exam.minCost);
        }

        long getCost(long needPoints) {
            if (needPoints > totalAdd) {
                throw new AssertionError();
            }
            if (needPoints == 0) {
                return 0;
            }
            if (minCost * 1L * hisScore >= needPoints) {
                return 1 + (needPoints - 1) / minCost;
            }
            needPoints -= hisScore * 1L * minCost;
            if (needPoints == 0) {
                return hisScore;
            }
            return hisScore + 1 + (needPoints - 1) / maxCost;
        }
    }

    void solve() {
        int n = in.nextInt();
        int maxScore = in.nextInt();
        Exam[] a = new Exam[n];
        long sumHisScore = 0;
        for (int i = 0; i < n; i++) {
            a[i] = new Exam(in.nextInt(), in.nextInt(), in.nextInt(), maxScore);
            sumHisScore += a[i].minCost * 1L * a[i].hisScore;
        }
        Arrays.sort(a);
//        System.err.println(Arrays.toString(a));
//        System.err.println("him = " + sumHisScore);
        long[] prefSum = new long[n + 1];
        for (int i = 0; i < n; i++) {
            prefSum[i + 1] = prefSum[i] + a[i].totalAdd;
        }
        long res = Long.MAX_VALUE;
        for (int notFully = 0; notFully < n; notFully++) {
//            System.err.println(notFully +"!");
            int l = -1, r = n;
            while (r - l > 1) {
                int mid = (l + r) >> 1;
                if (prefSum[mid + 1] - (mid < notFully ? 0 : a[notFully].totalAdd) <= sumHisScore) {
                    l = mid;
                } else {
                    r = mid;
                }
            }
            while (r >= 0) {
                if ((prefSum[r] - (r - 1 < notFully ? 0 : a[notFully].totalAdd)) + a[notFully].totalAdd < sumHisScore) {
                    break;
                }
                long needMore = sumHisScore - (prefSum[r] - (r - 1 < notFully ? 0 : a[notFully].totalAdd));
                long alreadyCost = maxScore * 1L * r;
                if (r - 1 < notFully) {

                } else {
                    alreadyCost -= maxScore;
                }
                long cost = alreadyCost + a[notFully].getCost(needMore);
                res = Math.min(res, cost);
                r--;
            }
        }
        out.println(res);
    }

    void run() {
        try {
            in = new FastScanner(new File("Main.in"));
            out = new PrintWriter(new File("Main.out"));

            solve();

            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    void runIO() {

        in = new FastScanner(System.in);
        out = new PrintWriter(System.out);

        solve();

        out.close();
    }

    class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner(File f) {
            try {
                br = new BufferedReader(new FileReader(f));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

        public FastScanner(InputStream f) {
            br = new BufferedReader(new InputStreamReader(f));
        }

        String next() {
            while (st == null || !st.hasMoreTokens()) {
                String s = null;
                try {
                    s = br.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (s == null)
                    return null;
                st = new StringTokenizer(s);
            }
            return st.nextToken();
        }

        boolean hasMoreTokens() {
            while (st == null || !st.hasMoreTokens()) {
                String s = null;
                try {
                    s = br.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (s == null)
                    return false;
                st = new StringTokenizer(s);
            }
            return true;
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }
    }

    public static void main(String[] args) {
        new Main().runIO();
    }
}