Codeforces Round 800 (Div. 1)
E
Outermost Maximums
Codeforces
3400
原题
← DF →
翻译中…

Yeri has an array of $n + 2$ non-negative integers : $a_0, a_1, ..., a_n, a_{n + 1}$.

翻译中…

We know that $a_0 = a_{n + 1} = 0$.

翻译中…

She wants to make all the elements of $a$ equal to zero in the minimum number of operations.

翻译中…

In one operation she can do one of the following:

翻译中…
  • Choose the leftmost maximum element and change it to the maximum of the elements on its left.
  • Choose the rightmost maximum element and change it to the maximum of the elements on its right.
翻译中…

Help her find the minimum number of operations needed to make all elements of $a$ equal to zero.

翻译中…

Input

翻译中…

The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$).

翻译中…

The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le n$).

翻译中…

Output

翻译中…

Print a single integer  — the minimum number of operations needed to make all elements of $a$ equal to zero.

翻译中…

Note

翻译中…

In the first sample, you get $\langle 1, \underline{1}, 2, 4, 0, 2 \rangle$ by performing the first operation and $\langle 1, 4, 2, \underline{2}, 0, 2 \rangle$ by performing the second operation.

翻译中…

One way to achieve our goal is shown below. (The underlines show the last change.) $\langle 1, 4, 2, 4, 0, 2 \rangle \to \langle 1, 4, 2, \underline{2}, 0, 2 \rangle \to \langle 1, \underline{1}, 2, 2, 0, 2 \rangle \to \langle 1, 1, 2, 2, 0, \underline{0} \rangle \to \langle 1, 1, 2, \underline{0}, 0, 0 \rangle \to \langle 1, 1, \underline{0}, 0, 0, 0 \rangle \to \langle \underline{0}, 1, 0, 0, 0, 0 \rangle \to \langle 0, \underline{0}, 0, 0, 0, 0 \rangle $

翻译中…

In the third sample each element is already equal to zero so no operations are needed.

翻译中…

样例

Input #1
6
1 4 2 4 0 2
Output #1
7
InputCopy OutputCopy
Input #2
5
1 3 5 4 2
Output #2
9
InputCopy OutputCopy
Input #3
4
0 0 0 0
Output #3
0
InputCopy OutputCopy
题目信息
题号
1693E
时间限制
2 s
内存限制
256 MB
标签
数据结构
贪心

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。