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 <map>
#include <queue>
#include <cstdio>
#include <vector>
#include <utility>
#include <algorithm>

using namespace std;

int sorting(vector<short> v)
{
	map<vector<short>, int> d;
	
	vector<short> sorted(v.begin(), v.end());
	
	sort(v.begin(), v.end());
	
	queue<pair<vector<short>, int> > que;
	
	que.push(make_pair(v, 1)); d[v] = 1; // pair(vector, cost + 1)
	
	while(!que.empty())
	{
		vector<short> v1 = que.front().first;
		
		int cost = que.front().second; que.pop();
		
		for(int i = 0; i < v.size(); i++)
		{
		    for(int j = i + 1; j < v.size(); j++)
		    {
		        vector<short> v2(v1.begin(), v1.end());
		        
		        int cost2 = cost + v1[i] + v1[j];
		        
		        swap(v2[i], v2[j]);
		        
		        if(d[v2] == 0 || d[v2] > cost2)
		        {
		            d[v2] = cost2;
		            
		            que.push(make_pair(v2, cost2));
		        }
		    }
		}
	}
	
	return d[sorted] - 1;
}

int main()
{
	vector<short> v; int n;
	
	scanf("%d", &n); v = vector<short>(n);
	
	for(int i = 0; i < n; i++) { cin >> v[i]; }
	
	printf("%d\n", sorting(v));
	
	return 0;
}