思想:反复选择两个最小的元素合并,直到只剩下一个元素
#include<cstdio> #include<cmath> #include<cstring> #include<iostream> #include<cstdlib> #include<algorithm> #include<string> #include<stack> #include<queue> #include<map> #define MAX 110 using namespace std; int n; //优先队列:设置优先级使最小的优先级大 priority_queue<int,vector<int>,greater<int>> Q; int main(){ scanf("%d",&n); int temp; for(int i=0;i<n;i++){ scanf("%d",&temp); Q.push(temp); } int a,b,sum=0; while(Q.size()>1){ a=Q.top(); Q.pop(); b=Q.top(); Q.pop(); Q.push(a+b); sum+=a+b; } printf("%d\n",sum); return 0; }