This page is still under construction.

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

Improvements

Time limit1sMemory limit256 MB

Summary
Reposition ships on a line from a station so no two ropes joining consecutive ships cross, keeping as many ships as possible in place.
Level

Hard8 of 10

Topics
Dynamic programming, Combinatorics, Segment tree
Solved
No attempts yet

Problem

Son Halo owns nn spaceships, numbered from 1 to nn, and one space station. The station and the ships all sit on a single line. Ship ii is xix_i meters from the station, and every xix_i is positive, so all ships are on the same side of the station. All xix_i are distinct. The station has number 0, and x0=0x_0 = 0.

Every two ships with consecutive numbers are joined by a rope, and the first ship is joined to the station. Rope ii (for 1≤i≤n1 \le i \le n) joins ship ii and ship i−1i-1, so rope 1 joins the first ship to the station.

Write xkmin⁡=min⁡(xk−1,xk)x_k^{\min} = \min(x_{k-1}, x_k) and xkmax⁡=max⁡(xk−1,xk)x_k^{\max} = \max(x_{k-1}, x_k). Son Halo considers that rope ii and rope jj intersect when the segments [ximin⁡,ximax⁡][x_i^{\min}, x_i^{\max}] and [xjmin⁡,xjmax⁡][x_j^{\min}, x_j^{\max}] have a common interior point and neither segment is completely contained in the other, that is, when one of the following holds.

{ximin⁡<xjmin⁡<ximax⁡<xjmax⁡xjmin⁡<ximin⁡<xjmax⁡<ximax⁡\begin{cases} x_i^{\min} < x_j^{\min} < x_i^{\max} < x_j^{\max} \\ x_j^{\min} < x_i^{\min} < x_j^{\max} < x_i^{\max} \end{cases}

Son Halo wants to rearrange the ships so that no two ropes intersect. He is lazy, so he wants the number of ships that remain at their original position xix_i to be as large as possible. After the rearrangement all ships must still be on the same side of the station and at distinct positions. A ship can be placed at any real position.

Find the largest number of ships that can remain at their initial positions.

Input

The first line contains nn (1≤n≤200 0001 \le n \le 200\,000), the number of ships. The second line contains nn distinct integers xix_i (1≤xi≤n1 \le x_i \le n), the initial positions of the ships.

Output

Print one integer, the largest number of ships that can remain at their initial positions.

Note

In the first example Son Halo can move the second ship to a position between the first ship and the third ship, and the other 3 ships keep their places. In the second example no two ropes intersect, so all 4 ships can stay where they are.

Examples2

  1. Example 1

    Input
    4
    1 3 2 4
    
    Expected output
    3
    
  2. Example 2

    Input
    4
    1 4 2 3
    
    Expected output
    4