题意: 新定义一种运算。每次运算都会产生一种cost。让总的cost最小。简单贪心。
使用优先队列轻松解决。
运算时两个数相加后产生的数会被再次使用。同时这个数还要加入到cost里面‘
//
// uva10954.cpp
// greedy
//
// Created by ni ni on 15/4/26.
// Copyright (c) 2015年 ni ni. All rights reserved.
//
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
struct cmp
{
bool operator () (int a,int b)
{
return a>b;
}
};
int main()
{
int a[100005],n;
while(~scanf("%d",&n) && n!=0){
priority_queue<int,vector<int>,cmp> que;
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
que.push(a[i]);
}
long long sum=0;
while(que.size()>1){
int a,b,c=0;
a=que.top();
c+=a;
que.pop();
b=que.top();
que.pop();
c+=b;
sum+=c;
que.push(c);
}
while(!que.empty()){
que.pop();
}
printf("%lld\n",sum);
}
return 0;
}