CF1399B.Gifts Fixing
入门
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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.
你有 n 件礼物,希望将它们全部分给孩子们。当然,你不想让任何人感到被冒犯,因此所有礼物之间必须完全相同。第 i 件礼物包含 ai 颗糖果和 bi 个橙子。
在一次操作中,你可以选择某一件礼物 1≤i≤n,并执行以下三种操作之一:
- 从该礼物中恰好吃掉一颗糖果(即令 ai 减少 1);
- 从该礼物中恰好吃掉一个橙子(即令 bi 减少 1);
- 从该礼物中恰好吃掉一颗糖果和一个橙子(即同时令 ai 和 bi 各减少 1)。
当然,你不能吃掉不存在的糖果或橙子(即 ai 和 bi 均不能变为负数)。
如前所述,所有礼物必须变得完全相同。这意味着经过若干次操作后,需满足如下两个条件:a1=a2=⋯=an 且 b1=b2=⋯=bn(注意:不要求对任意 i 都有 ai=bi)。
你的任务是求出使所有给定礼物变得相同的最少操作次数。
你需要回答 t 个相互独立的测试用例。
输入格式
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.
输入的第一行包含一个整数 t(1≤t≤1000),表示测试用例的数量。接下来是 t 个测试用例。
每个测试用例的第一行包含一个整数 n(1≤n≤50),表示礼物的数量。
每个测试用例的第二行包含 n 个整数 a1,a2,…,an(1≤ai≤109),其中 ai 表示第 i 个礼物中的糖果数量。
每个测试用例的第三行包含 n 个整数 b1,b2,…,bn(1≤bi≤109),其中 bi 表示第 i 个礼物中的橘子数量。
输出格式
For each test case, print one integer: the minimum number of moves required to equalize all the given gifts.
对于每个测试用例,输出一个整数:使所有给定礼物相等所需的最少移动次数。
输入输出样例
输入#1
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
输出#1
6 16 0 4999999995 7
说明/提示
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].
在示例的第一个测试用例中,我们可以执行以下操作序列:
- 选择第一个礼物并从中吃掉一个橘子,此时 a=[3,5,6],b=[2,2,3];
- 选择第二个礼物并从中吃掉一颗糖果,此时 a=[3,4,6],b=[2,2,3];
- 选择第二个礼物并从中吃掉一颗糖果,此时 a=[3,3,6],b=[2,2,3];
- 选择第三个礼物并从中各吃掉一颗糖果和一个橘子,此时 a=[3,3,5],b=[2,2,2];
- 选择第三个礼物并从中吃掉一颗糖果,此时 a=[3,3,4],b=[2,2,2];
- 选择第三个礼物并从中吃掉一颗糖果,此时 a=[3,3,3],b=[2,2,2]。
输入解题思路,AI测评打分。不知道怎么写?