`#include
include
using namespace std;
template
class smallest_heap{
private:
item heap[10001];
int len;
public:
smallest_heap();
void push(item const &);
void pop();
item top();
int size();
bool empty();
};
template
smallest_heap
len=0;
memset(heap,0,sizeof(heap));
}
template
void smallest_heap
heap[++len]=n;
int son=len,father=son/2;
while(heap[son]<heap[father] && father>=1){
swap(heap[son],heap[father]);
son=father,father=son/2;
}
}
template
void smallest_heap
swap(heap[1],heap[len]);
heap[len--]=0;
int father=1,son=2;
while(son<=len){
if(son<len && heap[son]>heap[son+1]) son++;
if(heap[father]>heap[son]){
swap(heap[father],heap[son]);
father=son,son=father*2;
}else break;
}
}
template
item smallest_heap
return heap[1];
}
template
int smallest_heap
return len;
}
template
bool smallest_heap
return len;
}
smallest_heap
int n,ans;
int main(){
cin>>n;
for(int i=1;i<=n;i++){
int a;
cin>>a;
h.push(a);
}
while(h.size()>1){
int x=h.top(); h.pop();
int y=h.top(); h.pop();
h.push(x+y);
ans+=x+y;
}
cout<<ans;
return 0;
}`