Add all

Posted on Thu, Dec 9, 2021 進階程設課程
#include <bits/stdc++.h>

using namespace std;

int main()
{
    long long N;
    long long temp;
    long long sum=0;
    long long cost=0;
    priority_queue<long long,vector<int>,greater<int>> pq;
    while(cin>>N){
        if(N==0) break;
        for(int i=0;i<N;i++){
            scanf("%lld",&temp);
            pq.push(temp);
        }
        sum+=pq.top();
        pq.pop();
        for(int i=0;i<N-1;i++){
            sum+=pq.top();
            pq.pop();
            cost+=sum;
        }
        cout<<cost<<endl;
    }
    return 0;
}