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
// reference https://www.ipsj.or.jp/07editj/promenade/4506.pdf

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

class Node implements Comparable<Node> {
    int value;
    int place;

    Node(int value, int place) {
        this.value = value;
        this.place = place;
    }

    public int compareTo(Node n) {
        return value - n.value;
    }
}

public class Main {
    private static int sillysort(int m, int[] seq) {
        // initialize
        Node[] nodes = new Node[m];
        Node[] temp = new Node[m];
        for(int i = 0; i < m; i++) {
            temp[i] = new Node(seq[i], i);
        }
        Arrays.sort(temp);
        for(int i = 0; i < m; i++) {
            nodes[i] = new Node(-1, -1);
        }
        for(int i = 0; i < m; i++) {
            nodes[i].value = seq[i];
            nodes[temp[i].place].place = i;
        }
        int s = temp[0].value;

        // compute
        int cost = 0;
        for(int i = 0; i < m; i++) {
            int j = nodes[i].place;
            if(j >= 0 && j != i) {
                int n = 1;
                int amin = nodes[i].value;
                int sum = nodes[i].value;
                while(j != i) {
                    int next = nodes[j].place;
                    amin = Math.min(amin, nodes[j].value);
                    sum += nodes[j].value;
                    n++;
                    nodes[j].place = -1;
                    j = next;
                }
                cost += Math.min(sum + (n - 2) * amin, sum + amin + (n + 1) * s);
            }
        }
        return cost;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
 
        int n = sc.nextInt();
        int[] a = new int[n];
        for(int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }

        System.out.println(sillysort(n, a));

        sc.close();
    }
}

