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;
}