POJ_3253_Fence Repair(哈夫曼树)
1 # include <stdio.h> 2 # include <algotithm> 3 int main() 4 { 5 int n,i,j,temp; 6 __int64 a[20000],sum=0,count=0,b; 7 scanf("%d",&n); 8 for(i=0;i<n;i++) 9 scanf("%I64d",&a[i]); 10 std::sort(a,a+n); 11 for(i=0;i<n-1;i++) 12 { 13 b=a[i]+a[i+1]; 14 j=i+1; 15 while(b>a[j]&&j<n) 16 { 17 a[j-1]=a[j]; 18 j++; 19 } 20 a[j-1]=b; 21 sum=sum+b; 22 } 23 printf("%I64d",sum); 24 return 0; 25 }