fork download
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. struct cmp{
  6. bool operator()(long long a, long long b){
  7. if(abs(a)==abs(b))
  8. return a>b;
  9. return abs(a)>abs(b);
  10. }
  11. };
  12.  
  13. int main(){
  14. int n, x;
  15. priority_queue<long long, vector<long long>, cmp>pq;
  16.  
  17. cin>>n;
  18.  
  19. while(n--){
  20. cin>>x;
  21.  
  22. if(x==0){
  23. if(pq.empty())
  24. cout<<0<<'\n';
  25. else{
  26. cout<<pq.top()<<'\n';
  27. pq.pop();
  28. }
  29. }
  30. else
  31. pq.push(x);
  32. }
  33.  
  34. return 0;
  35. }
Success #stdin #stdout 0s 5316KB
stdin
18
1
-1
0
0
0
1
1
-1
-1
2
-2
0
0
0
0
0
0
0
stdout
-1
1
0
-1
-1
1
1
-2
2
0