Card Sandcastle
Time limit1sMemory limit1024 MB
Given N slots each holding a card value 1 to 5, players alternately pick a slot and remove that slot and the next c_i slots; find the smallest first move that wins.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Game theory, Intervals
- Solved
- No attempts yet
Problem
The sandcastle game is played by two people who take turns removing small amounts of sand from a sandcastle, and whoever knocks the sandcastle down loses. Juhyeon and Sejung wanted to play the sandcastle game for old times' sake, but the nearby sand pit had closed, so they could not. They did have cards with positive integers between and written on them, so they came up with a sandcastle game they could play with cards. The rules of the card sandcastle game are as follows.
- Prepare card slots and number them from to .
- Place one card with a positive integer between and written on it on each card slot.
- The two players take turns.
- On your turn, choose a non-empty card slot among the card slots. If the chosen card slot has number and the number written on the card in that slot is , you must take all cards in card slots , , , , including the chosen slot. If any of those card slots does not exist or is empty, you cannot choose card slot .
- The player who has no card slot left to choose on their turn loses the game.
Sejung is smart and will always take cards in the optimal way. Juhyeon, who moves first, wants to win the game even if it means listening to your advice. Give Juhyeon, who is thirsty for a win, some advice.
Input
The first line gives the number of cards . ()
The second line gives the numbers , , , , written on the cards in each card slot, separated by spaces in order. ()
Output
Print the number of the card slot that Juhyeon, who moves first, must choose first in order to win.
If there are multiple such answers, print the smallest one.
If Juhyeon cannot win no matter which card he chooses, print -1.