logo AlgoBeat OnlineJudge
登录 注册

#215275. [USACO26JAN2] Balancing the Barns G

内存限制:256 MiB 时间限制:2000 ms 标准输入输出
题目类型:VJudge(洛谷) 评测方式:VJudge
上传者: 匿名

题目描述

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.

输入格式

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 .

输出格式

For each test case, output a single integer, the minimum possible value of after performing operations.

样例

样例输入 1

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

样例输出 1

-18
90
0
0

数据范围与提示

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.