codeforces-1399B-Gifts Fixing

B. Gifts Fixing

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You have nn 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 ii-th gift consists of aiai candies and bibi oranges.

During one move, you can choose some gift 1≤i≤n1≤i≤n and do one of the following operations:

  • eat exactly one candy from this gift (decrease aiai by one);
  • eat exactly one orange from this gift (decrease bibi by one);
  • eat exactly one candy and exactly one orange from this gift (decrease both aiai and bibi by one).

Of course, you can not eat a candy or orange if it's not present in the gift (so neither aiai nor bibi 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=⋯=ana1=a2=⋯=an and b1=b2=⋯=bnb1=b2=⋯=bn (and aiai equals bibi is not necessary).

Your task is to find the minimum number of moves required to equalize all the given gifts.

You have to answer tt independent test cases.

Input

The first line of the input contains one integer tt (1≤t≤10001≤t≤1000) — the number of test cases. Then tt test cases follow.

The first line of the test case contains one integer nn (1≤n≤501≤n≤50) — the number of gifts. The second line of the test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109), where aiai is the number of candies in the ii-th gift. The third line of the test case contains nn integers b1,b2,…,bnb1,b2,…,bn (1≤bi≤1091≤bi≤109), where bibi is the number of oranges in the ii-th gift.

Output

For each test case, print one integer: the minimum number of moves required to equalize all the given gifts.

Example

input

Copy

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

Copy

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]a=[3,5,6] and b=[2,2,3]b=[2,2,3];
  • choose the second gift and eat one candy from it, so a=[3,4,6]a=[3,4,6] and b=[2,2,3]b=[2,2,3];
  • choose the second gift and eat one candy from it, so a=[3,3,6]a=[3,3,6] and b=[2,2,3]b=[2,2,3];
  • choose the third gift and eat one candy and one orange from it, so a=[3,3,5]a=[3,3,5] and b=[2,2,2]b=[2,2,2];
  • choose the third gift and eat one candy from it, so a=[3,3,4]a=[3,3,4] and b=[2,2,2]b=[2,2,2];
  • choose the third gift and eat one candy from it, so a=[3,3,3]a=[3,3,3] and b=[2,2,2]b=[2,2,2].

题意:要分糖和橙子给小孩子,每个小孩子拿到的糖和橙子要一样多。现在有很多份糖和橙子,每份的糖和橙子的数量不确定,要用最少的操作次数使得每个孩子得到的糖和橙子的数量一样。

有三种操作:

1.丢掉一个糖。

2.丢掉一个橙子。

3.丢掉一个糖和一个橙子。

问最少可以多少次可以达到每个孩子得到的糖和橙子的数量一样。

思路:先求出每个孩子可以得到多少,即糖和橙子的最小值。然后取当前份的糖和橙子与糖和橙子的最小值的差的最小值做第3种操作,也就是同时减到其中一个不能减为止。然后剩下多出来的部分单独减。注意答案要用long long。

ac代码:

#include<iostream>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<math.h>
#define MAXN 1000000005
using namespace std;
bool cmp(int a,int b){
	return a<b;
}
int main(){
	int t;
	cin>>t;
	while(t--){
		int a[55];
		int b[55];
		int n;
		int mina=MAXN;
		int minb=MAXN;
		cin>>n;
		for(int i=0;i<n;i++){
			cin>>a[i];
			if(a[i]<mina){
				mina=a[i];
			}
		}
		for(int i=0;i<n;i++){
			cin>>b[i];
			if(b[i]<minb){
				minb=b[i];
			}
		}
		//计算需要多少次 
		int difa;
		int difb;
		long long ans=0;
		for(int i=0;i<n;i++){
			difa=a[i]-mina;
			difb=b[i]-minb;
			ans+=min(difa,difb);
			ans+=abs(difa-difb);
		}
		cout<<ans<<endl;
	}
} 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值