#include <iostream>
#include <fstream>
#define MAX 10001

using namespace std;

typedef struct weight{
    int w;
    int id;
} weight_t;

void weight_swap(weight_t *a,weight_t *b){
    weight_t tmp = *a;
    *a = *b;
    *b = tmp;
}

void pp_weight(weight_t *w, int w_size){
    for(int i=0;i<w_size;i++){
        cout << "i: " << i;
        cout << " w: " << w[i].w;
        cout << " id: " << w[i].id << endl;
    }
}

int maxW(weight_t *w, int w_size){
    int ans = w[0].w;
    for(int i=0;i<w_size;i++){
        if(ans < w[i].w){
            ans = w[i].w;
        }
    }
    return ans;
}

int minW(weight_t *w, int w_size){
    int ans = w[0].w;
    for(int i=0;i<w_size;i++){
        if(ans > w[i].w){
            ans = w[i].w;
        }
    }
    return ans;
}

void countingSort(weight_t *w, int w_size){
    int max_w = maxW(w, w_size) + 1;
    int *c = NULL;
    c= new int[max_w];
    for(int i=0;i<max_w;i++) c[i] = 0;
    for(int i=0;i<w_size;i++) c[w[i].w]++;
    for(int i=1;i<max_w;i++) c[i] = c[i] + c[i-1];
    for(int i=w_size-1;i>=0;i--){
        w[i].id = c[w[i].w]-1;
        c[w[i].w]--;
    }
    delete [] c;
}

int checkmin(weight_t *w, int w_size){
    int ans = -1;
    int min_weight =MAX;
    for(int i=0;i<w_size;i++){
        if(i != w[i].id){
            if(min_weight > w[i].w){
                min_weight = w[i].w;
                ans = i;
            }
        }
    }
    return ans;
}

int changed_ahead(weight_t *w, int w_size, int id){
    for(int i=0;i<w_size;i++){
        if(id == w[i].id){
            return i;
        }
    }
    return -1;
}

int swap_cost = 0;

void circuitExchange(weight_t *w, int w_size, int changed_id, int counting){
    // cout << "circuit"<<endl;
    int goal = w[changed_id].id;
    int ahead = changed_ahead(w,w_size,changed_id);
    // cout << "id: " << changed_id << " goal: " << goal;
    // cout << " ahead: " << ahead << endl;
    if(ahead == goal){
        // cout << "calc" << endl;
        swap_cost += w[ahead].w;
        counting++;
        int no_use_min = counting * w[changed_id].w;
        int use_min = (counting + 2) * minW(w,w_size) + 2* w[changed_id].w;
        // cout << "no_use: " << no_use_min;
        // cout << " use: " << use_min <<endl;
        // cout << "swap :" << swap_cost << endl;
        swap_cost += (no_use_min < use_min) ? no_use_min : use_min;
                // cout << "swap :" << swap_cost << endl;
        weight_swap(w+ahead,w+changed_id);
        // pp_weight(w,w_size);
        // cout << "finish cals" << endl;
    }else{
        swap_cost += w[ahead].w;
        weight_swap(w+ahead,w+changed_id);
        // pp_weight(w,w_size);
        // cout << "next" << endl;
        circuitExchange(w,w_size,ahead,counting + 1);
    }

}

void minimumCostSort(weight_t *w, int w_size){
    int min_w = minW(w,w_size);
    int prev_min = -1;
    int change_min;
    while((change_min = checkmin(w,w_size)) != -1){
        // cout << "start cost" <<endl;
        circuitExchange(w,w_size,change_min,0);
        // cout << "end cost" <<endl;
    }
}

int main(){
    int n;
    cin >> n;
    weight_t *w = new weight_t[n];
    for(int i=0;i<n;i++) cin >> w[i].w;
    countingSort(w,n);
    // pp_weight(w,n);
    minimumCostSort(w,n);
    cout << swap_cost << endl;
    
    delete [] w;
    return 0;
}



Measured memory_bytes: 3120