Tidying the Row
时间限制:C/C++/Rust/Pascal 1秒,其他语言2秒
空间限制:C/C++/Rust/Pascal 512 M,其他语言1024 M
64bit IO Format: %lld

题目描述

In preparation for an upcoming dance competition, the children are practicing intensely. Just before they are set to perform, they realize their line is not properly arranged. If they head out as they are, the judges will give them a low score due to the lack of neatness.

To avoid losing the competition, they decide to make a local adjustment. The current formation can be viewed as a sequence A of length n, where A_i represents the height of the i-th child. The neatness of the formation is defined as the length of the longest contiguous subsegment consisting of children with identical heights. They are allowed to perform at most one adjustment operation: Select a subsegment [L,R] of children. Reorder the children within this subsegment in any order. The time required for this operation is R-L+1.

The children want to know: what is the minimum time to achieve the maximum possible neatness using at most one adjustment?

输入描述:

The first line contains an integer n (1\le n \le 2\cdot 10^5), representing the length of the sequence.

The second line contains n integers A_i (1\le A_i \le 10^6), representing the height of the i-th child.

输出描述:

Output a single integer representing the minimum time required to reach the maximum possible neatness.
示例1

输入

复制
6
1 1 4 5 1 4

输出

复制
3

说明

For Example 1, a feasible optimal solution is to select the interval [3,5] for adjustment, resulting in a minimum time of 3.

示例2

输入

复制
9
10 2 5 10 2 6 10 2 7

输出

复制
6

说明

For Example 2, a feasible optimal solution is to select the interval [3,8] for adjustment, resulting in a minimum time of 6.