Wednesday, July 27, 2016

UVA 10954 - Add All

#include <bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(0)
#define dbug(x) cout<<x<<" "
using namespace std;
int main()
{
    FAST;
    int t;
    while(cin>> t && t){
        priority_queue<int ,vector<int>, greater<int> >pq;
        int sum=0 , cost=0;
        for(int i=0; i<t; i++){
            int x;
            cin>>x;
            pq.push(x);
        }
        if(t==1){cout<<pq.top();continue;}
        for(int i=1; i<t; i++){
                int x=pq.top();
                pq.pop();
                x+=pq.top();
                sum+=x;
                pq.pop();
                pq.push(x);
        }
        cout<<sum<<'\n';
    }
    return 0;
}

No comments:

Post a Comment