Problem
Two players take turns removing stones from several piles. The player who takes the last stone wins.
There are piles, numbered from pile to pile starting from the left. is between and inclusive, and each pile initially has at least one stone. The total number of stones across all piles does not exceed . A pile keeps its number even after all of its stones are gone.
The game starts with the first player A, and A and B take turns alternately.
On your turn, choose one pile that still has stones and take one or more stones from it. You may take from only one pile at a time, and you cannot take more stones than that pile has left.
For example, if the piles currently hold stones, taking stones from pile gives . Pile has only one stone, so you cannot take two from it.
Each player has a time limit of second for the entire game. Only the time taken to output an action on your own turn is deducted, and the remaining time does not grow back on the next turn.
The following actions are invalid.
- Printing a string that does not match the output format
- Choosing a pile that does not exist or has no stones left
- Taking fewer than one stone, or taking more stones than the chosen pile has left
A player who makes an invalid action loses immediately. An invalid action is not applied to the piles, and the opponent does not get another turn. A player also loses immediately if they fail to output an action within the time limit or if an error occurs while their program is running.
The game ends when any of the following conditions is met.
- All piles are empty
- A player makes an invalid action
- A player exceeds the time limit or has a runtime error
If the game ends because all stones are gone, the player who took the last stone wins.
Design an AI that reads the number of stones left in each pile, takes the last stone, and becomes the final winner!
Input
Before the game starts, the judge gives both players the same initial piles as the following two lines.
N
stones_1 stones_2 ... stones_N
Nis an integer between and inclusive, giving the number of piles.- The second line gives the number of stones in each pile,
stones_1,stones_2, ...,stones_N, separated by spaces. - Each pile has at least one stone, and the total number of stones across all piles does not exceed .
- Piles are numbered starting from at the left of the second line.
The judge communicates with the players one line at a time as follows.
| Command | Judge→Player (input) | Player→Judge (output) | Time limit (ms) | Description |
|---|---|---|---|---|
| READY | READY (FIRST | SECOND) |
OK |
500 |
Tells the player whether they move first or second. FIRST is the first player A, and SECOND is the second player B. |
| OPP | OPP, then on the next line PASS opponent_time or REMOVE heap_number take opponent_time |
- | - | Gives the opponent's previous action and remaining time. On the first player's first turn there is no opponent action, so PASS is given. |
| TURN | TURN my_time |
REMOVE heap_number take |
my_time |
Gives your remaining time. Output the number of the pile to take stones from this turn and how many stones to take. |
| FINISH | FINISH |
- | - | Announces the end of the game. The player must exit the program normally without any further output. |
heap_number,take,my_time, andopponent_timeare integers.heap_numberis between andNinclusive and is the number of the pile to take stones from.takeis at least and cannot exceed the number of stones currently left in the chosen pile.my_timeandopponent_timeare given in milliseconds.my_timeis your remaining time just before your action, andopponent_timeis the opponent's remaining time after finishing their previous action.- OPP is always given as two lines. The first line is
OPP, and the second line isPASS opponent_timeorREMOVE heap_number take opponent_time. - Each player has milliseconds for the entire game. READY response time is not included, and only the time spent responding to your own TURN is deducted.
- The judge does not resend all piles every turn. You must track the current piles by applying your own actions and the opponent actions received via OPP to the initial state in order.
- After every output, print a newline and then flush the buffer.
- Print debug messages only to standard error. Do not print anything other than the specified responses to standard output.
- If you fail to output within the time limit, you lose immediately by time limit exceeded. You also lose immediately if you print a string that does not match the output format or an invalid action, and that action is not passed to the opponent via OPP.
Example
In a match whose initial piles are , communication can proceed as follows. The table below omits the two lines of initial state given to both players. Time values in the table are for illustration only.
| First player input | First player output | Second player input | Second player output |
|---|---|---|---|
READY FIRST |
READY SECOND |
||
OK |
OK |
||
OPP |
|||
PASS 1000 |
|||
TURN 1000 |
REMOVE 1 1 |
||
OPP |
|||
REMOVE 1 1 988 |
|||
TURN 1000 |
REMOVE 2 1 |
||
OPP |
|||
REMOVE 2 1 991 |
|||
TURN 988 |
REMOVE 1 1 |
||
FINISH |
FINISH |
Sample code
Author
