fork download
  1. #include <bits/stdc++.h>
  2. #include <set>
  3. using namespace std;
  4.  
  5. int main() {
  6. int t ; cin>>t;
  7. while(t--) {
  8. int n ; cin >> n;
  9. multiset <int> s ;
  10. long long sum = 0;
  11. for(int i = 0 ; i < n ; i++) {
  12. int x ;
  13. cin>>x;
  14. if(x!=0) {
  15. s.insert(x);
  16. }
  17. else {
  18. if(!s.empty()) {
  19. auto it = s.end();
  20. it--;
  21. sum+=*it;
  22. s.erase(it);
  23. }
  24. }
  25. }
  26. cout<<sum<<endl;
  27. }
  28.  
  29. }
Success #stdin #stdout 0s 5304KB
stdin
Standard input is empty
stdout
Standard output is empty