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.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Main {
	long count = 0;

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

	private void run() {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		int min = Integer.MAX_VALUE;
		int[] A = new int[n];
		int max = -1;
		for (int i = 0; i < n; i++) {
			A[i] = scanner.nextInt();
			min = Math.min(min, A[i]);
			max = Math.max(max, A[i]);
		}
		int ans = 0;
		boolean[] V = new boolean[n];
		int[] B = Arrays.copyOf(A, A.length);
		Arrays.sort(B);
		int[] T = new int[max + 1];
		List<Integer> list  = new ArrayList<Integer>();
		for (int i = 0; i < n; i++) {
			list.add(B[i]);
		}
		for (int i = 0; i < n; i++) {
			int cur = i;
			int s = 0;
			int m = max;
			int an = 0;
			while (true) {
				V[cur] = true;
				an++;
				int v = A[cur];
				m = Math.min(m, v);
				s += v;
				cur = list.indexOf(v);
				if (V[cur])
					break;
			}
			int a = s + (an - 2) * m;
			int b = m + s + (an + 1) * min;
			ans += Math.min(a, b);
		}
		System.out.println(ans);
	}
}