//#define MYDEBUG
//
#include <bits/stdc++.h>
#ifdef MYDEBUG
#define dbp(x) cout<<#x<<": "<<x<<endl
#define dbp2(x,y) cout<<#x<<","<<#y<<": "<<x<<","<<y<<endl
#define dbp3(x,y,z) cout<<#x<<","<<#y<<","<<#z<<": "<<x<<","<<y<<","<<z<<endl
#define dbp4(w,x,y,z) cout<<#w<<","<<#x<<","<<#y<<","<<#z<<": "<<w<<","<<x<<","<<y<<","<<z<<endl
#define ifcin(x) std::ifstream cin(x)
#else
#define dbp(x)
#define dbp2(x,y)
#define dbp3(x,y,z)
#define dbp4(w,x,y,z)
#define ifcin(x)
#endif
#define ll long long
#define all(x) x.begin(), x.end()
#define rep(i, from, to) for(int i=from; i<to; ++i)
#define REP(i, from, to) for(int i=from; i<=to; ++i)
using namespace std;
template<typename T>
ostream& operator<<(ostream& out, const vector<T>& v) {
	out << "[";
	size_t last = v.size() - 1;
	for (size_t i = 0; i < v.size(); ++i) {
		out << v[i];
		if (i != last) {
			out << ",";
		}
	}
	out << "]";
	return out;
}
template<typename T>
ostream& operator<<(ostream& out, const vector<vector<T> >& v) {
	for (size_t i = 0; i < v.size(); ++i) {
		out << v[i] << endl;
	}
	return out;
}
template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& p) {
	out << "{" << p.first << "," << p.second << "}";
	return out;
}

static const int MAX_N = 1000;
static const int MAX_W = 10000 + 1;
static const int INF = 1000000;
int n;

void merge(vector<pair<int, int> > &v, int left, int mid, int right) {
	int n1 = mid - left, n2 = right - mid;
	vector<pair<int, int> > l = vector<pair<int, int> >(n1 + 1);
	vector<pair<int, int> > r = vector<pair<int, int> >(n2 + 1);
	rep(i,0,n1)
	{
		l[i] = v[left + i];
	}
	rep(j,0,n2)
	{
		r[j] = v[mid + j];
	}
	l[n1] = r[n2] = make_pair(INF, right);
	int i = 0, j = 0;
	rep(k,0,right-left)
	{
		if (l[i].first <= r[j].first) {
			v[k + left] = l[i++];
		} else {
			v[k + left] = r[j++];
		}
	}
}

void mergeSort(vector<pair<int, int> > &v, int left, int right) {
	if (left + 1 < right) {
		int mid = (left + right) / 2;
		mergeSort(v, left, mid);
		mergeSort(v, mid, right);
		merge(v, left, mid, right);
	}
}

void getCycle(vector<vector<int> > &cyc, vector<pair<int, int> > &a) {
	vector<bool> used(MAX_W);
	fill(all(used), false);
	rep(i,0,(int)a.size())
	{
		if (used[a[i].first]) {
			continue;
		}
		used[a[i].first] = true;
		cyc.push_back(vector<int>());
		int j = a[i].second, tail = cyc.size() - 1;
		while (true) {
			used[a[j].first] = true;
			cyc[tail].push_back(a[j].first);
			j = a[j].second;
			if (a[j].first == cyc[tail][0]) { //1巡した
				break;
			}
		}
		sort(all(cyc[tail]));
	}
}

int getSmallerCost(vector<int> v, int minW) {
	int n = v.size();
	if (n == 1) {
		return 0;
	}
	int c1 = 0, c2 = 0;
	rep(i,0,n)
	{
		c1 += v[i];
		c2 += v[i];
	}
	c1 += (n - 2) * v[0];
	c2 += (n - 2) * v[0]; //あとから差分を加減して調整
	//minWを外から借りてくる
	//minW <= v[0]
	//得: (n-2) * (v[0] - minW)
	//損: 2 * (v[0] + minW)
	c2 -= (n - 1) * (v[0] - minW);	//借り物なのでn-2より1回多い
	c2 += 2 * (v[0] + minW);
	dbp3(v, c1, c2);
	return min(c1, c2);
}

void solve() {
	scanf("%d", &n);
	//pair: 値, 元の位置
	vector<pair<int, int> > a = vector<pair<int, int> >(n);
	vector<vector<int> > cycle = vector<vector<int> >();
	int minW = MAX_W;
	rep(i,0,n)
	{
		a[i] = make_pair(0, i);
		scanf("%d", &a[i].first);
		minW = min(minW, a[i].first);
	}dbp(a);
	mergeSort(a, 0, n);
	dbp(a);
	getCycle(cycle, a);
	dbp(cycle);
	int sum = 0;
	rep(i,0,(int)cycle.size())
	{
		sum += getSmallerCost(cycle[i], minW);
	}
	printf("%d\n", sum);
}

void printTestCase() {
	int n = 1000;
	int ins = 0;
	printf("%d\n", n);
	rep(i,0,n)
	{
		if (ins++ > 0) {
			printf(" ");
		}
		printf("%d", (n - i));
	}
}

int main() {
	solve();
//	printTestCase();
	return 0;
}



Measured memory_bytes: 3308