403167: GYM101059 B Shift and Push

Memory Limit:256 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

B. Shift and Pushtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

Given two equally sized arrays A and B of size N. A is empty and B has some values.

You need to fill A with an element X such that X belongs to B.

The only operations allowed are:

1. Copy B[i] to A[i].

2. Cyclic shift B by 1 to the the right.

You need to minimise the number of operations.

Input

The first line contains a single positive integer N(1 ≤ N ≤ 106), denoting the size of the arrays.

Next line contains N space separated positive integers denoting the elements of the array B(1 ≤ B[i] ≤ 105).

Output

Output a single integer, denoting the minimum number of operations required.

ExamplesInput
3
1 2 3
Output
5
Input
6
1 1 2 2 3 3
Output
10
Note

In the first test case:

We can have 5 steps as: fill first element, shift, fill second element, shift, fill third element.

Initially, A = [_, _, _], B = [1, 2, 3]

After step 1, A = [1, _, _], B = [1, 2, 3]

After step 2, A = [1, _, _], B = [3, 1, 2]

After step 3, A = [1, 1, _], B = [3, 1, 2]

After step 4, A = [1, 1, _], B = [2, 3, 1]

After step 5, A = [1, 1, 1], B = [2, 3, 1]

Source/Category

加入题单

上一题 下一题 算法标签: