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. 14.2.1 MySQL and the ACID Model

    14.2 InnoDB Concepts and Architecture InnoDB 概念和结构体系: 14.2.1 MySQL and the ACID Model 14.2.2 InnoDB ...

  2. J2EE SSH学习(二)安装Eclipse插件和第一个Eclipse项目

    (一)安装Eclipse插件 Eclipse有很多功能很强大的插件,我现在作为一个菜鸟只知道插件的功能通常都很牛叉实用或者很有趣,那么该怎么安装Eclipse插件呢? 我使用的是Eclipse 4.3 ...

  3. struts2官方演示程序总结struts2-blank

    struts-2.2.3.1-all\struts-2.2.3.1\apps\struts2-blank总结 1.Html可以访问action  ,如下:    < head  >     ...

  4. Spring的事件处理

    Spring对事件有一些支持,因为项目须要,所以近期小小研究了下究竟这个怎么能够方便的用在实际项目其中来. 说起事件这个东西,事实上就是借鉴的那个观察者模式.这里面涉及到事件源.事件监听者.事件公布者 ...

  5. VC/MFC 当鼠标移到控件上时显示提示信息

    VC/MFC 当鼠标移到控件上时显示提示信息 ToolTip是Win32中一个通用控件,MFC中为其生成了一个类CToolTipCtrl,总的说来其使用方法是较简单的,下面讲一下它的一般用法和高级用法 ...

  6. D7升级时候发现许多System函数和网络函数只有Byte版本的,需要注意

    SetLength 对于字符串,是WideChar的长度GetMem 只针对ByteMove 只针对ByteFillChar 只针对ByteWriteFile(API) 只针对Byte SetSock ...

  7. 将markdown格式转化为bootstrap风格html

    前言:这些年markdown格式的文件很流行,像github里project说明文档都是用markdown格式编写. 一方面,我们能够通过pandoc将markdown文件转换为html,这样将htm ...

  8. MySQL 通配符学习总结

    MySQL 通配符 SQL您同意使用模式匹配"_"无论单个字符相匹配,和"%"匹配随意数目字符(包含零个字符). 在 MySQL中.SQL的模式缺省是忽略大写和 ...

  9. Dom对象和JQuery对象的详细介绍及其区别

    一直搞不清Dom对象和JQuery对象之间的区别,今天好好总结下 1.dom对象(摘抄自百度百科http://baike.baidu.com/link?url=4L8bZ7kW6kE-it4F-1LU ...

  10. RSA密码系统 基于大数环境编写 密码学课程设计

    RSA密码系统的实现 1.问题描述 RSA密码系统可具体描述为:取两个大素数p和q,令n=pq,N=(p-1)(q-1),随机选择整数d,满足gcd(d,N)=1,ed=1 modN. 公开密钥:k1 ...