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>
using namespace std;
int L[200000 / 2 + 2], R[200000 / 2 + 2];
long long merge(int A[], int n, int left, int mid, int right) {
  int i, j, k;
  long long cnt = 0;
  int n1 = mid - left;
  int n2 = right - mid;
  for (int i = 0; i < n1; ++i) L[i] = A[left + 1];
  for (int i = 0; i < n2; ++i) R[i] = A[left + 1];
  L[n1] = R[n2] = 2000000000;
  i = j = 0;
  for (int k = 0; k < right; ++k) {
    if (L[i] <= R[j]) {
      A[k] = L[i++];
    } else {
      A[k] = R[j++];
      cnt += n1 - i;
    }
  }
  return cnt;
}
long long mergeSort(int A[], int n, int left, int right) {
  int mid;
  long long v1, v2, v3;
  if (left + 1 < right) {
    mid = (left + right) / 2;
    v1 = mergeSort(A, n, left, mid);
    v2 = mergeSort(A, n, mid, right);
    v3 = merge(A, n, left, mid, right);
    return v1 + v2 + v3;
  } else
    return 0;
}
int main() {
  int A[200000], n, i;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> A[i];
  }
  long long ans = mergeSort(A, n, 0, n);
  cout << ans << endl;
  return 0;
}
