题库练习 The Party and Sweets
← 上一题 下一题 →

A12650 | The Party and Sweets

时间限制1s
内存限制256MB
通过 / 提交0/0

题目描述

$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 \leq i \leq n$ the minimal number of sweets, which $i$ -th boy presented to some girl is equal to $b_i$ and for all $1 \leq j \leq m$ the maximal number of sweets, which $j$ -th girl received from some boy is equal to $g_j$ .

More formally, let $a_{i,j}$ be the number of sweets which the $i$ -th boy give to the $j$ -th girl. Then $b_i$ is equal exactly to the minimum among values $a_{i,1}, a_{i,2}, \ldots, a_{i,m}$ and $g_j$ is equal exactly to the maximum among values $b_{1,j}, b_{2,j}, \ldots, b_{n,j}$ .

You are interested in the minimum total number of sweets that boys could present, so you need to minimize the sum of $a_{i,j}$ for all $(i,j)$ such that $1 \leq i \leq n$ and $1 \leq j \leq m$ . You are given the numbers $b_1, \ldots, b_n$ and $g_1, \ldots, g_m$ , determine this number.

输入格式

The first line contains two integers $n$ and $m$ , separated with space — the number of boys and girls, respectively ( $2 \leq n, m \leq 100\,000$ ). The second line contains $n$ integers $b_1, \ldots, b_n$ , separated by spaces — $b_i$ is equal to the minimal number of sweets, which $i$ -th boy presented to some girl ( $0 \leq b_i \leq 10^8$ ). The third line contains $m$ integers $g_1, \ldots, g_m$ , separated by spaces — $g_j$ is equal to the maximal number of sweets, which $j$ -th girl received from some boy ( $0 \leq g_j \leq 10^8$ ).

输出格式

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
C++ 编辑器
输入
输出