This should be a simple program, but for some reason it's partially correct?

My solution for the problem TSECS04 appears to be incorrect for some reason? I’ve tested it with large numbers based on the limits but for some reason it’s giving me a partially correct answer. The limits also do not include negative numbers so that wouldn’t be the issue with the function. Does anybody wanna help me figure out where the screw up is?

Consider the test input:

1 3
1 2
1 1
1 Like

Thank you! The test case made me realize that I forgot to put a loop for my stack. Looks like it exceeds a time limit though, but I think I can sort that out myself.

1 Like

Nevermind, how come this gives a SIGSEGV erorr?

#include <iostream>
#include <stack>
using namespace std;

int main() {
	stack<int> stk;
    int m; cin >> m;
    int max{};
    int top{};
    while(m--) {
        int cmd; cin >> cmd;
        switch(cmd) {
            case 1:
                cin >> top;
                if (top > max) max = top;
            case 2:
                if (top == max) max =;
                top =;
            case 3:
                cout << max << endl;
	return 0;

Consider an instance where the stack consists of exactly 1 element, and the query is 2.

  • stk.pop() will remove the only element present in the stack
  • if(top == max) max = is trying to refer the top element of the stack that is empty, which will obviously result in a Runtime error.

I see, thank you! I wouldn’t have thought of something like this.