fork download
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <bits/stdc++.h>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. #define ll long long
  7. #define ld long double
  8.  
  9.  
  10. int main() {
  11. // your code goes here
  12.  
  13. ll n;
  14. cin>> n;
  15. stack<ll> sk;
  16. while(n--)
  17. {
  18. int op;
  19. cin>> op;
  20.  
  21. if(op==1)
  22. {
  23. ll x;
  24. cin>> x;
  25.  
  26. if(sk.empty())
  27. sk.push(x);
  28. else
  29. sk.push(max(x, sk.top()));
  30.  
  31. cout<< sk.top() << "\n";
  32.  
  33.  
  34. }
  35. else
  36. {
  37. if(!sk.empty())
  38. sk.pop();
  39.  
  40. if(sk.empty())
  41. cout<< "Empty!\n";
  42. else
  43. cout<< sk.top() << "\n";
  44. }
  45. }
  46. return 0;
  47. }
Success #stdin #stdout 0s 5312KB
stdin
5
1 5
1 3
1 6
2
2
stdout
5
5
6
5
5