Wednesday, March 2, 2016

UVa 10954 - Add All

#include <bits/stdc++.h>
#define LL  long long
#define SZ(x) ((int)(x).size())
#define pri(a) cout<<a<<endl
#define prii(a,b) cout<<a<<" "<<b<<endl
#define priii(a,b,c) cout<<a<<" "<<b<<" "<<c<<endl
using namespace std;

const int INF = 1<<29;
const int MX  = 1e5+10;


///** The minimum cost of summation u get from -
///** updating the container given numbers and the minimum sum - adding the minimum most two numbers from the container
///** declare priority queue where u need to empty the pq each time;

int main()
{
    LL n,x;

    while(cin>>n and n)
    {
        priority_queue < int, vector<int>, greater<int> > pq;

        while(n--)
        {
            cin>>x;
            pq.push(x);
        }
        LL cst = 0;
        while(SZ(pq)>1)
        {
            LL x = pq.top(); pq.pop();
            LL y = pq.top(); pq.pop();
            LL sm = x + y;
            pq.push(sm);
            cst += sm;
        }
        pri(cst);
    }

    return 0;
}

No comments:

Post a Comment