#P13962. [ICPC 2023 Nanjing R] 电梯

[ICPC 2023 Nanjing R] 电梯

Description

There are nn groups of parcels waiting to be delivered. There are cic_i parcels in the ii-th group, where each parcel has the same weight of wiw_i (wiw_i is either 11 or 22) and should be delivered to the fif_i-th floor.

There is an elevator which can carry parcels with a maximum total weight of kk (kk is even) for each ride. The elevator will start from the ground floor and move gradually towards the hightest floor hh where a parcel in the elevator should be delivered and finally move back to the ground floor, costing hh units of electric power for that ride.

More formally, let (w,f)(w, f) be a parcel whose weight is ww and should be delivered to the ff-th floor. A multiset (a set which allows duplicated elements) of parcels P\mathbb{P} can be delivered in the same ride if (w,f)Pwk\sum\limits_{(w, f) \in \mathbb{P}} w \le k. This ride will cost max(w,f)Pf\max\limits_{(w, f) \in \mathbb{P}} f units of electric power.

What's the minimum total units of electric power needed to deliver all parcels?

Note that each ride can contain parcels from different groups and each group of parcels can be delivered through multiple rides. You can treat this problem as if there are a total of i=1nci\sum\limits_{i=1}^n c_i parcels to be delivered, just that some parcels may have the same weight and the same destination.

Input Format

There are multiple test cases. The first line of the input contains an integer TT indicating the number of test cases. For each test case:

The first line contains two integers nn and kk (1n1051 \le n \le 10^5, 2k2×10102 \le k \le 2 \times 10^{10}, kk is even) indicating the number of groups and the capacity of the elevator.

For the following nn lines, the ii-th line contains three integers cic_i, wiw_i and fif_i (1ci1051 \le c_i \le 10^5, wi{1,2}w_i \in \{1, 2\}, 1fi1051 \le f_i \le 10^5) indicating the number of parcels in the ii-th group, the weight of each parcel and the destination of the parcels.

It's guaranteed that the sum of nn over all test cases does not exceed 3×1053 \times 10^5.

Output Format

For each test case output one line containing one integer indicating the minimum total units of electric power needed to deliver all parcels.

2
4 6
1 1 8
7 2 5
1 1 7
3 2 6
8 1200000
100000 1 100000
100000 1 12345
100000 2 100000
100000 2 12345
100000 1 100000
100000 1 12345
100000 2 100000
100000 2 12345
24
100000

Hint

For the first sample test case we can follow this strategy:

  • In the first ride, deliver parcel (2,6)(2, 6), (2,6)(2, 6) and (2,5)(2, 5). This ride costs 66 units of electric power.
  • In the second ride, deliver parcel (1,8)(1, 8), (1,7)(1, 7), (2,6)(2, 6) and (2,5)(2, 5). This ride costs 88 units of electric power.
  • In the third ride, deliver parcel (2,5)(2, 5), (2,5)(2, 5) and (2,5)(2, 5). This ride costs 55 units of electric power.
  • In the fourth ride, deliver parcel (2,5)(2, 5) and (2,5)(2, 5). This ride costs 55 units of electric power.

The total units of electric power is 6+8+5+5=246 + 8 + 5 + 5 = 24. It can be proven that this is the minimum total units of electric power needed.

For the second sample test case, all parcels can be delivered in the same ride.