QOJ.ac

QOJ

時間限制: 1 s 記憶體限制: 1024 MB 總分: 100

#5428. 卡牌游戏

统计

Nicole 和 Simon 正在进行一场包含 $N$ 轮的卡牌游戏。在第 $i$ 轮中,Nicole 打出一张写有数字 $a_i$ 的卡牌。Simon 必须从他手中的卡牌中选出一张进行回应。如果 Simon 打出的卡牌数值为 $b_i$,则 Nicole 获得 $|a_i - b_i|$ 分。因此,Simon 打出的卡牌数值与 Nicole 打出的卡牌数值相差越远,Nicole 获得的分数就越高。

已知 Nicole 将要打出的所有卡牌以及 Simon 手中初始持有的所有卡牌,如果 Simon 采取最优策略,Nicole 能获得的最低总分是多少?如果 Simon 有 $M$ 张卡牌,他们总共会进行 $M$ 或 $M-1$ 轮游戏,即 $M = N$ 或 $M = N + 1$。

输入格式

第一行包含两个整数 $N$ ($1 \le N \le 2 \cdot 10^5$) 和 $M$ ($N \le M \le N + 1$),分别表示游戏轮数和 Simon 持有的卡牌数量。

第二行包含 $N$ 个整数 $a_1, \dots, a_N$ ($0 \le a_i \le 10^9$),其中 $a_i$ 是 Nicole 在第 $i$ 轮打出的卡牌数值。

第三行包含 $M$ 个整数 $b_1, \dots, b_M$ ($0 \le b_i \le 10^9$),表示 Simon 手中卡牌的数值。

输出格式

输出 Simon 采取最优策略时,Nicole 能获得的最低总分。

子任务

你的解法将在若干测试点组上进行测试。为了获得某一组的分数,必须通过该组内的所有测试点。

子任务 分值 数据范围
1 15 $N \le 8$
2 25 $N \le 2000$
3 20 $M = N$
4 40 无附加限制

说明

在样例 1 中,Simon 的最优策略是:第一轮打出数值为 1 的卡牌,第二轮打出数值为 2 的卡牌。此时 Nicole 获得 $|1 - 1| + |10 - 2| = 8$ 分。

在样例 2 中,Simon 按顺序打出数值为 2, 5, 1 的卡牌。

在样例 3 中,Simon 按顺序打出数值为 4, 6, 3, 1 的卡牌。

样例

输入 1

2 3
1 10
2 0 1

输出 1

8

输入 2

3 3
4 8 1
5 1 2

输出 2

5

输入 3

4 5
6 10 6 2
1 4 0 6 3

输出 3

10

输入 4

5 5
0 0 0 0 0
1000000000 1000000000 1000000000 1000000000 1000000000

输出 4

5000000000

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.