Copou Grand Prix

Time limit: 1s Memory limit: 128MB Input: Output:

Breaking news! Contestant Andu from Iași has announced that he will not attend the IIOT finals because the Copou Grand Prix will take place at the same time as the contest.

The Grand Prix consists of NN races in which MM drivers compete, two of whom are Alice and Bob. We know that Alice finished the races in positions A1,A2,,ANA_1, A_2, \ldots, A_N, and Bob in positions B1,B2,,BNB_1, B_2, \ldots, B_N.

Task

Because defeat is inevitable, Andu wants to assign the number of points pip_i awarded for each finishing position ii (1iM1 \leq i \leq M) in such a way that the following conditions are met:

  • W=p1>p2>>pM=0W = p_1 > p_2 > \ldots > p_M = 0, where WW is a given positive integer.
  • The value D=i=1NpAii=1NpBiD = \displaystyle \sum_{i=1}^{N} p_{A_i} - \sum_{i=1}^{N} p_{B_i} is maximized.

Once assigned, the points are the same for all NN races. Your task is to find the maximum possible value of DD.

Input data

The first line of the input contains an integer TT, representing the number of test cases. Each test case is described as follows:

  • The first line contains three space-separated integers, NN, MM, and WW, where NN is the number of races, MM is the number of drivers, and WW is the maximum number of points awarded for finishing in first place.
  • The second line contains NN space-separated integers, A1,A2,,ANA_1, A_2, \ldots, A_N, representing the Alices's finishing positions in each race.
  • The third line contains NN space-separated integers, B1,B2,,BNB_1, B_2, \ldots, B_N, representing the Bob's finishing positions in each race.

Output data

For each test case, you must output the maximum value achievable for DD on a separate line.

Constraints and clarifications

  • 1T30 0001 \leq T \leq 30 \ 000
  • 1N100 0001 \leq N \leq 100 \ 000
  • 1M1091 \leq M \leq 10^9
  • M1W109M - 1 \leq W \leq 10^9
  • The sum of NN over all test cases will not exceed 300 000300 \ 000.
  • 1Ai,BiM1 \leq A_i, B_i \leq M
  • AiBiA_i \neq B_i, for every 1iN1 \leq i \leq N
# Score Constraints
0 0 Examples
1 6 W=M1W = M - 1
2 11 N,M,W10N, M, W \leq 10
3 38 T3000T \le 3000; N,M,W100N, M, W \leq 100
4 25 The sum of MM over all test cases will not exceed 300 000300 \ 000.
5 16 No further restrictions.

Example

stdin

6
6 5 5
1 1 3 3 3 5
1 2 2 3 4 5
8 12 15
1 1 2 2 3 3 6 11
1 2 2 3 5 5 7 9
8 12 15
1 2 2 3 5 5 7 9
1 1 2 2 3 3 6 11
4 8 10
5 6 7 8
1 2 3 4
6 6 5
1 2 3 4 5 6
1 2 3 4 5 6
10 6 6
2 3 3 3 4 4 6 6 6 6
2 2 2 3 3 4 4 4 5 6

stdout

2
13
-1
-19
0
-8

Explanation

In the first test case, the optimal number of points awarded for each position (with regard to maximizing DD) is [5,3,2,1,0][5, 3, 2, 1, 0].

For this point distribution, D=(p1+p1+p3+p3+p3+p5)(p1+p2+p2+p3+p4+p5)=(5+5+2+2+2+0)(5+3+3+2+1+0)=1614=2D = (p_1+p_1+p_3+p_3+p_3+p_5) - (p_1+p_2+p_2+p_3+p_4+p_5)=(5+5+2+2+2+0)-(5+3+3+2+1+0)=16-14=2.

In the second test case, an optimal point distribution is [15,14,13,10,7,6,5,4,3,2,1,0][15, 14, 13, 10, 7, 6, 5, 4, 3, 2, 1, 0].

Log in or sign up to be able to send submissions!