#include <stdio.h>
#include <string.h>

#define N 1024

int cost = 0, W[N] = {0}, C[N] = {0};

void swap(int *x, int *y) {
  int tmp;

  tmp = *x;
  *x = *y;
  *y = tmp;
}

int partition(int p, int r) {
  int i, j, x;

  x = W[r];
  i = p - 1;
  for (j = p; j < r; j++) {
    if (W[j] <= x) {
      i++;
      swap(&W[i], &W[j]);
    }
  }
  swap(&W[i + 1], &W[r]);
  return i + 1;
}

void quickSort(int p, int r) {
  int q;
  
  if (p < r) {
    q = partition(p, r);
    quickSort(p, q - 1);
    quickSort(q + 1, r);
  }
}

int main(void) {
  int i, j, n;
  const char *sp;

  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%d", &W[i]);
  memcpy(C, W, sizeof(W));

  quickSort(0, n - 1);
  for (i = n - 1; 0 < i; i--) {
    for (j = 0; j < n; j++) {
      if (W[i] == C[j] && i != j) {
	cost += C[i];
	cost += C[j];
	//	printf("swap:%d,%d\n", C[i], C[j]);
	swap(&C[i], &C[j]);
	break;
      }
    }
  }

  /* for (i = 0, sp = ""; i < n; i++) { */
  /*   printf("%c%d", *sp, W[i]); */
  /*   sp = " "; */
  /* } */
  /* puts(""); */
  printf("%d\n", cost);
  return 0;
}