You are given $n$ integers $w_i (i = 0, 1, ..., n-1)$ to be sorted in ascending order. You can swap two integers $w_i$ and $w_j$. Each swap operation has a cost, which is the sum of the two integers $w_i + w_j$. You can perform the operations any number of times.

Write a program which reports the minimal total cost to sort the given integers.

Constraints

* $1 \leq n \leq 1,000$
* $0 \leq w_i\leq 10^4$
* $w_i$ are all different

Input

In the first line, an integer $n$ is given. In the second line, $n$ integers $w_i (i = 0, 1, 2, ... n-1)$ separated by space characters are given.

Output

Print the minimal cost in a line.

Examples

Input

5
1 5 3 4 2


Output

7


Input

4
4 3 2 1


Output

10
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<math.h>
#define Lens 1000
#define Maxi 10005

using namespace std;
int N,mini;
int A[Lens], B[Lens],C[Maxi];
bool V[Lens];

int solve()
{
	int total=0;
	for (int i = 0; i < N; i++)
	{
		B[i] = A[i];
		V[i] = false;
	}
	sort(B, B + N);
	mini = B[0];
	for (int i = 0; i < N; i++)
	{
		C[B[i]] = i;
		if (B[i] == A[i]) V[i] = true;
	}
	for (int i = 0; i < N; i++)
	{
		while (V[i] == false)
		{
			int tmp = A[i];
			int min_tmp;
			int next = C[A[i]];
			int sum =A[i];
			int n = 0;
			V[i] = true;
			min_tmp= min(A[i], A[next]);
			while (!V[next])
			{
				n++;
				V[next] = true;
				sum = sum + A[next];
				if (A[next] < min_tmp) 
					min_tmp = A[next];
				next= C[A[next]];
			}
			total = total + min(sum + (n - 1)*min_tmp, sum + min_tmp + (n + 2)*mini);
		}
		
	}
	return total;
}

int main()
{
	cin >> N;
	for (int i = 0; i < N; i++)
	{
		cin >> A[i];
	}
	int s = solve();
	cout << s << endl;
	return 0;
}
