poj3253

/** \brief poj 3253
 *
 * \param date 2014/8/8
 * \param state AC
 * \return memory 1124K time 125ms
 *
 */

#include <iostream>
#include <fstream>
#include <queue>
#include <functional>

using namespace std;

struct number
{
    //int x;
    __int64 x;
    bool operator < (const number& a) const{
         return x>a.x;//最小值优先
    }

};

priority_queue<number>que;

//int total,mincost;
__int64 total,mincost;

int main()
{
    //cout << "Hello world!" << endl;
    //freopen("input.txt","r",stdin);
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        number num;
        for(int i=0;i<n;i++)
        {
            //int v;
            __int64 v;
            cin>>v;
            num.x=v;
            que.push(num);
        }
        mincost=0;
        total=0;
        while(que.size()>1)
        {
            number v1,v2,temp;
            v1=que.top();
            que.pop();

            v2=que.top();
            que.pop();

            total=(v1.x+v2.x);

            temp.x=total;

            que.push(temp);
            mincost+=total;
        }
        cout<<mincost<<endl;

        while(!que.empty())
            que.pop();
    }
    return 0;
}



你可能感兴趣的:(poj)