#include <stdio.h>
#define MAX_N  1000
#define INF 200000001

int Partition(int, int);
void QuickSort(int, int);
int Cost(int);
void Swap(int, int);
int MinimumCostOfSort(int);

struct rist
{
  int w;
  int p;
} typedef rist;

rist A[MAX_N];

int main()
{
	int i;
	int n;
	int cost;
	
	scanf("%d", &n);
	for(i = 0; i < n; i++)
	{
		scanf("%d", &A[i].w);
		A[i].p = i;
	}
	
	QuickSort(0, n - 1);
	cost = MinimumCostOfSort(n);

	printf("%d\n", cost);
	
	return 0;
}

int Partition(int p, int r)
{
  int i, j;
  rist tmp;

  i = p - 1;
  for(j = p; j < r; j++)
    {
      if(A[j].w <= A[r].w)
	{
	  i = i + 1;
	  tmp = A[i];
	  A[i] = A[j];
	  A[j] = tmp;
	}
    }
  tmp = A[i + 1];
  A[i + 1] = A[r];
  A[r] = tmp;
  
  return i + 1;
}

void QuickSort(int p, int r)
{
  int q;

  if(p < r)
    {
      q = Partition(p, r);
      QuickSort(p, q - 1);
      QuickSort(q + 1, r);
    }
}

int Cost(int i)
{
	return A[i].w + A[A[i].p].w;
}

void swap(int i1, int i2)
{
	int tmp;
	
	tmp = A[i2].p;
	A[i2].p = A[i1].p;
	A[i1].p = tmp;
}

int MinimumCostOfSort(int n)
{
	int i, j;
	int mini;
	int swap_j;
	int cost = 0;
	
	for(i = 0 ; i < n; i++)
	{
		mini = INF;
		for(j = 0; j < n; j++)
		{
			if(Cost(j) < mini && j != A[j].p)
			{
				mini = Cost(j);
				swap_j = j;
			}
		}
		if(mini != INF)
		{
			cost += Cost(swap_j);
			swap(swap_j, A[swap_j].p);
		}
	}

	return cost;
}