题解
2024-12-28 13:36:39
发布于:广东
7阅读
0回复
0点赞
#include<bits/stdc++.h>
using namespace std;
int heap[10005],len = 0;
void put(int idx){
if(idx == 1) return;
if(heap[idx] < heap[idx / 2]){
swap(heap[idx],heap[idx / 2]);
put(idx / 2);
}
}
void pw(int idx){
if(idx * 2 > len) return;
if(heap[idx * 2] < heap[idx * 2 + 1]){
if(heap[idx] > heap[idx * 2]){
swap(heap[idx],heap[idx * 2]);
pw(idx * 2);
}
} else {
if(heap[idx] > heap[idx * 2 + 1]){
swap(heap[idx],heap[idx * 2 + 1]);
pw(idx * 2 + 1);
}
}
}
int get(){
int ans = heap[1];
heap[1] = heap[len --];
pw(1);
return ans;
}
int main(){
int n;
cin >> n;
for(int i = 0;i < n;i ++){
cin >> heap[++ len];
put(len);
}
int ans = 0;
while(len > 1){
int xa = get(),ya = get();
ans += xa + ya;
heap[++ len] = xa + ya;
put(len);
}
cout << ans;
return 0;
}
这里空空如也
有帮助,赞一个