# 最优合并问题

``````4
5 12 11 2``````

``78 52``

``````#include<iostream>
#include<algorithm>
#include<vector>
#include<functional>
#include<queue>
using namespace std;
struct mycmp
{
bool operator()(const int &a,const int &b){
return a>b;
}
};

int main(){
priority_queue<int>x1;
priority_queue<int,vector<int>,mycmp>x2;
int k,i,sum=0,temp,num1,num2;
int a[2000];
cin>>k;
for(i=0;i<k;i++){
cin>>a[i];
x1.push(a[i]);
x2.push(a[i]);
}
sum=0;
temp=0;
while(temp!=k-1){
num1=x1.top();
x1.pop();
num2=x1.top();
x1.pop();
sum=sum+num1+num2-1;
x1.push(num1+num2);
temp++;
}
cout<<sum<<" ";
temp=0;
sum=0;
while(temp!=k-1){
num1=x2.top();
x2.pop();
num2=x2.top();
x2.pop();
sum=sum+num1+num2-1;
x2.push(num1+num2);
temp++;
}
cout<<sum;

return 0;
}
``````

``````#include<iostream>
#include<algorithm>
#include<vector>
#include<functional>
#include<queue>
using namespace std;
struct mycmp
{
bool operator()(const int &a,const int &b){
return a>b;
}
};

int main(){
priority_queue<int>x1;
priority_queue<int,vector<int>,mycmp>x2;
int k,i,sum=0,temp,num1,num2;
int a[2000];
cin>>k;
for(i=0;i<k;i++){
cin>>a[i];
x1.push(a[i]);
x2.push(a[i]);
}
sum=0;
temp=0;
while(temp!=k-1){
num1=x1.top();
x1.pop();
num2=x1.top();
x1.pop();
sum=sum+num1+num2-1;
x1.push(num1+num2);
temp++;
}
cout<<sum<<" ";
temp=0;
sum=0;
while(temp!=k-1){
num1=x2.top();
x2.pop();
num2=x2.top();
x2.pop();
sum=sum+num1+num2-1;
x2.push(num1+num2);
temp++;
}
cout<<sum;

return 0;
}
``````