/*一道USACO 2007的题...很好的一道利用置换群去排序的题*/
#include<stdio.h>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
#define M 1000
using namespace std;
struct cow
{
int weight;
int id;
};
cow c[M];
int cmp(const cow a,const cow b)
{
return a.weight<b.weight;
}
int main()
{
freopen("3270.txt","r",stdin);
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&c[i].weight);
c[i].id=i;
}
sort(c,c+n,cmp);
int m=c[0].weight;
int ans=0;
for(int i=0;i<n;i++)
{
if(c[i].id!=-1)
{
int j=c[i].id;
int w=c[i].weight;
int k=1;
while(j!=i)
{
ans+=c[j].weight;
int nxt=c[j].id;
c[j].id=-1;
j=nxt;
k++;
}
c[i].id=-1;
ans+=min((k-1)*w,(k-1)*m+2*(m+w));
}
}
printf("%d\n",ans);
return 0;
}