You have n gifts and you want to give all of them to children. Of course, you don’t want to offend anyone, so all gifts should be equal between each other. The i-th gift consists of ai candies and bi oranges.
During one move, you can choose some gift 1≤i≤n and do one of the following operations:
eat exactly one candy from this gift (decrease ai by one);
eat exactly one orange from this gift (decrease bi by one);
eat exactly one candy and exactly one orange from this gift (decrease both ai and bi by one).
Of course, you can not eat a candy or orange if it’s not present in the gift (so neither ai nor bi can become less than zero).
As said above, all gifts should be equal. This means that after some sequence of moves the following two conditions should be satisfied: a1=a2=⋯=an and b1=b2=⋯=bn (and ai equals bi is not necessary).
Your task is to find the minimum number of moves required to equalize all the given gifts.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1≤t≤1000) — the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1≤n≤50) — the number of gifts. The second line of the test case contains n integers a1,a2,…,an (1≤ai≤109), where ai is the number of candies in the i-th gift. The third line of the test case contains n integers b1,b2,…,bn (1≤bi≤109), where bi is the number of oranges in the i-th gift.
Output
For each test case, print one integer: the minimum number of moves required to equalize all the given gifts.
Example
Input
5
3
3 5 6
3 2 3
5
1 2 3 4 5
5 4 3 2 1
3
1 1 1
2 2 2
6
1 1000000000 1000000000 1000000000 1000000000 1000000000
1 1 1 1 1 1
3
10 12 8
7 5 4
Output
6
16
0
4999999995
7
Note
In the first test case of the example, we can perform the following sequence of moves:
choose the first gift and eat one orange from it, so a=[3,5,6] and b=[2,2,3];
choose the second gift and eat one candy from it, so a=[3,4,6] and b=[2,2,3];
choose the second gift and eat one candy from it, so a=[3,3,6] and b=[2,2,3];
choose the third gift and eat one candy and one orange from it, so a=[3,3,5] and b=[2,2,2];
choose the third gift and eat one candy from it, so a=[3,3,4] and b=[2,2,2];
choose the third gift and eat one candy from it, so a=[3,3,3] and b=[2,2,2].
3
3 5 6
3 2 3
6
部分样例模拟如下:
礼物1 2 3
糖果3 5 6 最终每个礼物糖的数量都是3
桔子3 2 3 最终每个礼物桔子的数量都是2
位置 1 2 3
糖糖 3 5 6 最终所有位置糖的数量都是3
差值 3-3=0 5-3=2 6-3=3
桔子 3 2 3
差值 3-2=1 2-2=0 3-2=1 最终所有位置桔子的数量都是2
操作次数max(0,1)=1 max(2,0)=2 max(3,1)=3 总操作次数1+2+3=6
题意:均分礼物,提供n个礼物,每个礼物由糖果a[i],桔子b[i]组成,要求最后分配到每个人手上的
糖果数量相等a[1]=a[2]=a[3]=…a[n-1]=a[n];
桔子数量也相等b[1]=b[2]=b[3]…=b[n-1]=b[n]。
每个礼物有三种操作:
1.丢掉一个糖。——a[i]-1
2.丢掉一个橙子。——b[i]-1
3.丢掉一个糖和一个橙子。——a[i]-1&&b[i]-1
因为操作只有减法没有加法,所以最后每个礼物中糖果的数量的值一定是所有a(i)中的最小值mina,b(i)同理。所以对于每个物品,需要操作的次数是max(a(i)-mina,b(i)-minb),累加就是答案。
#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll a[55],b[55];
int t,n;
ll mina,minb;
int main()
{
cin>>t;
while(t--)
{
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
for(int i=1;i<=n;i++)
cin>>b[i];
mina=a[1],minb=b[1];
for(int i=1;i<=n;i++)
{
mina=min(mina,a[i]);
minb=min(minb,b[i]);
}
ll ans=0;
for(int i=1;i<=n;i++)
{
ans+=max(a[i]-mina,b[i]-minb);
}
cout<<ans<<endl;
}
return 0;
}