Skip to content

Instantly share code, notes, and snippets.

@KT-Yeh
Created March 2, 2014 17:27
Show Gist options
  • Save KT-Yeh/9310106 to your computer and use it in GitHub Desktop.
Save KT-Yeh/9310106 to your computer and use it in GitHub Desktop.
#include <cstdio>
#include <queue>
using namespace std;
typedef long long int llt;
int main()
{
llt N, L;
while (scanf("%lld", &N) != EOF) {
priority_queue<llt, vector<llt>, greater<llt> > PQ;
for (llt i = 0; i < N; ++i) {
scanf("%lld", &L);
PQ.push(L);
}
llt sum = 0;
while (PQ.size() > 1) {
llt tmp = PQ.top();
PQ.pop();
tmp += PQ.top();
PQ.pop();
sum += tmp;
PQ.push(tmp);
}
printf("%lld\n", sum);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment