1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| #include <bits/stdc++.h> #define ll long long #define int long long #define Orz ios::sync_with_stdio(0),cin.tie(0) #define N 51 #define FOR(i,n) for(int i=0;i<n;i++) #define pii pair<int,int> #define pid pair<int,double> #define pdi pair<double,int> using namespace std; int n,m;
void solve(){ while(cin>>n && n){ priority_queue<int,vector<int>,greater<int>> pq; for(int i=0;i<n;i++){ int temp;cin>>temp; pq.push(temp); } int ans = 0; while(pq.size()!=1){ int cur = pq.top(); pq.pop(); cur += pq.top(); pq.pop(); ans+=cur; pq.push(cur); } cout<<ans<<endl; } }
signed main(){ Orz; int t=1; while(t--){ solve(); } }
|