提交时间:2024-03-09 10:08:40

运行 ID: 136526

#include<bits/stdc++.h> using namespace std; int n,guo,heap_size,ans=0,ans1=0,x,y; int heap[10001]; void put(int d) { int now,next; heap[++heap_size]=d; now=heap_size; while(now>1) { next=now>>1; if(heap[now]>=heap[next]) break; else swap(heap[now],heap[next]); now=next; } } int get() { int now,next,res; res=heap[1]; heap[1]=heap[heap_size--]; now=1; while(now*2<=heap_size) { next=now*2; if(next<heap_size&&heap[next+1]<heap[next]) next++; if(heap[now]>heap[next]) swap(heap[now],heap[next]); now=next; } return res; } int main() { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&guo); put(guo); } for(int i=1;i<n ;i++) { x=get(); y=get(); ans+=x+y; put(x+y); } cout<<ans; return 0; }