#P2599. Dequeue Stone Game

    ID: 15868 Type: Default 1000ms 256MiB

Dequeue Stone Game

Dequeue Stone Game

This problem introduces a new variant of the stone removal game. There are \( n \) piles of stones arranged in a row. Two players take turns. On each turn, a player must remove a positive number of stones from either the leftmost or the rightmost pile. They may remove fewer than all the stones in that pile, but they must remove at least one stone. If a player removes exactly the number of stones present in a pile, that pile is removed from the row. The player who is unable to make a move (because there are no stones left) loses.

Orez has studied Nim and its variations and now wonders: given any initial configuration, does there exist a winning strategy for the first player? Intuitively, if the configuration is asymmetric (when n > 1), the first player can mirror the moves to force a win; however, if the configuration is symmetric, the second player can adopt a mirroring strategy. Special care must be taken when \( n = 1 \) because a lone pile is always a win for the first player as they can take all stones immediately.

Formally, if \( n = 1 \) then the answer is YES. For \( n > 1 \), if the current configuration a1, a2, \(\dots\), an is symmetric (i.e. ai = an-i+1 for every valid i), then the first player loses and the output should be NO; otherwise, the output should be YES.

inputFormat

The first line contains an integer \( n \) (1 \( \le n \le 105 \)), the number of piles. The second line contains n positive integers a1, a2, \(\dots\), an (1 \( \le ai \le 109 \)) representing the number of stones in each pile from left to right.

outputFormat

Output a single line containing YES if the first player has a winning strategy, otherwise output NO.

sample

1
5
YES