#include <stdio.h>
#include <sstream>
#include <string.h>
#include <vector>
#include <map>
#include <algorithm>
#include <utility>
#include <set>
#include <cctype>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <limits>
#include <iomanip>
#include <ctype.h>
#define _USE_MATH_DEFINES
#include <iostream>
#include <math.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, char> pic;
#define bit(x,v) ((ll)x << v)

const ll INF = 1000000007;
const int MAX = 210000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
	fac[0] = fac[1] = 1;
	finv[0] = finv[1] = 1;
	inv[1] = 1;
	for (int i = 2; i < MAX; i++) {
		fac[i] = fac[i - 1] * i % MOD;
		inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
		finv[i] = finv[i - 1] * inv[i] % MOD;
	}
}

// 二項係数計算
long long COM(int n, int k) {
	if (n < k) return 0;
	if (n < 0 || k < 0) return 0;
	return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

ll gcd(ll a, ll b) {
	if (b == 0) return a;
	return gcd(b, a % b);
}
int pr[100010];
void uini(int n) {
	for (size_t i = 0; i <= n; i++)
	{
		pr[i] = i;
	}
}

int parent(int x) {
	if (x == pr[x]) return x;
	return pr[x] = parent(pr[x]);
}

bool unit(int x, int y) {
	int px = parent(x);
	int py = parent(y);

	if (px == py) return false;
	if (px < py) {
		pr[py] = px;
	}
	else {
		pr[px] = py;
	}
	return true;
}

ll bit[200010];
int max_n = 200000;
int pm = 0;
void add(int x) {

	while (max_n >= x)
	{
		bit[x]++;
		x += x & -x;
	}
}
void sub(int x) {
	while (max_n >= x)
	{
		bit[x]--;
		x += x & -x;
	}
}
ll merge(ll *a, int left, int mid, int right) {
	ll n1 = mid - left;
	ll n2 = right - mid;
	vector<int> L(n1 + 1);
	vector<int> R(n2 + 1);
	for (size_t i = 0; i < n1; i++)
	{
		L[i] = a[left + i];
	}
	for (size_t i = 0; i < n2; i++)
	{
		R[i] = a[mid + i];
	}

	L[n1] = INF;
	R[n2] = INF;
	ll i = 0;
	ll j = 0;
	ll r = 0;
	for (size_t k = left; k < right; k++)
	{
		if (L[i] <= R[j]) {
			a[k] = L[i];
			i++;
		}
		else {
			a[k] = R[j];
			r += n1 - i;
			j++;
		}
	}
	return r;
}
ll merge2(pair<int,char> *a, int left, int mid, int right) {
	ll n1 = mid - left;
	ll n2 = right - mid;
	vector<pair<int, char>> L(n1 + 1);
	vector<pair<int, char>> R(n2 + 1);
	for (size_t i = 0; i < n1; i++)
	{
		L[i] = a[left + i];
	}
	for (size_t i = 0; i < n2; i++)
	{
		R[i] = a[mid + i];
	}

	L[n1] = make_pair(INF,' ');
	R[n2] = make_pair(INF,' ');
	ll i = 0;
	ll j = 0;
	ll r = 0;
	for (size_t k = left; k < right; k++)
	{
		if (L[i].first <= R[j].first) {
			a[k] = L[i];
			i++;
		}
		else {
			a[k] = R[j];
			r += n1 - i;
			j++;
		}
	}
	return r;
}
ll mergeSort2(pair<int, char> *a, int left, int right) {
	ll res = 0;
	if (left + 1 < right) {
		int mid = (left + right) / 2;
		res = mergeSort2(a, left, mid);
		res += mergeSort2(a, mid, right);
		res += merge2(a, left, mid, right);
	}
	return res;
}
ll mergeSort(ll *a, int left, int right) {
	ll res = 0;
	if (left + 1 < right) {
		int mid = (left + right) / 2;
		res = mergeSort(a, left, mid);
		res += mergeSort(a, mid, right);
		res += merge(a, left, mid, right);
	}
	return res;
}
int partition(pair<int,char> *a, int p, int r) {
	pair<int, char> x = a[r];
	int i = p - 1;
	for (size_t j = p; j < r; j++)
	{
		if (a[j].first <= x.first) {
			i++;
			swap(a[i], a[j]);
		}
	}
	swap(a[i + 1], a[r]);
	return i + 1;
}
void quick(pair<int, char> *a, int p, int r) {
	if (p < r) {
		int q = partition(a, p, r);
		quick(a, p, q - 1);
		quick(a, q + 1, r);
	}
}
int n;
ll calc(vector<int> a, vector<int> b,vector<int> ind) {
	ll res = 0;
	vector<int> at(a.begin(), a.end());
	vector<int> bt(b.begin(), b.end());
	vector<int> indt(ind.begin(), ind.end());
	for (size_t i = 0; i < n; i++)
	{
		ll tm = 0;
		while (a[i] != b[i]) {

			int v = b[i];
			int fr = ind[v];
			int com = b[fr];
			int to = ind[com];
			swap(ind[v], ind[com]);
			tm += v + com;
			swap(a[fr], a[to]);
		}

		ll t2 = 0;
		if (i > 0 && at[i] != bt[i]) {
			t2 += at[indt[bt[i]]] + at[0];
			int id = bt[i];
			swap(at[indt[id]], at[0]);
			swap(indt[bt[i]], indt[b[0]]);
		}
		while (at[0] != bt[0]) {
			int v = bt[0];
			int fr = indt[v];
			int com = bt[fr];
			int to = indt[com];
			swap(indt[v], indt[com]);
			t2 += v + com;
			swap(at[fr], at[to]);
		}

		res += min(tm, t2);
	}
	return res;
}
ll calcz(vector<int> a, int *b, vector<int> ind) {
	ll res = 0;
	for (size_t i = 0; i < n; i++)
	{

		if (i > 0 && a[0] == 0 && a[i] != b[i]) {
			res += a[ind[b[i]]];
			int id = b[i];
			swap(a[ind[id]], a[0]);
			swap(ind[b[i]], ind[0]);
		}
		while (a[0] != b[0]) {
			int v = b[0];
			int fr = ind[v];
			int com = b[fr];
			int to = ind[com];
			swap(ind[v], ind[com]);
			res += v + com;
			swap(a[fr], a[to]);
		}
	}
	return res;
}
void solv() {
	cin >> n;
	vector<int> a(n);
	vector<int> b(n);
	vector<int> ind(10010);
	for (size_t i = 0; i < n; i++)
	{
		cin >> b[i];
		a[i] = b[i];
		ind[a[i]] = i;
	}
	sort(b.begin(), b.end());
	ll r1 = calc(a, b,ind);
	cout << r1 << endl;

}
int main() {
	//	COMinit();
	solv();
	return 0;
}



Measured memory_bytes: 3296