#P15260. [USACO26JAN2] Balancing the Barns G
[USACO26JAN2] Balancing the Barns G
Description
Farmer John has () barns arranged along a road. The -th barn contains bales of hay and bags of feed ).
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 .
To address Bessie's concerns, Farmer John can perform exactly () transfers. In each transfer, he selects a barn , 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, times, you choose an index , decrement , and increment .
Help Farmer John determine the minimum possible imbalance after performing exactly transfers.
Input Format
The first line contains (), the number of independent test cases.
The first line of each test case contains and .
The following line contains .
The following line contains .
The sum of over all test cases is at most .
Output Format
For each test case, output a single integer, the minimum possible value of after performing 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 haybales from barn into bags of feed. This leaves and . The imbalance is .
In the second test case, Farmer john can transfer haybales from barn and haybale from barn . This leaves and . The imbalance is . This is the minimum imbalance Farmer John can achieve.
SCORING:
- Inputs 2-4: , sum of over all testcases is
- Inputs 5-8: Sum of over all testcases is
- Inputs 9-13: No additional constraints.
京公网安备 11011102002149号