2 条题解

  • 1
    @ 2026-4-18 22:14:44
    #include <bits/stdc++.h>
    using namespace std;
    int h[100005],sz;
    void up(int i){
        for(;i>1&&h[i]<h[i/2];i/=2)swap(h[i],h[i/2]);
    }
    void down(int i){
        for (int s;(s=i*2)<=sz;i=s){
            if(s+1<=sz&&h[s+1]<h[s])s++;
            if(h[i]<=h[s])break;
            swap(h[i],h[s]);
        }
    }
    void push(int x){
        h[++sz]=x;up(sz);
    }
    int pop(){
        int r=h[1];h[1]=h[sz--];down(1);return r;
    }
    int main(){
    	int n,x;long long ans=0;
    	cin>>n;
        while(n--){
            cin>>x;push(x);
        }
        while(sz>1){
            int a=pop(),b=pop();
            ans+=a+b;push(a+b);
        }
    	cout<<ans;
    	return 0;
    }
    

    信息

    ID
    675
    时间
    1000ms
    内存
    128MiB
    难度
    8
    标签
    递交数
    234
    已通过
    39
    上传者