题目链接:点击打开链接
题目大意:略。
解题思路:每次取最小两个数相加 + 优先队列。
AC 代码
#include<bits/stdc++.h> #include<cmath> #define mem(a,b) memset(a,b,sizeof a); #define INF 0x3f3f3f3f using namespace std; typedef long long ll; struct cmp { bool operator()(int a,int b) { return a>b; } }; int a[10100]; priority_queue<int,vector<int>,cmp> pq; int main() { int n,b; while(~scanf("%d",&n)) { while(!pq.empty()) pq.pop(); while(n--) { scanf("%d",&b); pq.push(b); } int n1,n2,rs=0; while(pq.size()!=1) { n1=pq.top(); pq.pop(); n2=pq.top(); pq.pop(); rs+=n1+n2; pq.push(n1+n2); } pq.pop(); printf("%d\n",rs); } return 0; }