fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main() {
  4. int a; cin >> a;
  5. stack<int> s;
  6. while (a--) {
  7. int b; cin >> b;
  8. if (b == 1) {
  9. int c; cin >> c;
  10. s.push(c);
  11. } else if (b == 2) {
  12. if (!s.empty()) s.pop();
  13. } else {
  14. if (s.empty()) cout << -1 << "\n";
  15. else cout << s.top() << "\n";
  16. }
  17. }
  18. }
  19.  
Success #stdin #stdout 0.01s 5288KB
stdin
8
1 2
1 3
2
3
2
3
1 5
3
stdout
2
-1
5