QOJ.ac

QOJ

Time Limit: 1.0 s Memory Limit: 256 MB Total points: 100 Hackable ✓

#15352. 邮递员

Statistics

$N$ 封信刚刚送达位于 $x = 0$ 的邮局,其中第 $i$ 封信需要被送到位置 $x = a_i$。我们亲爱的邮递员 BaoBao 将从邮局出发,独自送达所有这些信件。

不幸的是,BaoBao 的背包每次最多只能装 $K$ 封信(这意味着如果他想送达某封不在背包中的信,他必须返回邮局去取),因此他可能无法一次性送达所有 $N$ 封信。请注意,BaoBao 不能将信件临时放在邮局以外的地方并在稍后捡起。

BaoBao 送达所有 $N$ 封信所需移动的最小距离是多少?

BaoBao 不需要在邮局结束他的送信任务。

输入格式

输入包含多组测试数据。输入的第一行包含一个整数 $T$,表示测试数据的组数。对于每组测试数据:

第一行包含两个整数 $N$ 和 $K$($1 \le K \le N \le 10^5$),分别表示信件的总数和背包的容量。

第二行包含 $N$ 个整数 $a_1, a_2, \dots, a_N$($-10^9 \le a_i \le 10^9$),表示每封信的目的地。

保证所有测试数据的 $N$ 之和不超过 $10^6$。

输出格式

对于每组测试数据,输出一行,包含一个整数,表示 BaoBao 从位于 $x = 0$ 的邮局出发,送达所有信件所需移动的最小距离。

样例

输入样例 1

2
5 3
-1 -2 3 -4 -5
6 3
1 0 -2 -1 1 2

输出样例 1

13
6

说明

对于第一组样例测试数据,BaoBao 可以先送达第 1 封和第 3 封信(前往 $x = 3$,然后前往 $x = -1$,接着返回邮局),然后送达第 2 封、第 4 封和第 5 封信(前往 $x = -2$,然后前往 $x = -4$,接着前往 $x = -5$),并在 $x = -5$ 处结束他的送信任务。

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.