#include <algorithm>

#include <cstdio>

#include <cstring>

#include <iostream>

#include <queue>

using namespace std;
struct food{

    int g;

/*定义结构体函数,重载<运算符,一般大家如果是自定义类就干脆重载<号时注意下方向了,
没人在这里麻烦,这个选择基本上是在使用int类还想小值先出列时。
想要让自定义类能够使用优先队列,我们要重载小于号。*/
    bool operator<(const food &f)const{

        return f.g<g;
    //优先队列,重量小的在前  
	}

/*这里f函数其实有两个参数,第一个是A* const this, 另一个才是int类型的参数 
如果我们不想f函数改变参数的值,可以把函数原型改为f(constint),
但如果我们不允许f改变this指向的对象呢?因为this是隐含参数,const没法直接修饰它,就加在函数的后面了,
表示this的类型是constA *constthis。 
const修饰*this是本质,至于说“表示该成员函数不会修改类的数据。
否则会编译报错”之类的说法只是一个现象,根源就是因为*this是const类型的*/
};

int main(){
    int n,sum=0;

    priority_queue<food> qe;

    food a,b;

    scanf("%d",&n);
    for(int i=0;i<n;i++){

        scanf("%d",&a.g);

        qe.push(a);

    }
    for(int i=0;i<n-1;i++){

        a=qe.top(),qe.pop();

        b=qe.top(),qe.pop();

        sum+=a.g+b.g;

        a.g=a.g+b.g;

        qe.push(a);

    }
/*a获取队列头部第一个元素,弹出,b获取队列头部第二个元素,弹出
,sum加上两个元素,两个元素选一个等于这两个元素相加,
进入有规则的队列排序*/
    while(!qe.empty()){qe.pop();}
    printf("%d",sum);

    return 0;

}