POJ 3253(贪心)

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <queue>
 7 #include <stack>
 8 #include <vector>
 9 using namespace std;
10 priority_queue<int,vector<int>,greater<int> >q;
11 int main(int argc, char *argv[])
12 {
13     int n,t;
14     long long ans;
15     while(scanf("%d",&n)!=EOF)
16     {
17         ans=0;
18         for(int i=0;i<n;i++)
19         {
20             scanf("%d",&t);
21             q.push(t);
22         } 
23         while(q.size()>1)
24         {
25             int a,b;
26             a=q.top();
27             q.pop();
28             b=q.top();
29             q.pop();
30             ans+=a+b;
31             q.push(a+b);
32         }
33         q.pop();
34         printf("%I64d\n",ans);
35     }
36 }

 

posted @ 2018-09-10 15:34  huluxin  阅读(111)  评论(0编辑  收藏  举报