QOJ.ac

QOJ

时间限制: 4 s 内存限制: 256 MB 总分: 100

#71. 蛋糕 3

统计

今天是 IOI-chan 的生日,她的哥哥 JOI-kun 预订了生日蛋糕。虽然他原本打算买一个完整的蛋糕,但他错误地订购了 $N$ 块蛋糕。这些蛋糕编号为 $1$ 到 $N$,每块蛋糕都有其价值和颜色。第 $i$ 块蛋糕($1 \le i \le N$)的价值为 $V_i$,其颜色的深度为 $C_i$。

为了制作一个完整的蛋糕,他决定挑选 $M$ 块不同的蛋糕,并以任意顺序将它们排列成圆形。他所制作的完整蛋糕的美观度定义为:

$$\sum_{j=1}^{M} V_{k_j} - \sum_{j=1}^{M} |C_{k_j} - C_{k_{j+1}}|$$

如果他选择了 $k_1, \dots, k_M$ 这些块并按此顺序排列(此处令 $k_{M+1} = k_1$)。换句话说,完整蛋糕的美观度等于其所有碎片价值之和减去每两块相邻碎片之间颜色深度差的绝对值之和。JOI-kun 希望尽可能让这个完整的蛋糕变得美观。

请编写一个程序,给定蛋糕块的数量、制作完整蛋糕所需的块数,以及每块蛋糕的价值和颜色深度,计算 JOI-kun 能制作出的完整蛋糕的最大美观度。

输入格式

从标准输入读取以下数据。输入中的所有值均为整数。

$N \ M$ $V_1 \ C_1$ $:$ $V_N \ C_N$

输出格式

向标准输出写入一行。输出应包含 JOI-kun 能制作出的完整蛋糕的最大美观度。

数据范围

  • $3 \le N \le 200\,000$
  • $3 \le M \le N$
  • $1 \le V_i \le 1\,000\,000\,000$ ($1 \le i \le N$)
  • $1 \le C_i \le 1\,000\,000\,000$ ($1 \le i \le N$)

子任务

  1. (5 分) $N \le 100$
  2. (19 分) $N \le 2000$
  3. (76 分) 无附加限制

样例

样例输入 1

5 3
2 1
4 2
6 4
8 8
10 16

样例输出 1

6

说明

如果 JOI-kun 选择第 1、3 和 2 块并按此顺序排列,其碎片价值之和为 $2 + 6 + 4 = 12$,颜色深度差之和为 $|1 - 4| + |4 - 2| + |2 - 1| = 6$。因此,完整蛋糕的美观度为 $12 - 6 = 6$。

他也可以通过选择第 2、3 和 4 块并按此顺序排列来制作美观度为 6 的完整蛋糕。

由于他无法制作出更美观的完整蛋糕,你应该输出 6。

样例输入 2

8 4
112103441 501365808
659752417 137957977
86280801 257419447
902409188 565237611
965602301 689654312
104535476 646977261
945132881 114821749
198700181 915994879

样例输出 2

2323231661

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
EditorialOpen Official EditorialQingyu- Download
#257EditorialOpen题解jiangly2025-12-13 00:37:40View

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.