Game
Time limit2sMemory limit512 MB
For each starting size P, two players alternately take a number from a buffer that refills with later sequence elements, and we report Alice's score minus Bob's.
- Level
Medium7 of 10
- Topics
- Greedy, Sorting, Game theory, Prefix sum
- Solved
- No attempts yet
Problem
Alice and Bob play the following game.
There is a sequence of positive integers numbered from to . Each element is at most , and equal values may appear more than once. At the start of a game, the first elements of the sequence form a multiset . Alice moves first, and the two players alternate turns. Each turn proceeds as follows:
- The player to move chooses one number from , removes it, and adds its value to their own score. Both scores start at .
- If the sequence still has an element that has not entered , the earliest such element is added to . That is, after the first removal the element with index is added, after the second removal the element with index is added, and so on. If the sequence is already exhausted, nothing is added.
Play continues until becomes empty. Each player tries to maximize their own final score. The result of a game is Alice's score minus Bob's score.
Write a program game that processes games played on one given starting sequence, where the games differ only in their starting sizes.
Input
The first line of the standard input contains two positive integers and , separated by a space.
The second line contains positive integers , separated by spaces, representing the elements of the given sequence.
The third line contains positive integers , separated by spaces. The -th game starts from the set built from the first elements of the sequence, where .
Output
Print lines to the standard output. The -th line contains a single integer, the result of the -th game. The games are numbered from to in the order given by the input.
Constraints
- for every
- for every
- In of the tests:
- In of the tests:
- In of the tests: ,
Hint
Each game lasts exactly moves. Alice takes the numbers on the odd-numbered turns, and Bob takes the numbers on the even-numbered turns. While arrivals remain, one new number enters after every turn, so always holds numbers at the start of a turn during that phase. Once arrivals run out, the rest of is removed in order until it is empty.