PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000214. Xóa ít nhất để tạo dãy hình núi (Minimum Number of Removals to Make Mountain Array)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Xóa ít nhất để tạo dãy hình núi (Minimum Number of Removals to Make Mountain Array)

Minimum Number of Removals to Make Mountain Array

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

A sequence b1,b2,…,bmb_1,b_2,\ldots,b_m is a mountain if m≥3m\ge 3 and there is a peak position pp, with 1<p<m1<p<m, satisfying

b1<b2<⋯<bp>bp+1>⋯>bm.b_1<b_2<\cdots<b_p>b_{p+1}>\cdots>b_m.

Given a1,a2,…,ana_1,a_2,\ldots,a_n, you may remove elements anywhere without changing the relative order of the remaining elements. Find the smallest number of deletions that leaves a mountain. A feasible result is guaranteed.

Input

The first line contains the integer nn. The second line contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n.

Output

Print one integer: the minimum number of deletions.

Subtasks

  • Subtask 1 (20 points): 3≤n≤203\le n\le 20; 1≤ai≤1091\le a_i\le 10^9 for every 1≤i≤n1\le i\le n; a mountain subsequence exists.
  • Subtask 2 (30 points): 3≤n≤2003\le n\le 200; 1≤ai≤1091\le a_i\le 10^9 for every 1≤i≤n1\le i\le n; a mountain subsequence exists.
  • Subtask 3 (50 points): 3≤n≤10003\le n\le 1000; 1≤ai≤1091\le a_i\le 10^9 for every 1≤i≤n1\le i\le n; a mountain subsequence exists.

Examples

Input

3
1 3 1

Output

0

Explanation

The sequence rises from 11 to 33 and falls to 11. Its peak is at position 22, with a nonempty side on each side, so no deletion is necessary.

Input

8
2 1 1 5 6 2 3 1

Output

3

Explanation

Delete positions 1,2,61,2,6 to retain [1,5,6,3,1][1,5,6,3,1]. With peak 66, at most two elements can be retained on either side. Peak 55 allows at most one element before it and two decreasing elements after it; other peaks are smaller and cannot give a longer chain. The maximum retained length is five, so three deletions are necessary.