分析:这道题不是前缀和,记好!!!用到了优先队列,默认是从大到小,改一下就变成了从小到大
源码:
include <bits/stdc++.h>//优先队列
using namespace std;
typedef long long ll;
priority_queue<ll,vector,greater >qu;
ll arr[100005];
int main(void)
{
int n; cin>>n; for(int i=0;i<n;i++) { cin>>arr[i]; qu.push(arr[i]); } long long sum=0; while(qu.size()>1) { int lx=qu.top(); qu.pop(); int ly=qu.top(); qu.pop(); sum=sum+lx+ly; qu.push(lx+ly); } cout<<sum<<endl; return 0;
}