A. The Party and Sweets
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
=========================================================================
每个男生都要给每个一个,并且最小是b[i],那我们可以先把给每个女生的都算成b[i]
这样就满足了基本条件,女生这边的条件是最大值,首先a的最小值,要是小于b的最大值,那么就一定不可以了,因为b中这个最大值难免分配给每一个女生,会超过一个女生的最小值的。
然后就考虑b[n]<=a[1]的情况,我们可以在b[n]的基础上,添加一些值来达到ai的效果,也就是a[i]-b[n],但值得注意的是,一旦我们a[1]不等于b[n],我们这样做就把b[n]的值修改成了a[1],也就是说b[n]是至少出现一次的,这种情况我们还必须让b[n]出现一次,还必须让分配在a[1]上的b等于a1,易知b[n-1]已经分配了m个了,少一个也没事,我们把分配在a1上的b[n-1]变成a1即可。
- #include
- using namespace std;
- typedef long long ll;
-
- int a[100000+10],b[100000+10];
-
- int main()
- {
- ll sum=0;
-
- int n,m;
-
- cin>>n>>m;
-
- for(int i=1;i<=n;i++)
- {
- cin>>a[i];
- }
-
-
- for(int j=1;j<=m;j++)
- {
- cin>>b[j];
- }
-
-
- sort(a+1,a+1+n);
- sort(b+1,b+1+m);
-
- {
- cout<<-1;
- return 0;
- }
- else
- {
-
- for(int i=1;i
- {
- sum+=a[i];
- }
-
- sum*=m;
-
-
- for(int i=1;i<=m;i++)
- {
- sum+=b[i];
-
- }
-
-
- if(a[n]!=b[1])
- {
- sum+=a[n];
-
- sum-=a[n-1];
- }
-
- cout<<sum<
- }
-
- }
-
相关阅读:
浅谈Elasticsearch查询和搜索
Python 字符串
leetcode-260.只出现一次的数字 III
Git分布式管理-头歌实验日志和版本回退
【Transformers】第 6 章 :总结
Go :测试编译器诊断函数缺少返回语句(附完整源码)
1093 Count PAT‘s
Mybatis Available parameters are [0, 1, param1, param2]解决方法
hdu 5947 数论
MySQL - Heap 表是什么?
-
原文地址:https://blog.csdn.net/jisuanji2606414/article/details/126337569