This page is still under construction.

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

City Planning

Time limit2sMemory limit1024 MB

Summary
Given building heights in a row, delete the fewest buildings so that every pair of remaining rooftops can see each other, meaning the segment between them clears all buildings in between.
Level

Medium7 of 10

Topics
Dynamic programming, Geometry, Greedy, Sorting
Solved
No attempts yet

Problem

The city where Hyunwook lives has NN buildings. All the buildings stand in a single row, numbered 11 through NN from the first building onward. The gap between adjacent buildings is 11 everywhere. If the height of the ii-th building is HiH_i, then when the row of buildings is viewed head-on, the ii-th building can be represented as a line segment of zero thickness joining (i,0)(i, 0) and (i,Hi)(i, H_i).

For any ii-th and jj-th building, the two buildings can see each other's rooftops if the line segment joining the point (i,Hi)(i, H_i) representing the rooftop of the ii-th building and the point (j,Hj)(j, H_j) representing the rooftop of the jj-th building either does not meet any other building, or meets buildings only at their endpoints.

For example, suppose the city has 6 buildings with heights 2, 3, 7, 6, 1, 4 in order from the first building. Then, drawing each building as a line segment, the picture above can be drawn.

In the example, if we try to see the rooftop of the fourth building from the rooftop of the first building, the third building blocks the view as in the left side of the picture above, so the two rooftops cannot see each other. On the other hand, if we try to see the rooftop of the sixth building from the rooftop of the third building, the line joining the two rooftops meets the rooftop of the fourth building but only at an endpoint, so the two rooftops can see each other.

The mayor of the city where Hyunwook lives announced a new city plan one day. According to this plan, every building in the city must be able to see the rooftops of all other buildings from its rooftop.

Unfortunately, some buildings may have to be destroyed to satisfy this city plan. The mayor wants to minimize the number of destroyed buildings. Let us compute the minimum number of buildings that must be destroyed for the new city plan.

Input

The first line gives the number of buildings in the city, NN (1≤N≤20001 \le N \le 2000).

The second line gives the heights HiH_i of the NN buildings in the city, from the leftmost building onward, separated by spaces (1≤Hi≤1091 \le H_i \le 10^9).

Output

On the first line, print the minimum number of buildings that must be destroyed to satisfy the condition.

Examples1

  1. Example 1

    Input
    6
    2 3 7 6 1 4
    
    Expected output
    3