CF1158A.The Party and Sweets
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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.
输入格式
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 ).
输出格式
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.
输入输出样例
输入#1
3 2 1 2 1 3 4
输出#1
12
输入#2
2 2 0 1 1 0
输出#2
-1
输入#3
2 3 1 0 1 1 2
输出#3
4
说明/提示
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 .