#D10940. Folding Paper
Folding Paper
Folding Paper
Problem statement
There is a rectangular piece of paper divided in a grid with a height of squares and a width of squares. The integer is written in the cell of the line from the top and the column from the left counting from . An example of is shown in the figure below.
AOR Ika-chan performed the following operations on this paper in order.
- Fold it repeatedly along the dividing line of the square until it reaches the area of square. The folding lines, mountains and valleys, and the order at this time are arbitrary. You can fold it in any way that does not tear the paper.
- Cut off the edge and divide it into sheets of paper.
- Turn over from the top to create a sequence in which the written integers are arranged.
For example, if you fold it and then cut it as shown in the figure below, you will get as . In this way, it is possible to fold it so that it can be inserted between them.
You received a sequence of from AOR Ika-chan. However, I don't trust AOR Ika-chan, so I want to make sure this is the real thing. Write a program that outputs "YES" if there is a paper folding method that matches , and "NO" if not.
Input constraints
contains integers from to in increments of
sample
Sample input 1
14 0 1 2 3
Sample output 1
YES YES
Sample input 2
twenty three 4 3 0 5 2 1
Sample output 2
YES YES
This is an example in the problem statement.
Sample input 3
14 0 2 1 3
Sample output 3
NO
Sample input 4
twenty two 0 1 3 2
Sample output 4
YES YES
Fold it in half for .
input
output
Print "YES" or "NO" on the line.
Example
Input
1 4 0 1 2 3
Output
YES
inputFormat
outputFormat
outputs "YES" if there is a paper folding method that matches , and "NO" if not.
Input constraints
contains integers from to in increments of
sample
Sample input 1
14 0 1 2 3
Sample output 1
YES YES
Sample input 2
twenty three 4 3 0 5 2 1
Sample output 2
YES YES
This is an example in the problem statement.
Sample input 3
14 0 2 1 3
Sample output 3
NO
Sample input 4
twenty two 0 1 3 2
Sample output 4
YES YES
Fold it in half for .
input
output
Print "YES" or "NO" on the line.
Example
Input
1 4 0 1 2 3
Output
YES
样例
1 4
0 1 2 3
YES