#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include<stdio.h>
#include<stdlib.h>

int main() {
	int *W, n,i,j,jTmp,min,tmp,cost=0;

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

	for (i = 0; i < n-1; i++) {
		min = W[i];
		jTmp = i;
		for (j = i+1; j < n; j++) {
			if(min>W[j]){
				min = W[j];
				jTmp = j;
			}
		}
		if (i != jTmp) {
			tmp = W[i]; W[i] = W[jTmp]; W[jTmp] = tmp;
			cost += W[i] + W[jTmp];
		}
	}
	printf("%d\n", cost);
	free(W);
	return 0;
}