Skip to content

Instantly share code, notes, and snippets.

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