Skip to content

Instantly share code, notes, and snippets.

@surinoel
Created October 12, 2019 15:33
Show Gist options
  • Save surinoel/7820c0115a5035a03c10c24f645249ee to your computer and use it in GitHub Desktop.
Save surinoel/7820c0115a5035a03c10c24f645249ee to your computer and use it in GitHub Desktop.
#include <queue>
#include <vector>
#include <iostream>
#include <functional>
using namespace std;
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
priority_queue<int, vector<int>, greater<int>> pq;
int tc;
cin >> tc;
while (tc--) {
int x;
cin >> x;
pq.push(x);
}
int ans = 0;
while (pq.size() > 1) {
int a = pq.top();
pq.pop();
int b = pq.top();
pq.pop();
ans += (a + b);
pq.push(a + b);
}
cout << ans << '\n';
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment