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;
vector<int> sorted;
int minimumCostSort(vector<int> &v) {
  int n = v.size();
  int cost = 0;
  int vindex = 0;
  int indexOfSmallest = find(v.begin(), v.end(), sorted[vindex]) - v.begin();
  while (vindex < n) {
    if (v[indexOfSmallest] == v[vindex]) {
      vindex++;
      indexOfSmallest = find(v.begin(), v.end(), sorted[vindex]) - v.begin();
    } else {
      int indexOfTarget =
          find(v.begin(), v.end(), sorted[indexOfSmallest]) - v.begin();
      cost += v[indexOfSmallest] + v[indexOfTarget];
      int temp = v[indexOfSmallest];
      v[indexOfSmallest] = v[indexOfTarget];
      indexOfSmallest = indexOfTarget;
      v[indexOfTarget] = temp;
    }
  }
  return cost;
}
int main() {
  int n;
  cin >> n;
  vector<int> v;
  for (int i = 0; i < (n); i++) {
    int j;
    cin >> j;
    v.push_back(j);
  };
  copy(v.begin(), v.end(), back_inserter(sorted));
  sort(sorted.begin(), sorted.end());
  cout << minimumCostSort(v) << endl;
  return 0;
}
