Sorting of Railcars - B
A train is approaching a dead-end from track 1 (refer to the diagram). You are allowed to detach one or more of the leading cars from the train and move them into the dead-end. If desired, the entire train can be moved into the dead-end at once. Afterward, some of these cars can be moved onto track 2. You can then move additional cars into the dead-end and again transfer some of them to track 2. This process can be repeated, ensuring that each car enters the dead-end from track 1 only once and exits to track 2 only once. Cars cannot enter the dead-end from track 2 or exit to track 1. Direct movement from track 1 to track 2 without passing through the dead-end is not possible.
The initial sequence of train cars is given. Using the described operations, you need to arrange the cars in sequential order (starting with car number one, followed by car number two, and so on) as they move on track 2 away from the dead-end. Write a program to determine if this arrangement is feasible.
Input
The first line contains the number of cars in the train n (1 ≤ n ≤ 100). The subsequent line lists the car numbers in the order they appear from the head of the train moving on track 1 towards the dead-end. Each car is uniquely numbered from 1 to n.
Output
Print YES if it is possible to arrange the cars in order from 1 to n, starting from the head of the train as it moves on track 2 away from the dead-end. Otherwise, print NO.