Valentine Vex CodeChef Solution | CodeChef Problem Solution 2022
Alice and Bob play a game on an array of integers. They alternate moves, with Alice making the first move.
The rules are as follows:
- On their first move, a player can pick any element in the array, add its value to their score, and then remove it from the array.
- On a move that is not their first move, the player should pick an element with the opposite parity of the element chosen on their previous move, add its value to their score, and then remove it from the array.
- If a player cannot make a move, either because the array is empty or it doesn't contain an element of the parity they need, the player skips their turn.
- The game ends when both players are unable to move.
Note that the parity of an element chosen by a player depends only on the parity of their own previously chosen element — it does not depend on what their opponent last chose.
Determine the optimal score of Alice if both players play optimally, each attempting to maximize their own score. If there are multiple ways to obtain maximum score for themselves, the players will adopt a strategy that will maximize the score of their opponent whilst obtaining their maximum score themselves.
Note that it is not necessary to use all the elements in the array.
Input Format
- The first line of input will contain a single integer , denoting the number of test cases. Then the test cases follow.
- Each test case consists of two lines of input.
- The first line of each test case contains one integer — the size of the array
- The second line of each test case contains space-separated integers, denoting the array elements.
Output Format
For each test case, output on a new line the optimal score achieved by Alice.
Constraints
- Sum of across all test cases won't exceed
Sample Input 1
3
4
1 2 3 4
3
45 10 53
6
7 5 2 2 6 2
Sample Output 1
5
55
15
Explanation
Test case : Under optimal play, Alice's maximum score is . One sequence of moves leading to this score is:
- On her first move, Alice picks .
- On his first move, Bob picks .
- On her second move, Alice must choose an odd number, since she previously picked an even number. So, Alice's only choice is to pick .
- On his second move, Bob picks , since he must choose an even number and it is the only one left.
Alice's score is thus . Note that this is not the only optimal strategy — however, any other strategy where both players play optimally will still lead to Alice having a score of .
Test case : Alice's strategy is as follows:
- On her first move, Alice picks .
- On his first move, Bob picks .
- Finally, Alice picks , ending up with a score of .
- Note that if Alice picks either or on her first move, Bob will pick on his move. This leaves Alice unable to make a move, so she skips her turn and Bob picks the remaining number, leaving Alice with a score strictly smaller than .
Chef and Chapters CodeChef Solution | CodeChef Problem Solution 2022
Presents for Cheffina CodeChef Solution | CodeChef Problem Solution 2022
Chef and Bird farm CodeChef Solution | CodeChef Problem Solution 2022
Valentine Vex CodeChef Solution | CodeChef Problem Solution 2022
Buying Sweets CodeChef Solution | CodeChef Problem Solution 2022
Reduce to 1 CodeChef Solution | CodeChef Problem Solution 2022
Three Arrays CodeChef Solution | CodeChef Problem Solution 2022
Test case : Alice picks and for a score of , while Bob picks and for a score of . Note that Bob can also achieve a score of by picking and and then skipping his third move, but he won't do this because it would lead to Alice only reaching a score of — as mentioned earlier, a player attempts to maximize their own score, and then maximize their opponent's score.
Join Now for Solution:-
No comments:
Post a Comment