QOJ.ac

QOJ

Límite de tiempo: 2.0 s Límite de memoria: 1024 MB Puntuación total: 100

#9447. 仰慕之人

Estadísticas

Namuka 有一个长度为 $N$ 的整数序列 $A = (A_1, A_2, \dots, A_N)$,Namuka 的理想对象有一个长度为 $M$ 的序列 $B = (B_1, B_2, \dots, B_M)$。

为了更接近理想对象,Namuka 从 $A$ 中选择 $M$ 个不同的元素,以任意顺序排列,组成一个长度为 $M$ 的序列 $C = (C_1, C_2, \dots, C_M)$。

求 $\sum_{i=1}^M |B_i - C_i|$ 的最小值。

数据范围

  • $1 \le M \le N \le 5000$
  • $1 \le A_i, B_i \le 10^9$

输入格式

输入通过标准输入按以下格式给出:

$N \ M$ $A_1 \ A_2 \ \dots \ A_N$ $B_1 \ B_2 \ \dots \ B_M$

输出格式

输出答案。

样例

输入 1

5 3
2 6 5 1 1
6 3 8

输出 1

4

输入 2

3 2
1 1 9
1 1

输出 2

0

输入 3

11 7
13 21 9 5 16 32 15 29 20 40 4
24 34 43 39 18 30 11

输出 3

32

说明

对于第一个样例: 例如,通过选择 $C = (6, 2, 5)$,可以达到最小值 $|6 - 6| + |3 - 2| + |8 - 5| = 4$。

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.