You are given $n$ integers $w_i (i = 0, 1, ..., n-1)$ to be sorted in ascending order. You can swap two integers $w_i$ and $w_j$. Each swap operation has a cost, which is the sum of the two integers $w_i + w_j$. You can perform the operations any number of times.

Write a program which reports the minimal total cost to sort the given integers.

Constraints

* $1 \leq n \leq 1,000$
* $0 \leq w_i\leq 10^4$
* $w_i$ are all different

Input

In the first line, an integer $n$ is given. In the second line, $n$ integers $w_i (i = 0, 1, 2, ... n-1)$ separated by space characters are given.

Output

Print the minimal cost in a line.

Examples

Input

5
1 5 3 4 2


Output

7


Input

4
4 3 2 1


Output

10
import java.util.Arrays;
import java.util.Scanner;

/**
 * Created by zhangrunfeng on 2/18/20
 */
public class Main {
    private int N;

    private int[] arr;
    private int[] T;
    private boolean[] v;

    private int minVal;

    public Main(Scanner sc) {
        N = sc.nextInt();
        arr = new int[N];
        for (int i = 0; i < N; i++) {
            arr[i] = sc.nextInt();
        }
        int[] sortedArr = arr.clone();
        Arrays.sort(sortedArr);

        int VMAX = 10000 + 5;
        T = new int[VMAX];
        for (int i = 0; i < N; i++) {
            T[sortedArr[i]] = i;
        }
        minVal = sortedArr[0];

        v = new boolean[N]; // 不用初始化
        Arrays.fill(v, false);
    }

    public int minCost() {
        int sum = 0;
        for (int i = 0; i < N; i++) {
            if (v[i]) continue;

            int an = 0;
            int currSum = 0;
            int currMin = Integer.MAX_VALUE;
            int currIx = i;
            while (!v[currIx]) {
                v[currIx] = true;
                currSum += arr[currIx];
                if (arr[currIx] < currMin) {
                    currMin = arr[currIx];
                }
                an++;
                currIx = T[arr[currIx]];
            }
            if (an >= 2) {
                sum += (currSum + Math.min((an - 2) * currMin, (an + 1) * minVal + currMin));
            }
        }
        return sum;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Main m = new Main(sc);
        int res = m.minCost();
        System.out.println(res);
    }
}
