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 bby 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.
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).
You need to output a single integer representing the minimum number of operations needed to satisfy Devu's condition.
2 2 2 3 3 5
3
3 2 1 2 3 3 4
4
3 2 4 5 6 1 2
0
三分A数组的最小值,比赛的时候被系统搞掉了,把最后三分出的最小值l,取[l-5,l+5]的最小操作数,就过了。。。。。。。。。
#include <stdio.h>
#include <vector>
#include <string>
#include <queue>
#include <string.h>
#include <algorithm>
using namespace std;
#define rep(i,s,t) for(int i=s;i<t;i++)
#define red(i,s,t) for(int i=s-1;i>=t;i--)
#define clr(a,v) memset(a,v,sizeof a)
#define min(a,b) (a<b?a:b)
inline int input(){
int ret=0;bool isN=0;
char c=getchar();
while(c<'0' || c>'9'){
if(c=='-') isN=1;
c=getchar();
}
while(c>='0'&&c<='9'){
ret=ret*10+c-'0';
c=getchar();
}
return isN?-ret:ret;
}
int n,m;
int a[100005],b[100005];
inline void In(){
n=input(),m=input();
rep(i,0,n) a[i]=input();
rep(j,0,m) b[j]=input();
sort(a,a+n);
sort(b,b+m);
}
inline long long get(int x){
long long ans=0;
rep(i,0,n){
if(x>a[i]) ans+=(long long)(x-a[i]);
else break;
}
red(i,m,0){
if(b[i]>x) ans+=(long long)(b[i]-x);
else break;
}
return ans;
}
inline void Work(){
if(a[0]>=b[m-1]){
puts("0");
}
else{
int l=1,r=2000000000,cnt=0,mid,mmid;
while(cnt<=100){
mid=l+(r-l)/2;
mmid=mid+(r-mid)/2;
long long res=get(mid);
long long rres=get(mmid);
if(res<rres) r=mmid;
else l=mid;
cnt++;
}
long long ans=get(l);
cnt=1;
while(cnt<=5){
ans=min(ans,get(l+cnt));cnt++;
}
cnt=1;
while(cnt<=5){
ans=min(ans,get(l-cnt));cnt++;
}
printf("%I64d\n",ans);
}
}
int main(){
In();
Work();
return 0;
}