/********   All Required Header Files ********/
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <queue>
#include <deque>
#include <bitset>
#include <iterator>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <numeric>
#include <utility>
#include <limits>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <unordered_set>

using namespace std;

/*******  All Required define Pre-Processors and typedef Constants *******/
#define SCD(t) scanf("%d",&t)
#define SCLD(t) scanf("%ld",&t)
#define SCLLD(t) scanf("%lld",&t)
#define SCC(t) scanf("%c",&t)
#define SCS(t) scanf("%s",t)
#define SCF(t) scanf("%f",&t)
#define SCLF(t) scanf("%lf",&t)
#define MEM(a, b) memset(a, (b), sizeof(a))
#define FOR(i, j, k, in) for (int i=j ; i<k ; i+=in)
#define RFOR(i, j, k, in) for (int i=j ; i>=k ; i-=in)
#define REP(i, j) FOR(i, 0, j, 1)
#define RREP(i, j) RFOR(i, j, 0, 1)
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert( B <= A && A <= C)
#define MP make_pair
#define PB push_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define read(type) readInt<type>()
const double pi=acos(-1.0);
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef map<int,int> MPII;
typedef set<int> SETI;
typedef multiset<int> MSETI;
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int  uint64;

typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;

/****** Template of some basic operations *****/
template<typename T, typename U> inline void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> inline void amax(T &x, U y) { if(x < y) x = y; }
/**********************************************/


/**************************************/

int n;
int arr[1001];
/******** User-defined Function *******/

int64 solve(){
  int64 res = 0;

  int sorted[n];
  bool in_loop[n];
  int min_arr = INF;
  map<int,int> ind;
  for(int i = 0; i  <n; i++){
    sorted[i] = arr[i];
    amin(min_arr, arr[i]);
    in_loop[i] = false;
    ind[arr[i]]=i;
  }

  sort(sorted, sorted+n);
  map<int,int> rank;
  int out_of_place = 0;
  for(int i = 0; i  < n; i++){
    if(sorted[i]!=arr[i]){
      out_of_place++;
    }
    rank[sorted[i]] =i;
  }

  for(int i =0;i < n; i++){
    if(in_loop[i]){
      continue;
    } else {
      // not yet visited
      // find the length, min val of the loop
      int curr = i;
      int min_loop = INF;
      int min_len = 0;
      int64 sum_loop = 0;
      while(true){
        // cout << curr << endl;
        if(in_loop[curr]){
          break;
        }
        min_len++;
        in_loop[curr] = true;
        //cout << arr[curr] << " ";
        amin(min_loop, arr[curr]);
        sum_loop += arr[curr];
        curr = ind[sorted[curr]];
       
      }

      res += min(sum_loop + (min_len-2)*min_loop, sum_loop + min_loop + min_arr + (min_len)*min_arr );
    }
  }


  return res;
}



/********** Main()  function **********/
int main()
{
  cin >> n;
  for(int i = 0; i < n; i++){
    cin >> arr[i];
  }

 cout << solve() << endl;


    return 0;
}
/********  Main() Ends Here *************/



Measured memory_bytes: 3200