# 【cf439D】Devu and his Brother

## 描述 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.

## 输入格式 InputFormat

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

## 输出格式 OutputFormat

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

3 2
1 2 3
3 4

4

Codeforces 439D

## 代码 Code

``````#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int a[100005],b[100005];
int i,j,t,n,m,l,r,k,z,y,x;
long long ans;
int main()
{
scanf("%d%d",&n,&m);
for (i=1;i<=n;i++) scanf("%d",&a[i]);
for (i=1;i<=m;i++) scanf("%d",&b[i]);
sort(a+1,a+n+1);sort(b,b+m+1);
ans=0;
for (i=1;i<=n && i<=m;i++) if (a[i]<b[m-i+1])
{
ans+=b[m-i+1]-a[i];
}
else
{
break;
}
printf("%I64d\n",ans);
return 0;
}``````