cpp```
#include<bits/stdc++.h>
using namespace std;
int top = 0;
int a[100010], b[100010], stk[100010];
int main()
{
int n;
cin >> n;
int i = 0;
while(i<n)
{
cin >>a[i];
if(a[i]==0)
{
cin >> b[i];
top++;
stk[top] = b[i];
}
if(a[i]==1)
{
if(top!=0)
{
top--;
}
}
if(a[i]==2)
{
if(top==0)
{
cout << 0 << "\n";
}else
{
int max = stk[1];
for(int j = 1;j <= top;j++)
{
if(max < stk[j])
{
max = stk[j];
}
}
cout << max << "\n";
}
}
i++;
}
return 0;
}