【題解】TIOJ 1424 . 手續費 (Fee)

【題目敘述】https://tioj.ck.tp.edu.tw/problems/1424

#include <iostream>
#include <queue>
using namespace std;

int main(){
    int n, temp, ans;
    priority_queue <int, vector<int>, greater<int> > pq;
    while (cin >> n){
        ans = 0;
        for (int i = 0; i < n; i++){
            cin >> temp;
            pq.push(temp);
        }
        while (pq.size() > 1){
            temp = pq.top();
            pq.pop();
            temp += pq.top();
            pq.pop();
            ans += temp;
            pq.push(temp);
        }
        cout << ans << "\n";
    }
    return 0;
}
分享本文 Share with friends