시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 512 MB92474358.108%

문제

Mila likes to play Tetris. Today she learnt about a new game that is similar to Tetris. The new game has an infinite rectangular field with the bottom and width equal to $n$, divided into cells of size $1 \times 1$. Unlike real Tetris in this game horizontal pieces with height $1$ and width $x$ consisting of $x$ cells --- of size $1 \times x$, are used. Before the next piece starts to fall, a player can choose it's size $x$ as any integer between $1$ and $n$, inclusive. Pieces can't be rotated, but they can be moved left or right. A piece falls until it reaches an occupied cell under it or the bottom of the field.

Mile doesn't like to leave empty cells under the pieces. Her goal is to fill lower rows of the field in the way that all occupied cells form a rectangle of width $n$.

You are given a field state: $a_1, a_2, \ldots, a_n$, where $a_i$ --- the number of occupied cells in the $i$-th column of the field. In the given field no empty cell is under the occupied one. For example, if sequence $a$ is $3, 2, 4, 2, 2, 4$, the field looks like this:

Find the minimum number of pieces Mila needs to play to occupy the lower rows of the field forming a rectangle of width $n$.

입력

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

The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ --- the number of occupied cells in each column of the field ($0 \le a_i \le 10^9$).

At least on of the $a_i$ is strictly greater than $0$.

출력

Print a single integer: the minimum number of pieces Mila will need to build a rectangle of width $n$.

서브태스크

번호배점제한
18

$n \le 10$, $a_i \le 5$

213

$n \le 100$, $a_i \le 500$

316

$n \le 1000$, $a_i \le 5000$

417

$n \le 1000$, $a_i \le 10^9$

525

$n \le 10^5$, $a_i \le 10^9$, when $n > 1000$ sequence $a$ is generated randomly

621

$n \le 2 \cdot 10^5$, $a_i \le 10^9$

예제 입력 1

6
3 2 4 2 2 4

예제 출력 1

4

힌트

In the example Mile can use the following four pieces:

채점 및 기타 정보

  • 예제는 채점하지 않는다.