P1090 合并果子(优先队列)

题目链接:https://www.luogu.com.cn/problem/P1090

#include <bits/stdc++.h>
using namespace std;

priority_queue<int, vector<int>, greater<int>> q; //小顶堆

int main() {
    int n; cin >> n;
    for (int i = 0; i < n; i++) {
        int t; cin >> t;
        q.push(t);
    }
    int ans = 0 ;
    while (q.size() > 1) {
        int a = q.top();
        q.pop();
        int b = q.top();
        q.pop();
        ans += a + b;
        q.push(a + b);
    }
    cout << ans;
}

 

posted @ 2020-04-12 12:49  Kanoon  阅读(119)  评论(0)    收藏  举报