This page is still under construction.

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

Tetris Remastered

Time limit1sMemory limit512 MB

Summary
Given column heights with no gaps underneath, drop 1-by-x horizontal pieces to finish a full n-wide rectangle using the fewest pieces.
Level

Medium6 of 10

Topics
Greedy, Array, Implementation, Simulation
Solved
No attempts yet

Problem

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 nn, divided into cells of size 1×11 \times 1. Unlike real Tetris, this game uses horizontal pieces with height 11 and width xx consisting of xx cells, that is, pieces of size 1×x1 \times x. Before the next piece starts to fall, a player can choose its size xx as any integer between 11 and nn, 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.

Mila 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 nn.

You are given a field state: a1,a2,…,ana_1, a_2, \ldots, a_n, where aia_i is the number of occupied cells in the ii-th column of the field. In the given field no empty cell is under the occupied one. For example, if sequence aa is 3,2,4,2,2,43, 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 nn.

Input

The first line contains a single integer nn, the width of the field (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5).

The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n, the number of occupied cells in each column of the field (0≤ai≤1090 \le a_i \le 10^9).

At least one of the aia_i is strictly greater than 00.

Output

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

Notes

In the example Mila can use the following four pieces:

Examples1

  1. Example 1

    Input
    6
    3 2 4 2 2 4
    
    Expected output
    4