原题链接:http://codeforces.com/problemset/problem/1399/B

Gifts Fixing

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].

题目大意

ttt组询问,每次给出数列长度nnn以及两个长度为nnn的数列{ai}\{a_i\}{ai​}和{bi}\{b_i\}{bi​}。

有三种操作:ai−1,bi−1a_i-1,\ b_i-1ai​−1, bi​−1以及ai,bia_i,b_iai​,bi​同时−1-1−1。

问最少多少步以后可以让两个数列变成常数数列。

题解

很显然的贪心,要让整个数列变成常数列,最优的做法就是把每个数都减成所在数列的最小值。

同时,因为有同时减一的操作,同一位置上的数需要花费的总步数为与最小值的差较大的数与最小值的差。

代码

含泪复习三目运算。

记得开longlonglong\ longlong long。

#include<bits/stdc++.h>
using namespace std;
int t,a,minc,mino;
long long ans;
int can[55],ora[55];
void in()
{ans=0,minc=1e9+5,mino=1e9+5;scanf("%d",&a);for(int i=1;i<=a;++i){scanf("%d",&can[i]);minc=can[i]<minc?can[i]:minc;}for(int i=1;i<=a;++i){scanf("%d",&ora[i]);mino=ora[i]<mino?ora[i]:mino;}
}
void ac()
{for(int i=1;i<=a;++i)ans+=max(can[i]-minc,ora[i]-mino);printf("%lld\n",ans);
}
int main()
{scanf("%d",&t);for(int i=1;i<=t;++i){in(),ac();}
}

CF1399B Gifts Fixing相关推荐

  1. Codeforces Round #661 (Div. 3)题解

    目录 A.Remove Smallest(模拟) B.Gifts Fixing(模拟) C.Boats Competition(暴力枚举) D.Binary String To Subsequence ...

  2. Codeforces Round #661 (Div. 3)

    A - Remove Smallest 排个序,如果相邻的数大于一就不满足题意 #define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0) ...

  3. 口令红包 java,[1.7.2]Gifts — 我的世界服务器在线礼物和登陆奖励插件

    ==========*介绍*========= 是不是经常有服里的玩家闹着发福利呢,是不是用give指令用着蛋疼呢 这个插件帮助你--简洁的礼物派发,简单的每日登陆奖励! ==========*用法* ...

  4. Codeforces Round #726 (Div. 2) F. Figure Fixing 二分图 + 思维

    传送门 文章目录 题意: 思路 题意: 给你一张nnn个点mmm条边的图,每个点都有一个当前值aia_iai​,目标值bib_ibi​,每次可以选择一条边(i,j)(i,j)(i,j),将ai,aja ...

  5. Bailian4110 圣诞老人的礼物-Santa Clau’s Gifts【背包+贪心】

    4110:圣诞老人的礼物-Santa Clau's Gifts 总时间限制: 1000ms 内存限制: 65536kB 描述 圣诞节来临了,在城市A中圣诞老人准备分发糖果,现在有多箱不同的糖果,每箱糖 ...

  6. 百练-4110 圣诞老人的礼物-Santa Clau’s Gifts

    4110:圣诞老人的礼物-Santa Clau's Gifts 查看 提交 统计 提示 提问 总时间限制:  1000ms 内存限制:  65536kB 描述 圣诞节来临了,在城市A中圣诞老人准备分发 ...

  7. 4110:圣诞老人的礼物-Santa Clau’s Gifts(java)

    4110:圣诞老人的礼物-Santa Clau's Gifts(java) 描述 圣诞节来临了,在城市A中圣诞老人准备分发糖果,现在有多箱不同的糖果,每箱糖果有自己的价值和重量,每箱糖果都可以拆分成任 ...

  8. 【百练】圣诞老人的礼物-Santa Clau’s Gifts(c语言)

    题目描述 圣诞节来临了,在城市A中圣诞老人准备分发糖果,现在有多箱不同的糖果,每箱糖果有自己的价值和重量,每箱糖果都可以拆分成任意散装组合带走.圣诞老人的驯鹿最多只能承受一定重量的糖果,请问圣诞老人最 ...

  9. Fixing(D:) Stage 2: 77%(4892798 of 631772); Total: 65%;ETA: 0:01:21.的解决方案

    本文目录 一.背景描述 二.问题原因 三.解决方案 1.BIOS设置跳过开机自检 2.注册表禁止系统开机自检:进入Win10注册表编辑器中 3.以管理员身份实现禁止磁盘自检和实现命令行系统扫描,检查问 ...

  10. Codeforces 1283C Friends and Gifts

    题目链接: Codeforces 1283C Friends and Gifts 思路: 将还没有分配的人和还没有被分配的人分别存进两个数组,挨个遍历,如果相同位置是同一个人,就和下一个交换 代码: ...

最新文章

  1. 微服务化的十个设计要点
  2. 在华为云服务器的Linux系统中搭建开发环境
  3. Java:双向链表反转实现
  4. 谷歌爬虫ua_Python爬虫,实战爬取腾讯视频评论!
  5. 安装pattern出错mysql_config not found
  6. Altium Designer(十):极坐标
  7. EasyUI----增删改查
  8. Python中turtle模块画图
  9. Builder模式的误区:将复杂对象的构建进行封装,就是Builder模式了吗?
  10. 算法:Evaluate Reverse Polish Notation(逆波兰表达式求值)
  11. 使用Java快速开发一个新闻爬虫项目
  12. (Web前端)后台管理系统框架收集
  13. 【软件工具】之 TotalCommander
  14. Xmarks不会关闭了,太好了!
  15. 3分钟tips:高斯分布和高斯积分的关系
  16. 对d3d9里面的函数挂钩实现透视
  17. 2013年7月美国Boston之旅
  18. 【计算机毕业设计】33.快递取件预约系统源码
  19. 新手必学的20个人像摄影构图法
  20. 麦克利兰的成就动机理论(转载)

热门文章

  1. jupyter notebook 修改主题、字体、字号等
  2. 多线程打印ABCD顺序(带有线程池实现)
  3. python学习笔记:操作Excle
  4. C++实现建立和一二进制树的三个递归遍历
  5. bcp sqlcmd bulkinsert在unicode问题,Unexpected EOF encountered in BCP data-file
  6. 查询字符串(性能对比): Array Vs HashMap
  7. SpringMVC中的九大组件
  8. 解决jpa和hibernate的json序列化死循环问题
  9. Spring扩展之BeanFactoryPostProcessor接口
  10. SpringAop源码一:通知方法优先级