#include <iostream>
#include <vector>
#include <queue>
#pragma region 입출력 속도향상
#define FAST_IO() \
{\
ios::sync_with_stdio(false);\
cin.tie(NULL); \
cout.tie(NULL); \
}\
#pragma endregion
using namespace std;
int main()
{
FAST_IO();
int n, ans = 0;
cin >> n;
priority_queue<int, vector<int>, greater<int>> pq;
if (n == 1)
{
cout << 0 << '\n';
return 0;
}
while (n--)
{
int tmp;
cin >> tmp;
pq.push(tmp);
}
while (!pq.empty())
{
int sum = 0;
sum += pq.top();
pq.pop();
if (!pq.empty())
{
sum += pq.top();
pq.pop();
if (!pq.empty())
pq.push(sum);
}
ans += sum;
}
cout << ans << '\n';
return 0;
}