fork download
  1. #include <iostream>
  2. #include <stack>
  3. using namespace std;
  4.  
  5. int main() {
  6. int q;
  7. cin>>q;
  8. stack<int> s;
  9. stack<int> max;
  10. for(int i=0;i<q;i++)
  11. {
  12. char query;
  13. cin>>query;
  14. if(query=='A')
  15. {
  16. int detail;
  17. cin>>detail;
  18. s.push(detail);
  19. if(max.empty())
  20. max.push(detail);
  21. else if(detail>max.top())
  22. max.push(detail);
  23. }
  24. else if(query=='R')
  25. {
  26. if(!s.empty())
  27. {
  28. if(s.top()==max.top())
  29. max.pop();
  30. s.pop();
  31. }
  32. }
  33. else
  34. {
  35. if(max.empty())
  36. cout<<"Empty\n";
  37. else
  38. cout<<max.top()<<"\n";
  39. }
  40. }
  41. }
Success #stdin #stdout 0s 3476KB
stdin
4 A 1 A 1 R Q
stdout
Empty