nn boys and mm girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 11 to nn and all girls are numbered with integers from 11 to mm. For all 1≤i≤n1≤i≤n the minimal number of sweets, which ii-th boy presented to some girl is equal to bibi and for all 1≤j≤m1≤j≤m the maximal number of sweets, which jj-th girl received from some boy is equal to gjgj.
More formally, let ai,jai,j be the number of sweets which the ii-th boy give to the jj-th girl. Then bibi is equal exactly to the minimum among values ai,1,ai,2,…,ai,mai,1,ai,2,…,ai,m and gjgj is equal exactly to the maximum among values b1,j,b2,j,…,bn,jb1,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,jai,j for all (i,j)(i,j)such that 1≤i≤n1≤i≤n and 1≤j≤m1≤j≤m. You are given the numbers b1,…,bnb1,…,bn and g1,…,gmg1,…,gm, determine this number.
Input
The first line contains two integers nn and mm, separated with space — the number of boys and girls, respectively (2≤n,m≤1000002≤n,m≤100000). The second line contains nn integers b1,…,bnb1,…,bn, separated by spaces — bibi is equal to the minimal number of sweets, which ii-th boy presented to some girl (0≤bi≤1080≤bi≤108). The third line contains mm integers g1,…,gmg1,…,gm, separated by spaces — gjgj is equal to the maximal number of sweets, which jj-th girl received from some boy (0≤gj≤1080≤gj≤108).
Output
If the described situation is impossible, print −1−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 1212. This can be possible, for example, if the first boy presented 11 and 44 sweets, the second boy presented 33 and 22 sweets and the third boy presented 11 and 11 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 1212.
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 44. This can be possible, for example, if the first boy presented 11, 11, 22 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 44.
题意:n个男孩送m个女孩糖,第一行给出n,m。第二行n个数,第i个男孩最少给每个女孩bi个糖果,第三行m个数,第i个女孩最多收ai个糖果,每个男孩对于每个女孩都要送一遍糖果,每个男孩最少送出一次bi。每个女孩最少得到一次ai。
water。
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define ll long long
using namespace std;
ll A[100010],B[100010];
ll er(ll l,ll r,ll x)
{
while(l<=r)
{
ll mid=(l+r)>>1;
if(B[mid]>=x) r=mid-1;
else l=mid+1;
}
return l;
}
int main()
{
ll n,m;
scanf("%I64d%I64d",&n,&m);
for(ll i=1;i<=n;i++)
scanf("%I64d",&A[i]);
for(ll i=1;i<=m;i++)
scanf("%I64d",&B[i]);
sort(A+1,A+1+n);
sort(B+1,B+1+m);
ll l=1,r=m,ans=0,flag=1;
if(A[n]>B[1]) flag=0;
for(ll i=n;i>0;i--)
{
if(r>=l)
{
ll d=er(l,r,A[i]);
if(r-d+1==m&&B[d]!=A[i]) d++;
for(ll j=d;j<=r;j++)
ans+=B[j];
ans+=(m-(r-d+1))*A[i];
r=d-1;
}
else
{
ans+=m*A[i];
}
}
if(r>=1) flag=0;
if(flag)
printf("%I64d\n",ans);
else
printf("-1\n");
}