P1090 [NOIP2004 提高组] 合并果子 / [USACO06NOV]

/*
3 
1 2 9
15
*/
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<bits/stdc++.h>
#define ll long long
#define ddd printf("-----------------debug\n");
using namespace std;

int n;
priority_queue< int , vector<int> ,greater<int> >q;//greater

int main()
{
    ios::sync_with_stdio(false);
    cin>>n; int x,ans=0;
    for(int i=1;i<=n;i++) cin>>x,q.push(x);
    for(int i=1;i<=n-1;i++)
    {
        int a=q.top();q.pop();
        int b=q.top();q.pop();
        ans+=a+b;
        q.push(a+b);
    }
    cout<<ans<<endl;
    
    return 0;
}
View Code
 

 

 
posted @ 2023-07-12 22:07  JMXZ  阅读(8)  评论(0)    收藏  举报