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.HashMap;
import java.util.LinkedList;
import java.util.Scanner;

public class Main {
	final boolean DEBUG = false;
	final int INF = Integer.MAX_VALUE;

	void run() {
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		int[] w = new int[n];
		int[] a = new int[n];
		int[] _a = new int[n];
		int[] rank = new int[n];
		for (int i = 0; i < n; i++)
			rank[i] = INF;
		for (int i = 0; i < n; i++)
			a[i] = w[i] = scan.nextInt();
		Arrays.sort(a);
		int min = a[0];
		for (int i = 0; i < n; i++)
			_a[i] = Arrays.binarySearch(a, w[i]);
		if (DEBUG) {
			System.out.println(Arrays.toString(w));
			System.out.println(Arrays.toString(_a));
			System.out.println(Arrays.toString(a));
		}
		int index = 0;
		for (int i = 0; i < n; i++) {
			int idx = i;
			if (rank[i] != INF)
				continue;
			while (rank[i] == INF) {
				rank[_a[idx]] = index;
				idx = _a[idx];
			}
			index++;
		}
		if (DEBUG)
			System.out.println(Arrays.toString(rank));
		HashMap<Integer, LinkedList<Integer>> map = new HashMap<Integer, LinkedList<Integer>>();
		for (int i = 0; i < n; i++) {
			LinkedList<Integer> list = map.get(rank[i]);
			if (list == null) {
				LinkedList<Integer> l = new LinkedList<Integer>();
				l.add(w[i]);
				map.put(rank[i], l);
			} else {
				list.add(w[i]);
				map.put(rank[i], list);
			}
		}
		if (DEBUG) {
			System.out.println(map.size());
			for (int i = 0; i < map.size(); i++) {
				LinkedList<Integer> list = map.get(i);
				System.out.println("map " + i + ": " + list.toString());
			}
		}
		int ans = 0;
		for (int i = 0; i < map.size(); i++) {
			LinkedList<Integer> list = map.get(i);
			if (list.size() == 1)
				continue;
			int sum = 0;
			int m = INF;
			int length = list.size();
			for (int j = 0; j < length; j++) {
				int num = list.get(j);
				sum += num;
				m = Math.min(m, num);
			}
			if (DEBUG)
				System.out.println(sum + " " + m + " " + ((length + 1) * min));
			ans += Math.min(sum + (length - 2) * m, sum + m + (length + 1) * min);
		}
		System.out.println(ans);
	}

	public static void main(String[] args) {
		new Main().run();
	}
}

