Advertisement
hung_mine

884D

Oct 9th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. /// from HUNG MINE with love <3
  5. long long aa[200001], res = 0, n;
  6. priority_queue <long long, vector <long long>, greater <long long> > q;
  7. int main () {
  8.       //freopen (".inp", "r", stdin);
  9.       //freopen (".out", "w", stdout);
  10.       ios_base :: sync_with_stdio (0);
  11.       cin.tie (0);
  12.       cout.tie (0);
  13.       cin >> n;
  14.       for (long long i = 1; i <= n; ++ i) {
  15.             cin >> aa[i];
  16.             q.push (aa[i]);
  17.       }
  18.       if (n % 2 == 0) {
  19.             q.push (0);
  20.       }
  21.       while (q.size () > 1) {
  22.             long long a = q.top ();
  23.             q.pop ();
  24.             long long b = q.top ();
  25.             q.pop ();
  26.             long long c = q.top ();
  27.             q.pop ();
  28.             res += (a + b + c);
  29.             q.push (a + b + c);
  30.       }
  31.       cout << res;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement