Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Minimum Cost Sort

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.

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.

Constraints

$1 \leq n \leq 1,000$

$0 \leq w_i\leq 10^4$

$w_i$ are all different

Sample Input 1

5
1 5 3 4 2

Sample Output 1

7

Sample Input 2

4
4 3 2 1

Sample Output 2

10

Solution in JAVA (judge verdict: Accepted)

import java.util.*;
import java.io.*;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);

		int n = scanner.nextInt();
		int[] S = new int[n];
		int[] A = new int[n];

		for (int i = 0; i < n; i ++) {
			S[i] = i;
			A[i] = scanner.nextInt();
		}

		scanner.close();

		quickSort(S, A, 0, n - 1);
		
		boolean[] did = new boolean[n];
		int cost = 0;
		
		for (int i = 0; i < n; i ++) {
			if (did[i]) continue;
			did[i] = true;
			if (S[i] == i) continue;
			
			int x = 0;
			int y = (A[i] + A[0]) * 2;
			for (int j = S[i]; S[j] != S[i]; j = S[j]) {
				did[j] = true;
				x += A[j] + A[i];
				y += A[j] + A[0];
			}
			cost += Math.min(x, y);
		}
		System.out.println(cost);
	}

	public static int partition (int[] S, int[] A, int p, int r) {
		int x = A[r];
		int i = p - 1;

		for (int j = p; j < r; j++) {
			if (A[j] <= x) {
				i ++;
				int tempc = S[i];
				S[i] = S[j];
				S[j] = tempc;
				int temp = A[i];
				A[i] = A[j];
				A[j] = temp;
			}
		}

		int tempc = S[++ i];
		int temp = A[i];
		S[i] = S[r];
		A[i] = A[r];
		S[r] = tempc;
		A[r] = temp;

		return i;
	}

	public static void quickSort(int[] S, int[] A, int p, int r) {
		if (p < r) {
			int q = partition(S, A, p, r);
			quickSort(S, A, p, q - 1);
			quickSort(S, A, q + 1, r);
		}
	}
}