This page is still under construction.

Parts of this page are still being built. What you see may change.

Card Sandcastle

Time limit1sMemory limit1024 MB

Summary
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 NN cards with positive integers between 11 and 55 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.

  1. Prepare NN card slots and number them from 11 to NN.
  2. Place one card with a positive integer between 11 and 55 written on it on each card slot.
  3. The two players take turns.
  4. On your turn, choose a non-empty card slot among the NN card slots. If the chosen card slot has number ii and the number written on the card in that slot is cic_i, you must take all cards in card slots ii, i+1i+1, ......, i+cii+c_i, including the chosen slot. If any of those card slots does not exist or is empty, you cannot choose card slot ii.
  5. 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 NN. (1≤N≤2001 \le N \le 200)

The second line gives the numbers c1c_1, c2c_2, c3c_3, ......, cNc_N written on the cards in each card slot, separated by spaces in order. (1≤ci≤51 \le c_i \le 5)

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.

Examples3

  1. Example 1

    Input
    8
    1 2 1 2 1 2 1 2
    
    Expected output
    4
    
  2. Example 2

    Input
    7
    1 1 1 1 1 1 1
    
    Expected output
    1
    
  3. Example 3

    Input
    5
    1 1 1 1 1
    
    Expected output
    -1