#include <stdio.h> int split(int A[],int low,int high) { int i=low,k; int temp; for(k=low+1;k<=high;k++) { if(A[k]<A[low]) { i++; if(i!=k) temp=A[i],A[i]=A[k],A[k]=temp; }} temp=A[i],A[i]=A[low],A[low]=temp; return i; } void quick_sort(int A[],int low,int high) { int k; if(low<high) { k=split(A,low,high); quick_sort(A,low,k-1); quick_sort(A,k+1,high); } } int main() { int n,*A,i,j,k,l,s,a[2],sum; while(scanf("%d",&n)!=EOF) { A=new int[2*n+1]; for(i=0;i<n;i++) scanf("%d",&A[i]); quick_sort(A,0,n-1); i=0,j=k=n; sum=0; for(s=1;s<=n-1;s++) { for(l=0;l<=1;l++) { if(i<n) { if(j<k && A[j]<A[i]) a[l]=A[j++]; else a[l]=A[i++]; continue; } else a[l]=A[j++]; } sum+=a[0]+a[1]; A[k++]=a[0]+a[1]; } printf("%d\n",sum); delete[] A; } return 0; }
相关分类