CF BUDDY
← Problems·

1690E · Price Maximization

1500 · binary search, greedy, math

Problem: A batch of nn goods (nn — an even number) is brought to the store, ii-th of which has weight aia_i. Before selling the goods, they must be packed into packages. After packing, the following will be done:

  • There will be n2\frac{n}{2} packages, each package contains exactly two goods;
  • The weight of the package that contains goods with indices ii and jj (1i,jn1 \le i, j \le n) is ai+aja_i + a_j.

With this, the cost of a package of weight xx is always xk\left \lfloor\frac{x}{k}\right\rfloor burles (rounded down), where kk — a fixed and given value.

Pack the goods to the packages so that the revenue from their sale is maximized. In other words, make such n2\frac{n}{2} pairs of given goods that the sum of the values xik\left \lfloor\frac{x_i}{k} \right \rfloor, where xix_i is the weight of the package number ii (1in21 \le i \le \frac{n}{2}), is maximal.

For example, let n=6,k=3n = 6, k = 3, weights of goods a=[3,2,7,1,4,8]a = [3, 2, 7, 1, 4, 8]. Let's pack them into the following packages.

  • In the first package we will put the third and sixth goods. Its weight will be a3+a6=7+8=15a_3 + a_6 = 7 + 8 = 15. The cost of the package will be 153=5\left \lfloor\frac{15}{3}\right\rfloor = 5 burles.
  • In the second package put the first and fifth goods, the weight is a1+a5=3+4=7a_1 + a_5 = 3 + 4 = 7. The cost of the package is 73=2\left \lfloor\frac{7}{3}\right\rfloor = 2 burles.
  • In the third package put the second and fourth goods, the weight is a2+a4=2+1=3a_2 + a_4 = 2 + 1 = 3. The cost of the package is 33=1\left \lfloor\frac{3}{3}\right\rfloor = 1 burle.

With this packing, the total cost of all packs would be 5+2+1=85 + 2 + 1 = 8 burles.

Input Format: The first line of the input contains an integer tt (1t1041 \le t \le 10^4) —the number of test cases in the test.

The descriptions of the test cases follow.

The first line of each test case contains two integers nn (2n21052 \le n \le 2\cdot10^5) and kk (1k10001 \le k \le 1000). The number nn — is even.

The second line of each test case contains exactly nn integers a1,a2,,ana_1, a_2, \dots, a_n (0ai1090 \le a_i \le 10^9).

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

Output Format: For each test case, print on a separate line a single number — the maximum possible total cost of all the packages.

Note: The first test case is analyzed in the statement.

In the second test case, you can get a total value equal to 44 if you put the first and second goods in the first package and the third and fourth goods in the second package.

In the third test case, the cost of each item is 00, so the total cost will also be 00.

Sample Cases

Case 1

Input

6
6 3
3 2 7 1 4 8
4 3
2 1 5 6
4 12
0 0 0 0
2 1
1 1
6 10
2 0 0 5 9 4
6 5
5 3 8 6 3 2

Output

8
4
0
2
1
5

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback