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;
	 }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GUESSERR

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值