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>
struct MCS {
  int cost;
  int order;
};
void exchange(struct MCS *pa, struct MCS *pb) {
  struct MCS t;
  t = *pa;
  *pa = *pb;
  *pb = t;
  return;
}
int Partition(struct MCS A[], int p, int r) {
  struct MCS x;
  int i, j;
  x = A[r];
  i = p - 1;
  for (j = p; j < r; j++) {
    if (A[j].cost < x.cost) {
      i++;
      exchange(&A[i], &A[j]);
    }
  }
  exchange(&A[i + 1], &A[r]);
  return i + 1;
}
void QuickSort(struct MCS A[], int p, int r) {
  int q;
  if (p < r) {
    q = Partition(A, p, r);
    QuickSort(A, p, q - 1);
    QuickSort(A, q + 1, r);
  }
  return;
}
int tryChange(int n, struct MCS W[], struct MCS B[]) {
  int cnt, min;
  int i, j, t;
  int prei, prej;
  prei = prej = 0;
  min = cnt = 20000;
  for (i = 0; i < n; i++) {
    if (i != W[i].order) {
      for (j = 0; j < n; j++) {
        if (W[j].order != B[j].order && W[i].order == j) {
          cnt = B[i].cost + B[W[i].order].cost;
          if (cnt < min) {
            min = cnt;
            prei = i;
            prej = j;
          }
        }
      }
    }
  }
  if (20000 != min) {
    exchange(&B[W[prei].order], &B[prei]);
    t = B[W[prei].order].order = B[prei].order;
    if (B[prei].cost == W[prei].cost) {
      W[prei].order = B[prei].order = prei;
    }
    if (B[t].cost == W[t].cost) {
      W[t].order = t;
    }
  } else {
    min = 0;
  }
  return min;
}
int main(void) {
  int n, i;
  struct MCS W[1000 + 1];
  struct MCS B[1000 + 1];
  int cnt;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &W[i].cost);
    W[i].order = i;
    B[i] = W[i];
  }
  QuickSort(W, 0, n - 1);
  cnt = 0;
  for (i = 0; i < n; i++) {
    cnt += tryChange(n, W, B);
  }
  printf("%d\n", cnt);
  return 0;
}
