CF2238A.Another Puzzle from Papyrus

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are filled with determination.

— Undertale

Papyrus came up with another puzzle for Frisk to solve. Papyrus brought two arrays aa and bb of length nn and allowed the following two operations to be performed:

  • choose any index ii (1≤i≤n1 \le i \le n) and change aia_i to ai−1a_i - 1. The execution time of such an operation is 11 second.
  • reorder all elements of array aa in any way. The execution time of such an operation is cc seconds.

You need to convert array aa into array bb.

Frisk wants to solve the puzzle as soon as possible. Help Frisk determine the minimum time needed to solve the puzzle. If there is no solution, output −1-1.

你充满了决心。

——《Undertale》

帕派瑞斯(Papyrus)又为弗里斯克(Frisk)设计了一个谜题。帕派瑞斯带来了两个长度为 nn 的数组 aa 和 bb,并允许执行以下两种操作:

  • 选择任意下标 ii(1≤i≤n1 \le i \le n),将 aia_i 修改为 ai−1a_i - 1。该操作耗时 11 秒。
  • 以任意方式重排数组 aa 的所有元素。该操作耗时 cc 秒。

你需要将数组 aa 转换为数组 bb。

弗里斯克希望尽快解出该谜题。请帮助弗里斯克确定解出谜题所需的最短时间。若无解,请输出 −1-1。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤5001 \le t \le 500). The description of the test cases follows.

The first line of each test case contains two integers nn and cc (1≤n,c≤1001 \le n, c \le 100) — the length of arrays aa and bb and the cost of the second operation.

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤1001 \le a_i \le 100) — the elements of the first array.

The third line of each test case contains nn integers b1,b2,…,bnb_1, b_2, \ldots, b_n (1≤bi≤1001 \le b_i \le 100) — the elements of the second array.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤5001 \le t \le 500)。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 cc(1≤n,c≤1001 \le n, c \le 100)—— 分别表示数组 aa 和 bb 的长度,以及第二种操作的代价。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤1001 \le a_i \le 100)—— 表示第一个数组的元素。

每个测试用例的第三行包含 nn 个整数 b1,b2,…,bnb_1, b_2, \ldots, b_n(1≤bi≤1001 \le b_i \le 100)—— 表示第二个数组的元素。

输出格式

For each test case, output a single integer representing the minimum number of seconds required to solve the puzzle, or −1-1 if it is impossible to solve the puzzle.

对于每个测试用例,输出一个整数,表示解决该谜题所需的最少秒数;如果无法解决该谜题,则输出 −1-1。

输入输出样例

  • 输入#1

    6
    3 5
    5 2 3
    2 3 4
    3 3
    1 2 3
    4 5 6
    4 4
    4 5 2 3
    3 5 1 2
    6 4
    2 4 5 3 6 8
    5 8 3 1 2 5
    5 11
    5 8 11 14 17
    16 12 10 10 6
    3 5
    20 14 20
    12 18 17

    输出#1

    6
    -1
    3
    8
    -1
    12

说明/提示

In the first test case, it is impossible to transform aa into bb using only subtraction because a2<b2a_2 \lt b_2. Let's rearrange the elements of array aa as follows: [5,2,3]⇒[2,3,5][5, 2, 3] \Rightarrow [2, 3, 5]. Now it is enough to subtract one from a3a_3, and we get that array aa becomes equal to array bb in 5+1=65 + 1 = 6 seconds.

In the second test case, all elements of aa are less than all elements of bb, which means aa cannot be transformed into bb.

In the third test case, you can choose not to rearrange the elements and get an answer of 33. If you rearrange them at least once, the answer will be at least 44, so the optimal answer is 33 seconds.

In the sixth test case, the array can be rearranged as follows: [14,20,20][14, 20, 20]. It can be seen that the cost will then be 5+(14−12)+(20−18)+(20−17)=125 + (14 - 12) + (20 - 18) + (20 - 17) = 12.

在第一个测试用例中,仅使用减法操作无法将 aa 变换为 bb,因为 a2<b2a_2 \lt b_2。让我们将数组 aa 的元素重新排列如下:[5,2,3]⇒[2,3,5][5, 2, 3] \Rightarrow [2, 3, 5]。此时只需对 a3a_3 减去 11,即可使数组 aa 变为与数组 bb 相等,总耗时为 5+1=65 + 1 = 6 秒。

在第二个测试用例中,aa 的所有元素均小于 bb 的所有元素,这意味着 aa 无法被变换为 bb。

在第三个测试用例中,你可以选择不重排元素,从而得到答案 33。若至少重排一次,答案至少为 44,因此最优答案为 33 秒。

在第六个测试用例中,数组可重排为:[14,20,20][14, 20, 20]。此时总代价为 5+(14−12)+(20−18)+(20−17)=125 + (14 - 12) + (20 - 18) + (20 - 17) = 12。

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

首页