#P2123. 皇后游戏
皇后游戏
Description
The queen has ministers. On each minister’s left and right hands, a positive integer is written. As the National Day approaches, the queen decides to award bonuses to the ministers. For the -th minister, the bonus amount equals the larger of the previous minister’s bonus and the sum of the numbers on the left hands of the first ministers, then plus the number on the right hand of the -th minister.
Formally, let the positive integer on the left hand of the -th minister be , and the one on the right hand be . The bonus received by the -th minister is , defined as:
$$c_{i} = \begin{cases} a_{1}+b_{1} & ,i=1 \\ \displaystyle \max \left \{ c_{i-1},\sum_{j=1}^{i}a_{j} \right \} +b_{i} & ,2\leq i \leq n \end{cases} % $$Of course, the frugal queen does not want too much bonus to be given. She asks you to reorder the queue so that the maximum bonus obtained by any minister is as small as possible.
Note: Reordering the queue does not mean you must change the order. Keeping every minister in their original position is allowed.
Constraints: For all testdata, , , .
Input Format
The first line contains a positive integer , the number of testdata groups.
Then for each of the parts, the first line contains a positive integer , the number of ministers.
In each of the next lines, there are two positive integers and , as defined above.
Output Format
Output lines. Each line contains one integer: the maximum bonus obtained by any minister.
1
3
4 1
2 2
1 2
8
2
5
85 100
95 99
76 87
60 97
79 85
12
9 68
18 45
52 61
39 83
63 67
45 99
52 54
82 100
23 54
99 94
63 100
52 68
528
902
Hint
- If the queue is ordered as 1, 2, 3, the maximum bonus obtained by any minister is 10.
- If the queue is ordered as 1, 3, 2, the maximum bonus obtained by any minister is 9.
- If the queue is ordered as 2, 1, 3, the maximum bonus obtained by any minister is 9.
- If the queue is ordered as 2, 3, 1, the maximum bonus obtained by any minister is 8.
- If the queue is ordered as 3, 1, 2, the maximum bonus obtained by any minister is 9.
- If the queue is ordered as 3, 2, 1, the maximum bonus obtained by any minister is 8.
When the queue is ordered as 3, 2, 1, the numbers on the three ministers’ left and right hands are: (1, 2), (2, 2), (4, 1).
- The 1st minister’s bonus is .
- The 2nd minister’s bonus is .
- The 3rd minister’s bonus is .
Translated by ChatGPT 5
京公网安备 11011102002149号