注意超int.
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <queue>
using namespace std;
typedef unsigned long UL;
priority_queue<UL, vector<UL>, greater<UL> > q;
int n;
void init()
{
while(!q.empty()) q.pop();
}
void solve()
{
UL ans = 0;
while(q.size() != 1)
{
UL a = q.top(); q.pop();
ans += a;
UL b = q.top(); q.pop();
ans += b;
q.push(a+b);
}
printf("%lu\n", ans);
}
int main()
{
while(~scanf("%d", &n))
{
init();
for(int i = 1; i <= n; i++)
{
UL a;
scanf("%lu", &a);
q.push(a);
}
if(q.size() == 1)
{
printf("%lu\n", q.top());
continue;
}
solve();
}
return 0;
}
#include <cstdlib>
#include <cstdio>
#include <queue>
using namespace std;
typedef unsigned long UL;
priority_queue<UL, vector<UL>, greater<UL> > q;
int n;
void init()
{
while(!q.empty()) q.pop();
}
void solve()
{
UL ans = 0;
while(q.size() != 1)
{
UL a = q.top(); q.pop();
ans += a;
UL b = q.top(); q.pop();
ans += b;
q.push(a+b);
}
printf("%lu\n", ans);
}
int main()
{
while(~scanf("%d", &n))
{
init();
for(int i = 1; i <= n; i++)
{
UL a;
scanf("%lu", &a);
q.push(a);
}
if(q.size() == 1)
{
printf("%lu\n", q.top());
continue;
}
solve();
}
return 0;
}