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
#include <bits/stdc++.h>
void swap(int *A, int i, int j) {
  int temp;
  temp = A[i];
  A[i] = A[j];
  A[j] = temp;
}
void Merge(int *A, int left, int mid, int right) {
  int n1, n2, *L, *R, i, j, k;
  n1 = mid - left;
  n2 = right - mid;
  L = (int *)malloc(sizeof(int) * (n1 + 1));
  R = (int *)malloc(sizeof(int) * (n2 + 1));
  for (i = 0; i < n1; i++) {
    L[i] = A[left + i];
  }
  for (i = 0; i < n2; i++) {
    R[i] = A[mid + i];
  }
  L[n1] = INT_MAX;
  R[n2] = INT_MAX;
  i = 0;
  j = 0;
  for (k = left; k < right; k++) {
    if (L[i] < R[j]) {
      A[k] = L[i];
      i++;
    } else {
      A[k] = R[j];
      j++;
    }
  }
  free(L);
  free(R);
}
void Merge_Sort(int *A, int left, int right) {
  if (left + 1 < right) {
    int mid = (left + right) / 2;
    Merge_Sort(A, left, mid);
    Merge_Sort(A, mid, right);
    Merge(A, left, mid, right);
  }
}
int main() {
  int i, j, n, *w, *w2;
  long sum;
  scanf("%d", &n);
  w = (int *)malloc(sizeof(int) * n);
  w2 = (int *)malloc(sizeof(int) * n);
  for (i = 0; i < n; i++) {
    scanf("%d", w + i);
    w2[i] = w[i];
  }
  Merge_Sort(w2, 0, n);
  sum = 0;
  for (i = 0; i < n; i++) {
    j = i;
    while (w[j] != w2[i]) j++;
    if (j != i) {
      sum += w[i] + w[j];
      swap(w, i, j);
    }
  }
  printf("%ld\n", sum);
  return 0;
}
