Devu and his Brother
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Devu and his brother love each other a lot. As they are super geeks, they only like to play with arrays. They are given two arrays
a and b by their father. The array
a is given to Devu and
b to his brother.

As Devu is really a naughty kid, he wants the minimum value of his array
a should be at least as much as the maximum value of his brother's array
b.

Now you have to help Devu in achieving this condition. You can perform multiple operations on the arrays. In a single operation, you are allowed to decrease or increase any element of any of the arrays by 1. Note that you are allowed to apply the operation
on any index of the array multiple times.

You need to find minimum number of operations required to satisfy Devu's condition so that the brothers can play peacefully without fighting.

Input

The first line contains two space-separated integers n,
m (1 ≤ n, m ≤ 105). The second line will contain
n space-separated integers representing content of the array
a (1 ≤ ai ≤ 109). The third line will contain
m space-separated integers representing content of the array
b (1 ≤ bi ≤ 109).

Output

You need to output a single integer representing the minimum number of operations needed to satisfy Devu's condition.

Sample test(s)
Input
2 2
2 3
3 5
Output
3
Input
3 2
1 2 3
3 4
Output
4
Input
3 2
4 5 6
1 2
Output
0
Note

In example 1, you can increase
a1 by 1 and decrease
b2 by
1 and then again decrease b2 by
1. Now array a will be [3;
3] and array b will also be [3;
3]. Here minimum element of
a is at least as large as maximum element of
b. So minimum number of operations needed to satisfy Devu's condition are
3.

In example 3, you don't need to do any operation, Devu's condition is already satisfied.

贪心问题,代码不长,好看懂

AC代码例如以下:

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<iomanip>
using namespace std;
#define ll long long
#define M 100005 bool cmp(int a,int b)
{
return a>b;
} int min(int a,int b)
{
return a<b?a:b;
} int main()
{
int n,m,i;
int a[M],b[M];
cin>>n>>m;
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<m;i++)
cin>>b[i];
sort(a,a+n);
sort(b,b+m,cmp);
n=min(n,m);
ll sum=0;
for(i=0;i<n;i++)
{
if(b[i]>a[i])
sum+=(ll)(b[i]-a[i]);
}
cout<<sum<<endl;
return 0;
}

CF 439D(251D题)Devu and his Brother的更多相关文章

  1. codeforces 251 div2 D. Devu and his Brother 三分

    D. Devu and his Brother time limit per test 1 second memory limit per test 256 megabytes input stand ...

  2. Codeforces 439D Devu and his Brother 三分

    题目链接:点击打开链接 = - =曾经的三分姿势不对竟然没有被卡掉,,,太逗.. #include<iostream> #include<string> #include< ...

  3. CF 798B 渣渣题

    题目链接:http://codeforces.com/contest/798/problem/B 此题是我打河工大校赛前一晚熬夜打CF时硬肛过去的B题,今天补题时,偶然看到dalao的代码,ORZ,s ...

  4. 专题:CF图论杂题

    题目是来自HZW的博客(构造题我是各种不会...) Solved 1 / 1 A CodeForces 500A New Year Transportation Solved 1 / 1 B Code ...

  5. [ 9.29 ]CF每日一题系列—— 765B字符串规律

    Description: 遇到了ogo可以变成***如果ogo后面有go统统忽略,输出结果 Solution: 哎如果我一开始对题意的解读如上的话,就不会被整的那么麻烦了 Code: #include ...

  6. [ 9.24 ]CF每日一题系列—— 468A构造递推

    Description: 1 - n个数问你能否经过加减乘除这些运算n -1次的操作得到24 Solutrion: 一开始想暴力递推,发现n的范围太大直接否决,也否决了我的跑dfs,后来就像肯定有个递 ...

  7. [ 9.9 ]CF每日一题系列—— 259A黑白棋盘检查问题

    http://codeforces.com/problemset/problem/259/A PS9.8日做了但是忘了发博客,所以坚持3天了呦~ 终于读懂了这个题了,心累 Describe: 给你8 ...

  8. CF刷刷水题找自信 2

    CF 1114A  Got Any Grapes(葡萄)? 题目意思:给三个人分葡萄,三个人对葡萄的颜色有一些要求,问所准备的三种颜色的葡萄能否满足三人的要求. 解题意思:直接按条件判断即可. #in ...

  9. CF刷刷水题找自信1

    CF 1108A Two distinct points 题目意思:给你两个线段的起点和终点,让你给出两个不同的点,这两点分别处于两个不同的线段之中.解题思路:题目说如果存在多种可能的点,随意一组答案 ...

随机推荐

  1. nohup命令与&区别,jobs,fg,bg,Ctrl-Z、Ctrl-C、Ctrl-D

    &方式: Unix/Linux下一般想让某个程序在后台运行,很多都是使用 & 在程序结尾来让程序自动运行.比如我们要运行mysql在后台:          /usr/local/my ...

  2. H2内存数据库 支持存储到文件

     准备工作 1.下载JDK(本人下载的版本号为JDK1.7).环境变量设置JAVA_HOME.设置PATH(%JAVA_HOME%\bin%). 2.下载并解压:h2-2014-07-13.zip ...

  3. VC添加背景图片 的一种方法

    .如果程序是新建的对话框,要给其添加背景图片的步骤: 1)加入消息函数:afx_msg void OnPaint(); 2)BEGIN_MESSAGE_MAP(QueryDlg, CDialog) O ...

  4. 页面提交错误,页面间参数传递java.lang.NumberFormatException: null

    多次出现这样的错误,在点击一个按钮触发提交整个页面的事件时,总是报错,不止一次出现这样的错误了. 出现这种问题的分析: 1 我们从这个问题的本身来看,java.lang.NumberFormatExc ...

  5. cct,web技术

    基本信息 全国计算机等级考试二级教程——Web程序设计(2016年版)作    者:教育部考试中心 编出 版 社:高等教育出版社出版时间:20115-12-1 ISBN:9787040442991版 ...

  6. csdn发博文验证码缺陷

    csdn验证码的长处: 一,差点儿没有浪费人脑人力,却要花去机器人非常多cpu csdn发博文验证码却有非常大缺陷: 一.验证码的内容终于结果是简单的数字,能够穷举尽的,也就是说不怕被封号的话全然能够 ...

  7. 利用WinDbg找出程序崩溃的代码行号

    之前碰到论坛里有几个好友,说程序不时的崩溃,什么xxoo不能read的! 如果光要是这个内存地址,估计你会疯掉~~ 所以分享一下基本的调试技巧,需要准备的工具有WinDbg + VC6.0, 下面是自 ...

  8. 24位和8位BMP图片保存纯C代码

    BMP图片大家都知道,可以通过查看BMP图片结构使用纯C就可以打开,编辑,处理,保存图片.非常方便使用. 具体BMP结构可以参考:wingdi.h头文件.今天主要在进行删减代码,需要把多余的代码删除, ...

  9. [Android阅读代码]android-async-http源码学习一

    android-async-http 下载地址 一个比较常用的Http请求库,基于org.apache.http对http操作进行封装. 特点: 1.每一个HTTP请求发生在UI线程之外,Client ...

  10. C#之自己定义的implicit和explicit转换

    在类型转换时常会遇到隐式转换和显式转换.那我们自己定义的类型要怎样去定义隐式转换和显式转换?我们来看一段代码 public class Rational { private Int32 _inner_ ...