Universal Cup Judging System

Universal Cup

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

$1, 2, \dots, N$번이 붙은 $N$종류의 음식과 물젤리를 합쳐 총 $N + 1$종류의 음식이 있습니다. $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.