#abc352c. C - Standing On The Shoulders
C - Standing On The Shoulders
Score: points
问题陈述
有 个巨人,编号为 到 。当第 个巨人站立在地面上时,他们的肩膀高度是 ,头部高度是 。
你可以选择一个排列 ,它是 的一个排列,并根据以下规则堆叠这 个巨人:
-
首先,将巨人 放在地上。巨人 的肩膀将离地面 的高度,他们的头部将离地面 的高度。
-
对于 ,按顺序将巨人 放在巨人 的肩膀上。如果巨人 的肩膀离地面的高度是 ,那么巨人 的肩膀将离地面 的高度,他们的头部将离地面 的高度。
找出顶部巨人 的头部离地面的最大可能高度。
以上为大语言模型 kimi 翻译,仅供参考。
Problem Statement
There are giants, named to . When giant stands on the ground, their shoulder height is , and their head height is .
You can choose a permutation of and stack the giants according to the following rules:
-
First, place giant on the ground. The giant 's shoulder will be at a height of from the ground, and their head will be at a height of from the ground.
-
For in order, place giant on the shoulders of giant . If giant 's shoulders are at a height of from the ground, then giant 's shoulders will be at a height of from the ground, and their head will be at a height of from the ground.
Find the maximum possible height of the head of the topmost giant from the ground.
Constraints
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Output
Print the answer.
Sample Input 1
3
4 10
5 8
2 9
Sample Output 1
18
If , then measuring from the ground, giant has a shoulder height of and a head height of , giant has a shoulder height of and a head height of , and giant has a shoulder height of and a head height of .
The head height of the topmost giant from the ground cannot be greater than , so print .
Sample Input 2
5
1 1
1 1
1 1
1 1
1 1
Sample Output 2
5
Sample Input 3
10
690830957 868532399
741145463 930111470
612846445 948344128
540375785 925723427
723092548 925021315
928915367 973970164
563314352 832796216
562681294 868338948
923012648 954764623
691107436 891127278
Sample Output 3
7362669937