403167: GYM101059 B Shift and Push
Description
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.
InputThe 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).
OutputOutput a single integer, denoting the minimum number of operations required.
ExamplesInput3Output
1 2 3
5Input
6Output
1 1 2 2 3 3
10Note
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]