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 Mergesort(vector<int>&);
int main(int argc, char* argv[]) {
  std::ios_base::sync_with_stdio(false);
  vector<int> vi;
  int n, x, ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x;
    vi.push_back(x);
  }
  vector<int> merges(vi.begin(), vi.end());
  Mergesort(merges);
  for (int i = 0; i < n; i++) {
    if (merges[i] != vi[i]) ans += vi[i];
  }
  cout << ans << endl;
  return 0;
}
void Mergesort(vector<int>& merges) {
  int vecsize = merges.size();
  if (vecsize > 1) {
    vector<int> lft(merges.begin(), merges.begin() + vecsize / 2);
    vector<int> rht(merges.begin() + vecsize / 2, merges.end());
    Mergesort(lft);
    Mergesort(rht);
    for (int i = 0, l = 0, r = 0; i < vecsize; i++) {
      if (l == (int)lft.size())
        merges[i] = rht[r++];
      else if (r == (int)rht.size())
        merges[i] = lft[l++];
      else if (lft[l] <= rht[r])
        merges[i] = lft[l++];
      else
        merges[i] = rht[r++];
    }
  }
}
