CF w1d1 C. The Party and Sweets

Posted liangyc1021

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF w1d1 C. The Party and Sweets相关的知识,希望对你有一定的参考价值。

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

#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
ll n,m,b[100005],g[100005],tmp,tot;
ll cmp(ll x,ll y)
{
	return x>y;
}
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>b[i];
		tot+=b[i]*m; 
	}
	for(int i=1;i<=m;i++)cin>>g[i];
	sort(b+1,b+1+n,cmp);
	sort(g+1,g+1+m,cmp);//大到小排序 
	if(b[1]>g[m]){
		cout<<-1;
		return 0;
	}
	if(b[1]==g[m]){//男生中能发最多的等于女生中收的最少的 
		for(int i=1;i<=m;i++)tot+=g[i]-b[1];//只能由他把剩下的补上 
	}
	if(b[1]<g[m]){//能发最多的那个男生不是给的收的最少的 
		tot+=g[m]-b[2];
		for(int i=1;i<m;i++)tot+=g[i]-b[1];
	} 
	cout<<tot;
	return 0;
}

以上是关于CF w1d1 C. The Party and Sweets的主要内容,如果未能解决你的问题,请参考以下文章

CF741C Arpa’s overnight party and Mehrdad’s silent entering

cf C. Eugene and an array

C. Ayoub and Lost Array cf dp

CF #738(div2)C. Mocha and Hiking(构造)

CF #724(div2)C. Diluc and Kaeya,暴力,构造

CF Round410 C. Mike and gcd problem