Universal Cup Judging System

Universal Cup

Time Limit: 2.0 s Memory Limit: 1024 MB Total points: 100 Hackable ✓
Statistics

一共有 $N + 1$ 种食物:$N$ 种编号为 $1, 2, \dots, N$ 的食物,以及水啫喱。对于 $i = 1, 2, \dots, N$,食物 $i$ 的甜度为 $A_i$,辣度为 $B_i$。水啫喱的甜度为 $0$,辣度为 $0$。

UTPC-kun 首先吃水啫喱,然后以任意顺序将食物 $1, 2, \dots, N$ 各吃恰好一次,最后再次吃水啫喱。

在吃完第一个水啫喱后,UTPC-kun 的幸福度立即变为 $0$。此后,每当他吃一种食物时,他的幸福度会按如下方式变化:

  • 设 $a$ 和 $b$ 分别为他当前正在吃的食物的甜度和辣度,并设 $a'$ 和 $b'$ 分别为他紧接在前一个吃的食物的甜度和辣度。他的幸福度会增加 $\max(a - a', b - b')$。(幸福度的增加量可以为负数。)

通过选择吃食物 $1, 2, \dots, N$ 的最优顺序,求 UTPC-kun 最终可能获得的最大幸福度。

输入格式

输入按以下格式给出:

$$ \begin{array}{l} N \\ A_1 \ B_1 \\ A_2 \ B_2 \\ \vdots \\ A_N \ B_N \end{array} $$

数据范围

  • 所有输入值均为整数。
  • $1 \le N \le 5 \times 10^5$
  • $0 \le A_i, B_i \le 10^9$

输出格式

在单行中输出答案。

样例

输入样例 1

4
1 4
3 1
2 3
3 4

输出样例 1

6

输入样例 2

3
1 2
2 1
1 2

输出样例 2

3

输入样例 3

6
3 1
4 1
5 9
2 6
5 3
5 8

输出样例 3

18

说明

在第一个样例中,吃食物的一个最优顺序是 $1, 2, 3, 4$。在这种情况下,UTPC-kun 在吃完第一个水啫喱后,其幸福度的变化如下:

  • 吃食物 1:他的幸福度增加 $\max(1 - 0, 4 - 0) = 4$,变为 4。
  • 吃食物 2:他的幸福度增加 $\max(3 - 1, 1 - 4) = 2$,变为 6。
  • 吃食物 3:他的幸福度增加 $\max(2 - 3, 3 - 1) = 2$,变为 8。
  • 吃食物 4:他的幸福度增加 $\max(3 - 2, 4 - 3) = 1$,变为 9。
  • 吃水啫喱:他的幸福度增加 $\max(0 - 3, 0 - 4) = -3$,变为 6。

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
#1414EditorialOpen题解jiangly2026-04-05 16:31:51View

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.