fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define GG ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
  4. int main () { GG;
  5. int t; cin>>t;
  6. while(t--) {
  7. int n,x; cin>>n;
  8. long long sum=0;
  9. priority_queue<int> pq;
  10.  
  11. for(int i=0;i<n;i++)
  12. { cin>>x;
  13. if(x!=0)
  14. pq.push(x);
  15. else if(pq.size()!=0)
  16. { sum+=pq.top();
  17. pq.pop();
  18. }
  19. }
  20. cout<<sum<<"\n";
  21. }return 0;}
Success #stdin #stdout 0.01s 5300KB
stdin
5
5
3 3 3 0 0
6
0 3 3 0 0 3
7
1 2 3 0 4 5 0
7
1 2 5 0 4 3 0
5
3 1 0 0 4
stdout
6
6
8
9
4