//7976667 vrs 3253 Accepted 228K 47MS C 1334B 2010-12-06 23:03:13
//3253 锯木板 贪心+堆优先队列
//遇到一个问题,变量声明要放在最前面,不然POJ编译不通过
#include<stdio.h>
#define bool int
#define MAXNUM 20005
unsigned int n;
unsigned int wood[MAXNUM];
void swap(int i,int j)
{
wood[0]=wood[i];
wood[i]=wood[j];
wood[j]=wood[0];
}
void Insert(unsigned int data)
{
unsigned int father,son;
n++;
wood[n]=data;
son=n;
while(son>1)
{
father=son/2;
if(wood[father]>wood[son])
swap(father,son);
else
break;
son=father;
}
}
unsigned int Top()
{
return 1;
}
bool IsEmpty()
{
if(n==0)
return 1;
else
return 0;
}
void Pop()
{
unsigned int father,son;
swap(1,n);
n--;
father=1;
while(father<=n/2)
{
son=father*2;
if(son+1<=n && wood[son]>wood[son+1])
son++;
if(wood[father]>wood[son])
swap(father,son);
father=son;
}
}
int main()
{
unsigned int N;
unsigned long long sum;
unsigned long long currSum,min1,min2;
unsigned int temp;
n=0;
while(scanf("%d",&N)!=EOF)
{
while(N--)
{
scanf("%d",&wood[0]);
Insert(wood[0]);
}
if(1==N)
{
printf("0\n",sum);
continue;
}
sum=0;
currSum=0;
while(1)
{
temp=Top();
min1=wood[temp];
Pop();
temp=Top();
min2=wood[temp];
Pop();
min1=min1+min2;
sum+=min1;
if(IsEmpty())
break;
Insert(min1);
}
printf("%lld\n",sum);
}
return 0;
}