#P17297. [ICPC 2026 Xi'an I] Qenerals
[ICPC 2026 Xi'an I] Qenerals
Problem Description
Yuki is playing a game called Qenerals.
At time , Yuki has soldiers and occupies fortress. There are unoccupied fortresses on the map, where the -th fortress has a parameter .
The game lasts for seconds. For each positive integer such that :
- At the beginning of the -th second, each fortress occupied by Yuki produces soldier, i.e., .
- At the end of the -th second, Yuki can perform any number of operations (including zero). In each operation, Yuki chooses an unoccupied fortress such that , consumes soldiers, and occupies fortress , i.e., and .
You need to help Yuki determine the maximum number of soldiers she can have after the game ends.
Input Format
This problem contains multiple test cases.
The first line contains a positive integer , representing the number of test cases.
For each test case:
- The first line contains two positive integers .
- The second line contains positive integers .
It is guaranteed that the sum of over all test cases does not exceed .
Output Format
For each test case, output a single line containing an integer representing the maximum number of soldiers Yuki can have after the game ends.
3
2 3
2 1
3 6
1 1 3
3 5
1 1 1
4
13
12
Hint
For the first test case:
- At the beginning of the -st second, the number of fortresses occupied by Yuki is , so the number of soldiers increases from to .
- At the end of the -st second, Yuki can choose to occupy the -nd fortress, so increases from to , and decreases from to .
- At the beginning of the -nd second, , so increases from to .
- At the end of the -nd second, Yuki can choose not to perform any operations.
- At the beginning of the -rd second, , so increases from to .
- At the end of the -rd second, Yuki can choose not to perform any operations.
- After the game ends, the number of soldiers is . It can be proven that is the maximum number of soldiers Yuki can have.
For the second test case:
- Yuki can occupy the -st, -nd, and -rd fortresses at the end of the -st, -nd, and -rd seconds respectively, allowing her to have soldiers after the game ends.
For the third test case:
- Yuki can occupy the -st fortress at the end of the -st second, and the -nd and -rd fortresses at the end of the -nd second, allowing her to have soldiers after the game ends.