Ordering Pizza

It’s another Startup finals, and that means there is pizza to order for the onsite contestants. There are only 2 types of pizza (obviously not, but let’s just pretend for the sake of the problem), and all pizzas contain exactly S slices.

It is known that the i-th contestant will eat s i slices of pizza, and gain a i happiness for each slice of type 1 pizza they eat, and b i happiness for each slice of type 2 pizza they eat. We can order any number of type 1 and type 2 pizzas, but we want to buy the minimum possible number of pizzas for all of the contestants to be able to eat their required number of slices. Given that restriction, what is the maximum possible total happiness that can be achieved?

Input

The first line of input will contain integers N and S (1 ≤ N ≤ 105, 1 ≤ S ≤ 105), the number of contestants and the number of slices per pizza, respectively. N lines follow.

The i-th such line contains integers s ia i, and b i (1 ≤ s i ≤ 105, 1 ≤ a i ≤ 105, 1 ≤ b i ≤ 105), the number of slices the i-th contestant will eat, the happiness they will gain from each type 1 slice they eat, and the happiness they will gain from each type 2 slice they eat, respectively.

Output

Print the maximum total happiness that can be achieved.

Examples

input

3 12
3 5 7
4 6 7
5 9 5

output

84

input

6 10
7 4 7
5 8 8
12 5 8
6 11 6
3 3 7
5 9 6

output

314

Note

In the first example, you only need to buy one pizza. If you buy a type 1 pizza, the total happiness will be 3·5 + 4·6 + 5·9 = 84, and if you buy a type 2 pizza, the total happiness will be 3·7 + 4·7 + 5·5 = 74.

Solution:

#include <bits/stdc++.h>

using namespace std;

const int N = 1234567;

int eat[N], a[N], b[N];

int main() {
int n, s;
scanf("%d %d", &n, &s);
long long total = 0;
for (int i = 0; i < n; i++) {
    scanf("%d %d %d", eat + i, a + i, b + i);
    total += eat[i];
  }
  long long pizzas = (total + s - 1) / s;
  eat[n] = s * pizzas - total;
  a[n] = 0;
  b[n] = 0;
  n++;
  vector< pair<long long, long long> > a_to_b, b_to_a;
long long ans = 0;
long long cnt_a = 0, cnt_b = 0;
for (int i = 0; i < n; i++) {
    if (a[i] > b[i]) {
ans += eat[i] * 1LL * a[i];
a_to_b.emplace_back(a[i] - b[i], eat[i]);
cnt_a += eat[i];
} else {
ans += eat[i] * 1LL * b[i];
b_to_a.emplace_back(b[i] - a[i], eat[i]);
cnt_b += eat[i];
}
}
long long best = (long long) 1e18;
{
long long extra = cnt_a % s;
sort(a_to_b.begin(), a_to_b.end());
long long cur = 0;
for (int i = 0; i < (int) a_to_b.size(); i++) {
      auto &p = a_to_b[i];
      long long take = min(p.second, extra);
      cur += take * p.first;
      extra -= take;
    }
    best = min(best, cur);
  }
  {
    long long extra = cnt_b % s;
    sort(b_to_a.begin(), b_to_a.end());
    long long cur = 0;
    for (int i = 0; i < (int) b_to_a.size(); i++) {
      auto &p = b_to_a[i];
      long long take = min(p.second, extra);
      cur += take * p.first;
      extra -= take;
    }
    best = min(best, cur);
  }
  cout << (ans - best) << endl;
  return 0;
}