合并果子(优先队列水题)
AC_Code
1 #include <bits/stdc++.h> 2 #include <cstdio> 3 #include <cstring> 4 #include <string> 5 #include <cmath> 6 #include <queue> 7 #include <stack> 8 #include <vector> 9 #include <set> 10 #include <map> 11 #include <algorithm> 12 using namespace std; 13 typedef long long ll; 14 15 int n,x,ans; 16 priority_queue<int,vector<int>,greater<int> >q; 17 18 int main(){ 19 scanf("%d",&n); 20 for(int i=1;i<=n;i++){ 21 scanf("%d",&x); 22 q.push(x); 23 } 24 25 while( q.size()>=2 ){ 26 int a=q.top(); q.pop(); 27 int b=q.top(); q.pop(); 28 ans += a+b; 29 q.push(a+b); 30 } 31 printf("%d\n",ans); 32 return 0; 33 }