Codeforces Round #559 (Div. 2) C. The Party and Sweets

C. The Party and Sweets

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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

3 2
1 2 1
3 4

output

12

input

2 2
0 1
1 0

output

-1

input

2 3
1 0
1 1 2

output

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.

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<iomanip>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<vector>
#include<queue>
#include<set>
#include<sstream>
#define ll long long
#define mes(x,y) memset(x,y,sizeof(x))
using namespace std;
ll a[100020],b[100020];
bool cmp(ll s,ll ss){
	return s>ss;
}
int main(){
	ll x,y,i,j,sum;
	 while(cin>>x>>y){
	 	sum=0;
	 	for(i=0;i<x;i++){
	 		cin>>a[i];sum+=a[i]*y; 
		 }
	 	for(j=0;j<y;j++)cin>>b[j];
	 	sort(a,a+x,cmp);sort(b,b+y);
	 	if(a[0]>b[0]){
	 		cout<<"-1"<<endl;continue;
		 }
		 else if(a[0]==b[0])
		 for(i=0;i<y;i++){
		 	sum+=b[i]-a[0];
		 }
		 else{
		 	sum+=b[0]-a[1];
		 	for(i=1;i<y;i++){
		 		sum+=b[i]-a[0];
			 }
		 }
		 cout<<sum<<endl;
	 }
}
发布了148 篇原创文章 · 获赞 7 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_44417851/article/details/90485635
今日推荐