CF BUDDY
← Problems·

1969D · Shop Game

1900 · data structures, greedy, math

Problem: Alice and Bob are playing a game in the shop. There are nn items in the shop; each item has two parameters: aia_i (item price for Alice) and bib_i (item price for Bob).

Alice wants to choose a subset (possibly empty) of items and buy them. After that, Bob does the following:

  • if Alice bought less than kk items, Bob can take all of them for free;
  • otherwise, he will take kk items for free that Alice bought (Bob chooses which kk items it will be), and for the rest of the chosen items, Bob will buy them from Alice and pay bib_i for the ii-th item.

Alice's profit is equal to iSbijTaj\sum\limits_{i \in S} b_i - \sum\limits_{j \in T} a_j, where SS is the set of items Bob buys from Alice, and TT is the set of items Alice buys from the shop. In other words, Alice's profit is the difference between the amount Bob pays her and the amount she spends buying the items.

Alice wants to maximize her profit, Bob wants to minimize Alice's profit. Your task is to calculate Alice's profit if both Alice and Bob act optimally.

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

The first line of each test case contains two integers nn and kk (1n21051 \le n \le 2 \cdot 10^5; 0kn0 \le k \le n).

The second line contains nn integers a1,a2,,ana_1, a_2, \dots, a_n (1ai1091 \le a_i \le 10^9).

The third line contains nn integers b1,b2,,bnb_1, b_2, \dots, b_n (1bi1091 \le b_i \le 10^9).

Additional constraint on the input: the sum of nn over all test cases doesn't exceed 21052 \cdot 10^5.

Output Format: For each test case, print a single integer — Alice's profit if both Alice and Bob act optimally.

Note: In the first test case, Alice should buy the 22-nd item and sell it to Bob, so her profit is 21=12 - 1 = 1.

In the second test case, Alice should buy the 11-st, the 22-nd and the 33-rd item; then Bob takes the 11-st item for free and pays for the 22-nd and the 33-rd item. Alice's profit is (3+2)(1+2+1)=1(3+2) - (1+2+1) = 1. Bob could take 22-nd item for free instead; this does not change Alice's profit. Bob won't take the 33-rd item for free, since this would lead to a profit of 22.

Sample Cases

Case 1

Input

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

Output

1
1
0
7

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