https://ac.nowcoder.com/acm/contest/22669/I
堆的用法
Type: 佇列中儲存元素的型別。例如 int,double,pair<int, int> 等。
Container: 底層儲存資料的容器型別,預設為 vector
Compare: 比較函式,用於決定優先順序順序。預設是 less
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
priority_queue<int, vector<int>, greater<int>> q;
for(int i=0;i<n;i++) {
int t;
scanf("%d", &t);
q.push(t);
}
long long ans = 0;
while(q.size() > 1) {
int first = q.top();
q.pop();
int second = q.top();
q.pop();
long long total = first + second;
ans += total;
q.push(total);
}
cout << ans;
return 0;
}