This page is still under construction.

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

Reorganization

Interview

Time limit2sMemory limit512 MB

Summary
Given each employee's rank in ID order, decide whether a binary hierarchy exists where every non-root has a supervisor with a smaller ID and a better rank.
Level

Medium6 of 10

Topics
Greedy, Tree, Stack, Implementation
Solved
No attempts yet

Problem

Alice and Bob run a company together, and they want to reorganize it into a single hierarchy.

There are nn employees. Each employee has a distinct employee ID from 11 to nn, and a distinct integer rank RR; a smaller rank value means a higher rank.

Decide whether the company can be organized so that all of the following hold:

  1. Exactly one employee is the director, who has no supervisor.
  2. Every employee except the director has exactly one direct supervisor, whose employee ID is smaller and whose rank is higher (that is, whose rank value is smaller).
  3. Each employee directly supervises at most 22 people.

Input

The first line contains the number of employees nn (1≤n≤100,0001 \le n \le 100{,}000).

Each of the next nn lines contains one integer: the ii-th of these lines gives the rank RR (1≤R≤10,000,0001 \le R \le 10{,}000{,}000) of the employee whose ID is ii. All rank values are distinct.

Output

Print YES if the company can be reorganized as required, and NO otherwise.

Examples2

  1. Example 1

    Input
    6
    1
    6
    5
    2
    3
    4
    
    Expected output
    NO
    
  2. Example 2

    Input
    6
    1
    6
    2
    3
    4
    5
    
    Expected output
    YES