#P10370. Optimal Sequence Value

    ID: 12376 Type: Default 1000ms 256MiB

Optimal Sequence Value

Optimal Sequence Value

We are given a natural number sequence (a_1,a_2,\dots,a_n) with (0 \le a_i \le 10^9). Define the binary operation (\operatorname{mex}(x,y)) as the smallest natural number (i.e. non‐negative integer) missing from the set ({x,y}). For example, (\operatorname{mex}(1,5)=0) and (\operatorname{mex}(3,0)=1).

An operation on a sequence (a) is defined as replacing (a_1,a_2,\dots,a_n) by a new sequence of length (n-1) given by

[ a'i = \operatorname{mex}(a_i,a{i+1}) \quad \text{for } 1\le i\le n-1, ]

If we perform (n-1) such operations continuously, only one number remains. We define the final number as the value of the sequence, i.e. (f(a)).

For any natural number sequence (b=b_1, b_2, \dots, b_n) of length (n) (note that there is no upper bound for the (b_i)), the maximum possible final value is achieved when (f(b)=2) (this can be verified by noting that for any pair (x,y), (\operatorname{mex}(x,y)\in{0,1,2}) and hence for (n\ge2) the final value is always either 0, 1, or 2).

Your task is: Given an integer (n) and a sequence (a) of length (n) (with (0\le a_i\le10^9)), determine whether (a) is optimal in the sense that its value (f(a)) is at least as large as the final value (f(b)) of any length-(n) natural number sequence (b).

In other words, for (n\ge2) the answer is YES if (f(a)=2) and NO if (f(a)) is less than 2. (For (n=1), note that since a single number is its own value and natural numbers are unbounded, the answer is always NO.)

inputFormat

The first line contains a single integer \(n\) (\(n\ge 1\)).

The second line contains \(n\) space-separated integers \(a_1,a_2,\dots,a_n\) representing the sequence \(a\).

outputFormat

Output a single line: YES if \(f(a)\ge f(b)\) for every natural sequence \(b\) of length \(n\) (which for \(n\ge2\) is equivalent to \(f(a)=2\)); otherwise, output NO.

sample

2
0 1
YES