CF1158A.The Party and Sweets

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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

More formally, let ai,ja_{i,j} be the number of sweets which the ii-th boy give to the jj-th girl. Then bib_i is equal exactly to the minimum among values ai,1,ai,2,…,ai,ma_{i,1}, a_{i,2}, \ldots, a_{i,m} and gjg_j is equal exactly to the maximum among values a1,j,a2,j,…,an,ja_{1,j}, a_{2,j}, \ldots, a_{n,j}.

You are interested in the minimum total number of sweets that boys could present, so you need to minimize the sum of ai,ja_{i,j} for all (i,j)(i,j) such that 1≤i≤n1 \leq i \leq n and 1≤j≤m1 \leq j \leq m. You are given the numbers b1,…,bnb_1, \ldots, b_n and g1,…,gmg_1, \ldots, g_m, determine this number.

有 nn 个男孩和 mm 个女孩参加了派对。每个男孩向每个女孩赠送了某个非负整数数量的糖果(可以为零)。所有男孩按整数 11 到 nn 编号,所有女孩按整数 11 到 mm 编号。对于每个 1≤i≤n1 \leq i \leq n,第 ii 个男孩送给某个女孩的最少糖果数恰好为 bib_i;而对于每个 1≤j≤m1 \leq j \leq m,第 jj 个女孩从某个男孩那里收到的最多糖果数恰好为 gjg_j。

更形式化地,令 ai,ja_{i,j} 表示第 ii 个男孩送给第 jj 个女孩的糖果数量。则 bib_i 恰好等于 ai,1,ai,2,…,ai,ma_{i,1}, a_{i,2}, \ldots, a_{i,m} 中的最小值,而 gjg_j 恰好等于 a1,j,a2,j,…,an,ja_{1,j}, a_{2,j}, \ldots, a_{n,j} 中的最大值。

你关心的是男孩们所赠送糖果的最小总数,即需最小化所有满足 1≤i≤n1 \leq i \leq n 且 1≤j≤m1 \leq j \leq m 的 ai,ja_{i,j} 之和。现给出 b1,…,bnb_1, \ldots, b_n 和 g1,…,gmg_1, \ldots, g_m,请确定该最小总和。

输入格式

The first line contains two integers nn and mm, separated with space — the number of boys and girls, respectively (2≤n,m≤100 0002 \leq n, m \leq 100\,000). The second line contains nn integers b1,…,bnb_1, \ldots, b_n, separated by spaces — bib_i is equal to the minimal number of sweets, which ii-th boy presented to some girl (0≤bi≤1080 \leq b_i \leq 10^8). The third line contains mm integers g1,…,gmg_1, \ldots, g_m, separated by spaces — gjg_j is equal to the maximal number of sweets, which jj-th girl received from some boy (0≤gj≤1080 \leq g_j \leq 10^8).

第一行包含两个整数 nn 和 mm,以空格分隔——分别表示男孩和女孩的人数(2≤n,m≤100 0002 \leq n, m \leq 100\,000)。
第二行包含 nn 个整数 b1,…,bnb_1, \ldots, b_n,以空格分隔——其中 bib_i 表示第 ii 个男孩送给某个女孩的最少糖果数(0≤bi≤1080 \leq b_i \leq 10^8)。
第三行包含 mm 个整数 g1,…,gmg_1, \ldots, g_m,以空格分隔——其中 gjg_j 表示第 jj 个女孩从某个男孩那里收到的最多糖果数(0≤gj≤1080 \leq g_j \leq 10^8)。

输出格式

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.

如果所述情况不可能,则输出 −1-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 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.

在第一个测试用例中,男孩们可能送出的糖果总数的最小值为 1212。例如,这可以实现如下:第一个男孩分别向两位女孩送出 11 颗和 44 颗糖果,第二个男孩分别送出 33 颗和 22 颗糖果,第三个男孩分别送出 11 颗和 11 颗糖果。容易验证,所有条件均被满足,且糖果总数恰好为 1212。

在第二个测试用例中,不存在一种送糖果的方式,使得所有陈述均成立。

在第三个测试用例中,男孩们可能送出的糖果总数的最小值为 44。例如,这可以实现如下:第一个男孩分别向第一位、第二位、第三位女孩送出 11、11、22 颗糖果,而第二个男孩未向任何一位女孩送出糖果。容易验证,所有条件均被满足,且糖果总数恰好为 44。

输入解题思路,AI测评打分。不知道怎么写?

首页