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 最終可能獲得的最大幸福度。

輸入格式

輸入按以下格式給出:

N
A_1 B_1
A_2 B_2
:
A_N B_N
  • 所有輸入值均為整數。
  • $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题解jianglyView

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.