View Code
#include#include #include #include #define maxn 20007 using namespace std; struct cmp { bool operator() (int &a,int &b) { return a > b; } }; int main() { priority_queue ,cmp>q; int n,i,sum,a; while (~scanf("%d",&n)) { while (!q.empty()) q.pop(); for (i = 0; i < n; ++i) { scanf("%d",&a); q.push(a); } sum = 0; while (!q.empty()) { int x = q.top(); q.pop(); int y = q.top(); q.pop(); //printf("%d %d\n",x,y); sum += (x+y); if (!q.empty()) q.push(x + y); } printf("%d\n",sum); } }