Codeforces Round #251 (Div. 2) D. Devu and his Brother

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

Input

The first line contains two space-separated integers nm (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



三分A数组的最小值,比赛的时候被系统搞掉了,把最后三分出的最小值l,取[l-5,l+5]的最小操作数,就过了。。。。。。。。。

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

#define rep(i,s,t) for(int i=s;i=t;i--)
#define clr(a,v) memset(a,v,sizeof a)
#define min(a,b) (a'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


你可能感兴趣的:(ACM)