#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cassert>
#include <functional>

using namespace std;

typedef long long i64;

const int MAXN = 200000;

int a[MAXN];
int b[MAXN];
int c[MAXN];

int main() {
	//freopen("D.in", "r", stdin);
	//freopen("D.out", "w", stdout);
	
	int n; scanf("%d", &n);
	assert((2 <= n) && (n <= 100000));
	int m = 0;
	for (int i = 0; i < n; ++i) {
		scanf("%d", &a[i]);
		assert((1 <= a[i]) && (a[i] <= 100000));
		++c[a[i]];
		m = max(m, a[i]);
	}
	
	int k = -1;
	for (int i = 0; i <= MAXN; ++i) {
		if (c[i] > (n >> 1) + (n & 1)) {
			k = i;
			break;
		}
	}
	if (k != -1) {
		int l = 0;
		if (k != m) {
			for (int i = MAXN; i > k; --i) {
				while (c[i] > 0) {
					b[l++] = k;
					--c[i];
					b[l++] = i;
					--c[k];
				}
			}
		}
		int r = n - 1;
		for (int i = 0; i <= k; ++i) {
			while (c[i] > 0) {
				if (r < l) {
					break;
				}
				b[r--] = k;
				--c[k];
				if (r < l) {
					break;
				}
				b[r--] = i;
				--c[i];
			}
		}
	}
	if (k == -1) {
		sort(a, a + n, greater<int>());
		int k = 0;
		for (int i = 1; i < n; i += 2) {
			b[i] = a[k++];
		}
		for (int i = 0; i < n; i += 2) {
			b[i] = a[k++];
		}
		
		if (n & 1) {
			swap(b[n - 1], b[n - 2]);
			swap(b[n - 2], b[n - 3]);
		}
	}

	i64 result = 0;
	for (int i = 0; i < n; ++i) {
		int j = i;
		while ((j < n) && (b[j] <= b[j + 1])) {
			++j;
		}
		if (j - i + 1 < 2) {
			cerr << "OOPS" << endl;
		}
		result += b[j];
		i = j;
	}
	
	//printf("%lld\n", result);
	cout<<result<<endl;
	
	return 0;
}
