Chef and Roma are playing a game. Rules of the game are quite simple.
Initially there are N piles of stones on the table.
In each turn, a player can choose one pile and remove it from the table.
Each player want to maximize the total number of stones removed by him.
Chef takes the first turn.
Please tell Chef the maximum number of stones he can remove assuming that both players play optimally.
Input
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains a single integer N denoting the number of piles.
The second line contains N space separated integers A1, A2, ..., AN denoting the number of stones in each pile.
Output
For each test case, output a single line containg the maximum number of stones that Chef can remove.
Constraints
1 ≤ Ai ≤ 10^9
Example
Input:
2
3
1 2 3
3
1 2 1
Output:
4
3
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to solve this game: N piles, each pile has some number of stones. Players alternate, each turn a player chooses one pile and removes it entirely. So each pile is removed by the player who picks it. Both players want to maximize the total number of stones they remove (i.e., sum of sizes of piles they take). Chef goes first. Both play optimally. We need the maximum stones Chef can guarantee.
This is a classic game: Two players alternately take from a set of numbers, each number taken adds to that player's score. Equivalent to a "take-turns picking numbers" game, where each number is an item with a value, players want to maximize their own total. Since both have same objective (maximize their sum), and they choose one item per turn, the optimal strategy is to always take the largest remaining pile? But not exactly, because the order matters and the opponent will also take large piles. Actually, this is a zero-sum game: total sum is fixed, so maximizing one's own sum is equivalent to minimizing opponent's sum. Since they alternate, the optimal play for both is to always take the largest remaining pile. Because in a finite collection of items, if both players are rational and the only moves are to take any remaining item, the optimal strategy is indeed to sort the items in descending order, and then players alternately pick from the sorted list: first player gets the largest, second gets the second largest, first gets third, etc. This is a well-known result: In a two-player alternating pick game where each chooses an item from a common set, and each item's value is known, the optimal strategy for both is to take the highest value item available. This can be proven by a simple exchange argument: Suppose at some move a player picks an item that is not the largest remaining. Then the opponent can pick the largest, and the player could have done better by taking the largest himself, because the rest of the game is symmetric but with the player's move wasted. More formally, it's a game of perfect information, zero-sum, and it's known that greedy is optimal. So Chef's maximum total is the sum of the piles at odd positions after sorting descending.
Thus the answer: sort array descending, sum elements at indices 0,2,4,... (0-indexed). Or sum of elements at 1st, 3rd, 5th... if counting from 1.
But we need to be careful: Is there any nuance? The players remove entire pile; there's no restriction on which pile to choose except that they can choose any pile. So yes, the optimal strategy is to always take the current largest pile. So Chef's optimal total = sum of the largest, third largest, fifth largest, etc.
Check examples:
Test1: [1,2,3] sorted descending: [3,2,1]. Chef picks 3, Roma picks 2, Chef picks 1 => Chef total=4. Output 4. matches.
Test2: [1,2,1] sorted descending: [2,1,1]. Chef picks 2, Roma picks 1 (largest remaining 1), Chef picks 1 => Chef total=3. Output 3. matches.
Thus solution is straightforward.
But we must consider potential tricky aspects: The game might have a different optimal strategy if there are many piles? Actually, typical "pick from a set" with no other constraints: Greedy works. So solution: For each test case, read N, read array A, sort descending, sum every other element (starting from first). Use 64-bit (long long) because Ai up to 1e9, N not given explicitly in constraints but can be large, maybe up to 1e5 or 10^6? Usually N could be up to 10^5 per test case. Sum might exceed 32-bit. So use long long.
Implementation: For T test cases, sort each array descending. Then compute sum. Complexity O(T * N log N). That should be fine.
However, sometimes problems like this might require alternative DP? But given constraints typical for CodeChef (Chef and Roma game) I think it's this.
Double-check: Could there be a scenario where not picking the largest is beneficial? Consider if picking a smaller pile might force opponent to pick a certain pile? No, because opponent can always pick any pile. There's no other interaction. The game is simply a set of numbers. So the move has no effect on future moves except that the chosen number is removed. So the game state is just the set of remaining numbers. At each turn, the player can choose any remaining number. This is equivalent to both players taking turns picking numbers from a multiset. This is a well-known "optimal play is to pick the largest" because any deviation allows opponent to pick the largest, which is always disadvantageous.
Thus answer is clear.
Edge Cases: N=1. Chef picks that pile, sum is that pile.
N=0 (not possible as per constraints, but N likely >=1). So fine.
Thus solution: Sort descending, take alternating sum.
Implementation details: Use vector, sort with greater(). Or sort ascending and traverse from end.
We'll output each answer on new line.
Let's provide a solution.
But the user requested: "Please tell Chef the maximum number of stones he can remove assuming that both players play optimally." So answer as above.
We'll also include brief explanation.
Now produce final answer in code.
The answer must be in C++ or any language? The problem statement doesn't specify, but typical for CodeChef, they accept multiple languages. I'll provide C++ solution.
Pseudo:
#include
#include
#include
using namespace std;
int main() {
int T;
cin >> T;
while (T--) {
int N;
cin >> N;
vector A(N);
for (int i=0; i<N; ++i) cin >> A[i];
sort(A.begin(), A.end(), greater());
long long sum = 0;
for (int i=0; i<N; i+=2) sum += A[i];
cout << sum << endl;
}
return 0;
}
That's it.
But ensure long long. Actually, A values up to 1e9, sum could be up to 1e9 * (N/2+1) maybe up to 5e13 if N=1e5, fits in 64-bit.
Thus final answer.
Also, one might use partial sort? Not needed.
Potential improvements: Could compute by sorting but it's fine.
Now, the user might expect an explanation, but the request is "solve this problem" presumably to output code. We'll output as final.
Thus final answer: Provide code.
But the platform is "ChatGPT" and the format: "final". We'll give solution.
I'll include brief comments.
That's it. ```cpp
#include
#include
#include
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
code
int T;
cin >> T;
while (T--) {
int N;
cin >> N;
vector<long long> A(N);
for (int i = 0; i < N; ++i) {
cin >> A[i];
}
// Sort in descending order
sort(A.begin(), A.end(), greater<long long>());
// Chef picks piles at indices 0, 2, 4, ...
long long chef_sum = 0;
for (int i = 0; i < N; i += 2) {
chef_sum += A[i];
}
cout << chef_sum << "\n";
}
return 0;