#P15260. [USACO26JAN2] Balancing the Barns G

[USACO26JAN2] Balancing the Barns G

Description

Farmer John has NN (1N51041\le N\le 5\cdot 10^4) barns arranged along a road. The ii-th barn contains aia_i bales of hay and bib_i bags of feed (0ai,bi109(0\le a_i,b_i\le 10^9).

Bessie has been complaining about the inequality between barns. She defines the "imbalance" of the farm as the difference between the maximum hay in any barn and the minimum feed in any barn. Formally, the imbalance is max(a)min(b)\max(a) - \min(b).

To address Bessie's concerns, Farmer John can perform exactly KK (1K10181\le K\le 10^{18}) transfers. In each transfer, he selects a barn ii, sells one of its haybales, and buys it a new bag of feed for the same barn. Note that there can be negative amounts in his farm (he is not afraid of debt). Formally, KK times, you choose an index i[1,N]i\in [1,N], decrement aia_i, and increment bib_i.

Help Farmer John determine the minimum possible imbalance after performing exactly KK transfers.

Input Format

The first line contains TT (1T1031 \leq T \leq 10^3), the number of independent test cases.

The first line of each test case contains NN and KK.

The following line contains a1aNa_1\dots a_N.

The following line contains b1bNb_1\dots b_N.

The sum of NN over all test cases is at most 51045 \cdot 10^4.

Output Format

For each test case, output a single integer, the minimum possible value of max(a)min(b)\max(a) - \min(b) after performing KK operations.

4
1 10
5
3
2 6
100 96
0 4
3 3
1 1 2
0 0 1
3 3
1 2 2
0 1 1
-18
90
0
0

Hint

In the first test case, Farmer John can transfer 1010 haybales from barn 11 into bags of feed. This leaves a=[5]a = [-5] and b=[13]b = [13]. The imbalance is max(a)min(b)=513=18\max(a) - \min(b) = -5 - 13 = -18.

In the second test case, Farmer john can transfer 55 haybales from barn 11 and 11 haybale from barn 22. This leaves a=[95,95]a = [95, 95] and b=[5,5]b = [5, 5]. The imbalance is 955=9095 - 5 = 90. This is the minimum imbalance Farmer John can achieve.

SCORING:

  • Inputs 2-4: K500K\le 500, sum of NN over all testcases is 500\le 500
  • Inputs 5-8: Sum of NN over all testcases is 500\le 500
  • Inputs 9-13: No additional constraints.