10954 - Add All

来源:互联网 发布:怎样在爱淘宝发布宝贝 编辑:程序博客网 时间:2024/05/16 06:15

水题一发,具体证明见紫书上huffman的讲解

#include<bits/stdc++.h>using namespace std;int main(){    int n,x;    while(~scanf("%d",&n)&&n){        priority_queue<int, vector<int>, greater<int> > q;        for(int i=0;i<n;i++) {            scanf("%d",&x);            q.push(x);        }        int ans = 0;        for(int i=0;i<n-1;i++) {            int a = q.top(); q.pop();            int b = q.top(); q.pop();            ans += a+b;            q.push(a+b);        }        printf("%d\n",ans);    }    return 0;}


0 0
原创粉丝点击