CF1158A.The Party and Sweets
普及/提高-
通过率:0%
时间限制:1.00s
内存限制:256MB
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 a1,j,a2,j,…,an,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.
有 n 个男孩和 m 个女孩参加了派对。每个男孩向每个女孩赠送了某个非负整数数量的糖果(可以为零)。所有男孩按整数 1 到 n 编号,所有女孩按整数 1 到 m 编号。对于每个 1≤i≤n,第 i 个男孩送给某个女孩的最少糖果数恰好为 bi;而对于每个 1≤j≤m,第 j 个女孩从某个男孩那里收到的最多糖果数恰好为 gj。
更形式化地,令 ai,j 表示第 i 个男孩送给第 j 个女孩的糖果数量。则 bi 恰好等于 ai,1,ai,2,…,ai,m 中的最小值,而 gj 恰好等于 a1,j,a2,j,…,an,j 中的最大值。
你关心的是男孩们所赠送糖果的最小总数,即需最小化所有满足 1≤i≤n 且 1≤j≤m 的 ai,j 之和。现给出 b1,…,bn 和 g1,…,gm,请确定该最小总和。
输入格式
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).
第一行包含两个整数 n 和 m,以空格分隔——分别表示男孩和女孩的人数(2≤n,m≤100000)。
第二行包含 n 个整数 b1,…,bn,以空格分隔——其中 bi 表示第 i 个男孩送给某个女孩的最少糖果数(0≤bi≤108)。
第三行包含 m 个整数 g1,…,gm,以空格分隔——其中 gj 表示第 j 个女孩从某个男孩那里收到的最多糖果数(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。否则,输出男孩们可能送出的糖果总数的最小值,使得所有条件均被满足。
输入输出样例
输入#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.
在第一个测试用例中,男孩们可能送出的糖果总数的最小值为 12。例如,这可以实现如下:第一个男孩分别向两位女孩送出 1 颗和 4 颗糖果,第二个男孩分别送出 3 颗和 2 颗糖果,第三个男孩分别送出 1 颗和 1 颗糖果。容易验证,所有条件均被满足,且糖果总数恰好为 12。
在第二个测试用例中,不存在一种送糖果的方式,使得所有陈述均成立。
在第三个测试用例中,男孩们可能送出的糖果总数的最小值为 4。例如,这可以实现如下:第一个男孩分别向第一位、第二位、第三位女孩送出 1、1、2 颗糖果,而第二个男孩未向任何一位女孩送出糖果。容易验证,所有条件均被满足,且糖果总数恰好为 4。
输入解题思路,AI测评打分。不知道怎么写?