UVa 10954 - Add All(最优二叉树)

来源:互联网 发布:php 接口验证 编辑:程序博客网 时间:2024/06/08 13:20

简单题。

#include <cstdio>#include <queue>using namespace std;priority_queue<int, vector<int>, greater<int> > q;int main() {    int n, ans, k;    while(~scanf("%d", &n) && n) {        while(!q.empty()) q.pop();        ans = 0;        while(n--) scanf("%d", &k), q.push(k);        while(q.size() > 1) {            int a,b;            a = q.top(), q.pop(), b = q.top(), q.pop();            q.push(a + b);            ans += a + b;        }        printf("%d\n", ans);    }    return 0;}
0 0