A. The Party and Sweets
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
n
boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1≤i≤n the minimal number of sweets, which i-th boy presented to some girl is equal to bi and for all 1≤j≤m the maximal number of sweets, which j-th girl received from some boy is equal to gj
.
More formally, let ai,j
be the number of sweets which the i-th boy give to the j-th girl. Then bi is equal exactly to the minimum among values ai,1,ai,2,…,ai,m and gj is equal exactly to the maximum among values b1,j,b2,j,…,bn,j
.
You are interested in the minimum total number of sweets that boys could present, so you need to minimize the sum of ai,j
for all (i,j) such that 1≤i≤n and 1≤j≤m. You are given the numbers b1,…,bn and g1,…,gm
, determine this number.
Input
The first line contains two integers n
and m, separated with space — the number of boys and girls, respectively (2≤n,m≤100000). The second line contains n integers b1,…,bn, separated by spaces — bi is equal to the minimal number of sweets, which i-th boy presented to some girl (0≤bi≤108). The third line contains m integers g1,…,gm, separated by spaces — gj is equal to the maximal number of sweets, which j-th girl received from some boy (0≤gj≤108
).
Output
If the described situation is impossible, print −1
. In another case, print the minimal total number of sweets, which boys could have presented and all conditions could have satisfied.
Examples
Input
Copy
3 2 1 2 1 3 4
Output
Copy
12
Input
Copy
2 2 0 1 1 0
Output
Copy
-1
Input
Copy
2 3 1 0 1 1 2
Output
Copy
4
Note
In the first test, the minimal total number of sweets, which boys could have presented is equal to 12
. This can be possible, for example, if the first boy presented 1 and 4 sweets, the second boy presented 3 and 2 sweets and the third boy presented 1 and 1 sweets for the first and the second girl, respectively. It's easy to see, that all conditions are satisfied and the total number of sweets is equal to 12
.
In the second test, the boys couldn't have presented sweets in such way, that all statements satisfied.
In the third test, the minimal total number of sweets, which boys could have presented is equal to 4
. This can be possible, for example, if the first boy presented 1, 1, 2 sweets for the first, second, third girl, respectively and the second boy didn't present sweets for each girl. It's easy to see, that all conditions are satisfied and the total number of sweets is equal to 4.
题意:
一共有n个男孩,m个女孩,然后每一个男孩都要给女孩红包,给出每一个男孩给出最小的红包,和给出每一个女孩收到的最大的红包,让你求出怎样发红包使得红包总和最小。
思路:
1.我们需要先判断不符合现实的情况,就是在男孩中的最小红包的最大应该小于女孩最大红包的最小,因为每个男孩都要给女孩红包,如果存在一个男孩的最小红包都大于这个女孩收到的最大红包是不可能的,就像王思聪给你100万,但你说不要这样。
2.很明显直接贪心,让最小红包最大的派给女孩,但是你最小的红包还是要派出去,所以最多派m-1个女孩,但是如果她的最大红包恰好是你的最小,也能派。
code:
#include<iostream>
#include<deque>
#include<memory.h>
#include<stdio.h>
#include<map>
#include<string.h>
#include<algorithm>
#include<vector>
#include<math.h>
#include<stack>
#include<queue>
#include<set>
using namespace std;
typedef long long int ll;
ll n,m,a[100100],b[100100];
ll ans=0;
bool cmp(int a,int b)
{
return a>b;
}
int main()
{
int q1=1,q2=1,flag=0,temp=0;
cin>>n>>m;
for(int i=1;i<=n;i++)
cin>>a[i];
for(int i=1;i<=m;i++)
cin>>b[i];
sort(a+1,a+1+n,cmp);
sort(b+1,b+1+m,cmp);
if(b[m]<a[1])
flag=1;
if(flag)
{
cout<<-1<<endl;
}
else
{
while(q1<=n)
{
if((temp<m-1||a[q1]==b[q2])&&q2<=m)
ans+=b[q2],temp++,q2++;
else
ans+=(m-temp)*a[q1],q1++,temp=0;
}
cout<<ans<<endl;
}
return 0;
}