Advertisement
fooker

P1800C3

Mar 2nd, 2023
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.49 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. void solve()
  5. {
  6.     ll n;
  7.     cin>>n;
  8.     ll k=0;
  9.     multiset <ll> s;
  10.     s.insert(0);
  11.     for (ll i=1; i<=n; i++){
  12.         ll x;
  13.         cin>>x;
  14.         if (x!=0) s.insert(x);
  15.         else {
  16.             auto it=s.end(); it--;
  17.             k+=(*it);
  18.             if (*it!=0) s.erase(s.find(*it));
  19.         }
  20.     }
  21.     cout<<k<<"\n";
  22. }
  23. int main()
  24. {
  25.     int t;
  26.     cin>>t;
  27.     while(t--){
  28.         solve();
  29.     }
  30. }
  31.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement