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>
int merge(int *, int, int, int);
int mergeSort(int *, int, int);
int cnt = 0;
int main() {
  int n;
  std::cin >> n;
  int A[n];
  for (int i = 0; i < n; i++) std::cin >> A[i];
  int ans = mergeSort(A, 0, n);
  for (int i = 0; i < n; i++) std::cout << A[i] << (i != n - 1 ? " " : "\n");
  std::cout << ans << "\n";
  return 0;
}
int merge(int *A, int l, int m, int r) {
  int n1 = m - l, n2 = r - m, i, j, L[n1 + 1], R[n2 + 1], count = 0;
  for (i = 0; i < n1; i++) L[i] = A[l + i];
  for (i = 0; i < n2; i++) R[i] = A[m + i];
  i = j = 0, L[n1] = R[n2] = INT_MAX;
  ;
  for (int k = l; k < r; k++) {
    if (L[i] <= R[j])
      A[k] = L[i++], cnt++;
    else
      A[k] = R[j++], cnt++, count += n1 - i;
  }
  return count;
}
int mergeSort(int *A, int l, int r) {
  int m;
  if (l + 1 < r) {
    m = (l + r) / 2;
    int i = mergeSort(A, l, m), j = mergeSort(A, m, r);
    int k = merge(A, l, m, r);
    return i + j + k;
  } else
    return 0;
}
