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;
void merge(vector<pair<int, int> >& w, int left, int mid, int right) {
  vector<pair<int, int> > L, R;
  int n1 = mid - left;
  int n2 = right - mid;
  int i = 0, j = 0, k;
  L.resize(n1 + 1);
  R.resize(n2 + 1);
  for (i = 0; i < n1; ++i) {
    L[i] = w[left + i];
  }
  L[n1].first = -1;
  L[n1].second = 1000000001;
  for (i = 0; i < n2; ++i) {
    R[i] = w[mid + i];
  }
  R[n2].first = -1;
  R[n2].second = 1000000001;
  i = j = 0;
  for (k = left; k < right; ++k) {
    if (L[i].second < R[j].second) {
      w[k] = L[i];
      ++i;
    } else {
      w[k] = R[j];
      ++j;
    }
  }
  return;
}
void mergeSort(vector<pair<int, int> >& w, int left, int right) {
  if (left + 1 < right) {
    int mid = left + (right - left) / 2;
    mergeSort(w, left, mid);
    mergeSort(w, mid, right);
    merge(w, left, mid, right);
  }
  return;
}
int main() {
  int n;
  int i;
  int total = 0;
  vector<pair<int, int> > w;
  scanf("%d", &n);
  w.resize(n);
  for (i = 0; i < n; ++i) {
    w[i].first = i;
    scanf("%d", &w[i].second);
  }
  mergeSort(w, 0, n);
  for (i = 0; i < n; ++i) {
    total += (w[i].first != i ? w[i].second : 0);
  }
  printf("%d\n", total);
  return 0;
}
