#include <stdio.h>

int main() {
  /* a[0] to a[n-1] is the array to sort */
  int i,j;
  int n, swapTotal = 0;

  scanf("%d", &n);

  int a[n];

  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }

  /* advance the position through the entire array */
  /*   (could do j < n-1 because single element is also min element) */
  for (j = 0; j < n-1; j++)  {
      /* find the min element in the unsorted a[j .. n-1] */

      /* assume the min is the first element */
    int iMin = j;
      /* test against elements after j to find the smallest */
    for (i = j+1; i < n; i++) {
          /* if this element is less, then it is the new minimum */
      if (a[i] < a[iMin]) {
        /* found new minimum; remember its index */
        iMin = i;
      }
    }

    if (iMin != j) {
      int temp = a[j];
      a[j] = a[iMin];
      a[iMin] = temp;
      swapTotal += a[j] + a[iMin];
    }
  }

  printf("%d\n", swapTotal);
}

