int a[100]={2147483647};
ins(int z,int x)
{
while(x>a[z]) a[z+1]=a[z--];
a[z+1]=x;}
int main()
{int i,s=0,n,j,k;
cin>>n;
for(i=1;i<=n;i++){
cin>>a[i];
ins(i-1,a[i]);
}
for(j=n-1;j>0;j--){
s+=k=a[j]+a[j+1];
ins(j-1,k);
}
cout<<s;
return 0;}
........................................................................................................................................................................................
#include <iostream>
using namespace std;
int fd(int *a,int n)
{
int t=a[0],k=0;
for (int i=1;i<n;i++)
if (a[i]<t)
{
a[k++]=a[i];
a[i]=a[k];
}
a[k]=t;
return k;
}
void kp(int *a,int n)
{
if (n>1)
{
int m=fd(a,n);
kp(a,m);
kp(a+m+1,n-m-1);
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "rt", stdin);
freopen("output.txt", "wt", stdout);
#endif
int n;
cin>>n;
int a[10003],b[10003];
memset(b,63,sizeof(b));
a[n]=a[n+1]=1000000000;
for (int i=0;i<=n-1;i++)
cin>>a[i];
kp(a,n);
int ta=0,tb=1,s=0;
for (int i=1;i<=n-1;i++)
{
if (a[ta+1]<b[tb])
{
s+=b[i]=a[ta]+a[ta+1];
ta+=2;
}else
{
if (b[tb+1]>a[ta])
{
s+=b[i]=a[ta]+b[tb];
ta++;
tb++;
}else
{
s+=b[i]=b[tb]+b[tb+1];
tb+=2;
}
}
}
cout<<s<<endl;
return 0;
}