fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define int long long
  4. int32_t main()
  5. {
  6. cin.tie(0);
  7. cout.tie(0);
  8. ios::sync_with_stdio(false);
  9. int t=1;
  10. cin>>t;
  11. while(t--)
  12. {
  13. int n,i;
  14. cin>>n;
  15. multiset<int>se;
  16. int ans=0;
  17. for(i=0;i<n;i++)
  18. {
  19. int x;
  20. cin>>x;
  21. if(x==0)
  22. {
  23. if(se.size()>0)
  24. {
  25. ans+=*se.rbegin();
  26. se.erase(se.find(*se.rbegin()));
  27. }
  28. }
  29. else
  30. se.insert(x);
  31. }
  32. cout<<ans;
  33. cout<<'\n';
  34.  
  35. }
  36. }
Success #stdin #stdout 0.02s 6920KB
stdin
Standard input is empty
stdout
0