Uva 10954 Solution

#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
    int n, i, m;
    while(cin >> n && n>0)
    {
         priority_queue<int, vector<int>, greater<int> >a;
         for(i=1;i<=n;i++)
         {
             cin >> m;
             a.push(m);
         }

         ll t = a.top();
         a.pop();
         t += a.top();
         a.pop();
         a.push(t);
         ll t1 = 0;
         t1 += t;
         t = 0;
         i = 2;
         while(!a.empty())
         {
             t += a.top();
             a.pop();
             i--;
             if(i == 0)
             {
                 if(!a.empty())
                    a.push(t);
                 t1 += t;
                 t = 0;
                 i = 2;
             }
         }
        cout << t1 << "\n";
    }

    return 0;
}

Comments