#P9559. [SDCPC 2023] Fast and Fat

[SDCPC 2023] Fast and Fat

题目描述

You're participating in a team competition of trail running. There are nn members in your team where viv_i is the speed of the ii-th member and wiw_i is his/her weight.

The competition allows each team member to move alone or carry another team member on their back. When member ii carries member jj, if member ii's weight is greater than or equal to member jj's weight, member ii's speed remains unchanged at viv_i. However, if member ii's weight is less than member jj's weight, member ii's speed will decrease by the difference of their weight and becomes vi(wjwi)v_i - (w_j - w_i). If member ii's speed will become negative, then member ii is not able to carry member jj. Each member can only carry at most one other member. If a member is being carried, he/she cannot carry another member at the same time.

For all members not being carried, the speed of the slowest member is the speed of the whole team. Find the maximum possible speed of the whole team.

输入格式

There are multiple test cases. The first line of the input contains an integer TT indicating the number of test cases. For each test case:

The first line contains an integer nn (1n1051 \le n \le 10^5) indicating the number of team members.

For the following nn lines, the ii-th line contains two integers viv_i and wiw_i (1vi,wi1091 \le v_i, w_i \le 10^9) indicating the speed and weight of the ii-th member.

It's guaranteed that the sum of nn of all test cases will not exceed 10510^5.

输出格式

For each test case output one line containing one integer indicating the maximum speed of the whole team.

2
5
10 5
1 102
10 100
7 4
9 50
2
1 100
10 1
8
1

提示

The optimal strategy for the sample test case is shown as follows:

  • Let member 11 carry member 44. As w1>w4w_1 > w_4, member 11's speed remains unchanged, which is still 1010.
  • Let member 33 carry member 22. As w3<w2w_3 < w_2, member 33's speed will decrease by w2w3=2w_2 - w_3 = 2 and becomes 102=810 - 2 = 8.
  • Member 55 shall move alone. His/Her speed is 99.

So the answer is 88.