```cpp
#include <iostream>
#include <queue>
using namespace std;
int main()
{
int t,i,a,r=0;
cin>>t;
priority_queue<int ,vector<int>,greater<int>>h;
for(i=0;i<=t-1;i++)
{
cin>>a;
h.push(a);
}
r=0;
while(h.size()>=2)
{
int a=h.top();
h.pop();
int b=h.top();
h.pop();
int temp=a+b;
r=r+temp;
h.push(temp);
}
cout<<r<<endl;
}
by I_am_a_boy @ 2023-08-08 16:05:21
用优先队列做
by I_am_a_boy @ 2023-08-08 16:06:14
求关注
by I_am_a_boy @ 2023-08-08 16:06:43
@[Suboil](/user/1026350)
```
#include<bits/stdc++.h>
using namespace std;
int heap_size, n;
int heap[30001];
void swap(int &a, int &b) {
int t = a;
a = b;
b = t;
}
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])
return;
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])
return res;
swap(heap[now], heap[next]);
now = next;
}
return res;
}
void work() {
int i, x, y, ans = 0;
cin >> n;
for(i = 1 ; i <= n ; i++) {
cin >> x;
put(x);
}
for(i = 1 ; i < n ; i++) {
x = get();
y = get();
ans += x + y;
put(x + y);
}
cout << ans << endl;
}
int main() {
ios::sync_with_stdio(false);
work();
return 0;
}
```
堆排
by wsbSB @ 2023-08-09 10:11:02
```cpp
#include<algorithm>
#include<iostream>
#include<cstring>
#include<bitset>
#include<cmath>
#include<queue>
using namespace std;
int main(){
priority_queue<int>q;
int n,x,sum=0,a,b;
cin>>n;
for(int i=1;i<=n;i++){
cin>>x;
q.push(-x);
}
while(q.size()>1){
a=-q.top();
q.pop();
b=-q.top();
q.pop();
sum=sum+a+b;
q.push(-a-b);
}
cout<<sum;
return 0;
}
```
求关
by Patrick_Liu_Bingxian @ 2023-08-12 11:46:17
@[I_am_a_boy](/user/977653) 求互关
by Patrick_Liu_Bingxian @ 2023-08-12 11:47:11