# include <iostream>
# include <algorithm>
#include <array>
# include <cassert>
#include <cctype>
#include <climits>
#include <numeric>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <tuple>
# include <utility>
# include <stack>
# include <queue>
# include <list>
# include <bitset>
# include <complex>
# include <chrono>
# include <random>
# include <limits.h>
# include <unordered_map>
# include <unordered_set>
# include <deque>
# include <cstdio>
# include <cstring>
#include <stdio.h>
#include<time.h>
#include <stdlib.h>
#include <cstdint>
#include <cfenv>

//#include <bits/stdc++.h>
using namespace std;
using LL = int;
using ULL = unsigned long long;
long long MOD = 1000000000 + 7;
constexpr long long INF = numeric_limits<LL>::max();
const double PI = acos(-1);
#define fir first
#define sec second
#define thi third
#define debug(x) cerr<<#x<<": "<<x<<'\n'
typedef pair<LL, LL> Pll;
typedef pair<LL, pair<LL, LL>> Ppll;
typedef pair<LL, pair<LL, bitset<100001>>> Pbll;
typedef pair<LL, pair<LL, vector<LL>>> Pvll;
typedef pair<LL, LL> Vec2;
struct Tll { LL first, second, third; };
struct Fll { LL first, second, third, fourd; };
typedef pair<LL, Tll> Ptll;
#define rep(i,rept) for(LL i=0;i<rept;i++)
#define Mfor(i,mf) for(LL i=mf-1;i>=0;i--)
LL h, w, n, m, k, t, s, q, last, cnt, sum, ans, dp[10000], a[2000000], b[2000000];
string str, ss;
bool f[1100][1100];
char c;
int di[4][2] = { { 0,1 },{ 1,0 },{ 0,-1 },{ -1,0 } };
struct Edge { LL to, cost; };
vector<Edge>vec[200000];
vector<LL>v,pick[200000];
map<string, vector<LL>>ma;
multiset<LL>st[3];
void YN(bool f) {
	if (f)
		cout << "YES" << endl;
	else
		cout << "NO" << endl;
}
void yn(bool f) {
	if (f)
		cout << "Yes" << endl;
	else
		cout << "No" << endl;
}


vector<int> kosaraju() {
	int sz = 0;
	vector<Edge> rg[50000];
	vector<int> stk, cmp(n, -1), added(n), visited(n), ord(n);
	rep(i, n) {
		sz += vec[i].size();
		for (const Edge &e : vec[i])
			rg[e.to].push_back(Edge{ (int)i, e.cost });
	}
	stk.resize(n + sz); sz = 0;
	for (int i = 0; i < n; i++) {
		if (visited[i]) continue;
		int s = 0; stk[s++] = i;
		while (s != 0) {
			int v = stk[s - 1];
			visited[v] = true;
			bool pushed = false;
			for (auto &e : vec[v]) {
				int dst = e.to;
				if (!visited[dst]) { stk[s++] = dst; pushed = true; }
			}
			if (pushed) continue;
			int t = stk[s - 1];
			if (!added[t]) { added[t] = true; ord[n - ++sz] = t; }
			--s;
		}
	}
	int k = 0;
	for (int &u : ord) {
		if (cmp[u] != -1) continue;
		int s = 0; stk[s++] = u;
		while (s != 0) {
			int v = stk[--s]; cmp[v] = k;
			for (auto &e : rg[v]) {
				int d = e.to;
				if (cmp[d] == -1) stk[s++] = d;
			}
		}
		++k;
	}
	return cmp;
}

map<int, int> zip;
int unzip[200000];

int compress(vector<int> &x) {
	sort(x.begin(), x.end());
	x.erase(unique(x.begin(), x.end()), x.end());
	for (int i = 0; i < x.size(); i++) {
		zip[x[i]] = i;
		unzip[i] = x[i];
	}
	return x.size();
}

int main() {
	cin >> n;
	rep(i, n) {
		cin >> a[i];
		v.push_back(a[i]);
	}
	compress(v);
	v.clear();
	rep(i,n){
		vec[i].push_back(Edge{ zip[a[i]],1 });
	}
	v=kosaraju();
	rep(i, v.size()) {
		pick[v[i]].push_back(i);
	}
	rep(i, n) {
		sum = 0;
		LL mn = INF;
		if (pick[i].size() > 1) {
			rep(j, pick[i].size()) {
				sum += unzip[pick[i][j]];
				mn = min(mn, pick[i][j]);
			}
			ans += min(sum + unzip[mn] * (pick[i].size() -	2),sum+unzip[0] * (pick[i].size()+1)+unzip[mn]);
		}
	}
	cout << ans << endl;
	return 0;
}



Measured memory_bytes: 13928