CF1876A.Helmets in Night Light

传统题 时间 2000 ms 内存 256 MiB 5 尝试 1 已通过 1 标签

Helmets in Night Light

Pak Chanek is the chief of a village named Khuntien. On one night filled with lights, Pak Chanek has a sudden and important announcement that needs to be notified to all of the nn residents in Khuntien.

First, Pak Chanek shares the announcement directly to one or more residents with a cost of pp for each person. After that, the residents can share the announcement to other residents using a magical helmet-shaped device. However, there is a cost for using the helmet-shaped device. For each ii, if the ii-th resident has got the announcement at least once (either directly from Pak Chanek or from another resident), he/she can share the announcement to at most aia_i other residents with a cost of bib_i for each share.

If Pak Chanek can also control how the residents share the announcement to other residents, what is the minimum cost for Pak Chanek to notify all nn residents of Khuntien about the announcement?

Input

Each test contains multiple test cases. The first line contains an integer tt (1t1041 \leq t \leq 10^4) — the number of test cases. The following lines contain the description of each test case.

The first line contains two integers nn and pp (1n1051 \leq n \leq 10^5; 1p1051 \leq p \leq 10^5) — the number of residents and the cost for Pak Chanek to share the announcement directly to one resident.

The second line contains nn integers a1,a2,a3,,ana_1,a_2,a_3,\ldots,a_n (1ai1051\leq a_i\leq10^5) — the maximum number of residents that each resident can share the announcement to.

The third line contains nn integers b1,b2,b3,,bnb_1,b_2,b_3,\ldots,b_n (1bi1051\leq b_i\leq10^5) — the cost for each resident to share the announcement to one other resident.

It is guaranteed that the sum of nn over all test cases does not exceed 10510^5.

Output

For each test case, output a line containing an integer representing the minimum cost to notify all nn residents of Khuntien about the announcement.

Note

In the first test case, the following is a possible optimal strategy:

  1. Pak Chanek shares the announcement directly to the 33-rd, 55-th, and 66-th resident. This requires a cost of p+p+p=3+3+3=9p+p+p=3+3+3=9.
  2. The 33-rd resident shares the announcement to the 11-st and 22-nd resident. This requires a cost of b3+b3=2+2=4b_3+b_3=2+2=4.
  3. The 22-nd resident shares the announcement to the 44-th resident. This requires a cost of b2=3b_2=3.

The total cost is 9+4+3=169+4+3=16. It can be shown that there is no other strategy with a smaller cost.

Samples

3
6 3
2 3 2 1 1 3
4 3 2 6 3 6
1 100000
100000
1
4 94
1 4 2 3
103 96 86 57
16
100000
265

在线编程 IDE

建议全屏模式获得最佳体验