multi-

G - G
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

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 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 Input

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

Hint

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 [33] and array bwill also be [33]. 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.


下面的方法主要还是取公共部分去找结果Orz...

#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;

int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    int i,j;
    int a[n],b[m],c[m+n];
    for(i=0;i<n;i++) {scanf("%d",&a[i]);c[i]=a[i];}
    for(j=0;j<m;j++) {scanf("%d",&b[j]);c[n+j]=b[j];}
    sort(c,c+m+n);
    int t=c[m];
    long long ans=0;
    for(i=0;i<n;i++)
    {
        if(a[i]<t)
        {
            ans+=t-a[i];

        }
    }
            for(j=0;j<m;j++)
    {
        if(b[j]>t)
        {
            ans+=b[j]-t;
        }
    }
    printf("%I64d\n",ans);
    return 0;
}

老板的三分~~~~

 /**
 * @author neko01
 */
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define clr(a) memset(a,0,sizeof a)
#define clr1(a) memset(a,-1,sizeof a)
#define dbg(a) printf("%d\n",a)
typedef pair<int,int> pp;
#define INF 0x7fffffff
const LL inf=(((LL)1)<<61)+5;
LL a[100005];
LL b[100005];
int n,m;
LL gao(LL mid)
{
    LL gao1=0,gao2=0,ans=0;
    for(int i=0;i<n;i++)
    {
        if(a[i]<mid)
            gao1+=(mid-a[i]);
    }
    for(int i=0;i<m;i++)
    {
        if(b[i]>mid)
            gao2+=(b[i]-mid);
    }
    ans=gao1+gao2;
    return ans;
}
int main()
{
    scanf("%d%d",&n,&m);
    LL mmin=INF,mmax=0;
    for(int i=0;i<n;i++)
    {
        scanf("%I64d",&a[i]);
        mmin=min(mmin,a[i]);
    }
    for(int i=0;i<m;i++)
    {
        scanf("%I64d",&b[i]);
        mmax=max(mmax,b[i]);
    }
    if(mmax<mmin)
    {
        printf("0\n");
        return 0;
    }
    LL l=0,r=INF;
    LL ans=inf;

    while(l<=r)
    {
        LL mid=(l+r)>>1;
        LL midmid=(mid+r)>>1;
        LL gao1=gao(mid);
        LL gao2=gao(midmid);
        //printf("%d %d\n",gao1,gao2);
        if(gao1<gao2)
            r=midmid-1,ans=min(ans,gao1);
        else
            l=mid+1,ans=min(ans,gao2);
    }
    printf("%I64d\n",ans);
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值