This code is accepted on Leetcode but give the wrong answer on hackerblock. Why it is showing the wrong answer?
#include<iostream>
#include<stack>
int max(int a, int b){
return a>b? a: b;
}
using namespace std;
int main() {
int n; cin>>n;
int arr[n+1]={0};
for(int i=0; i<n; i++) cin>>arr[i];
stack<int> s;
int res=0;
for(int i=0; i<n; i++){
while(!s.empty() && arr[s.top()]>arr[i]){
int temp = s.top();
s.pop();
if(s.empty()) res=max(res, arr[temp]*i);
else res=max(res, arr[temp]*(i-s.top()-1));
}
s.push(i);
}
while(!s.empty()){
int temp = s.top();
s.pop();
if(s.empty()) res=max(res, arr[temp]*n);
else res=max(res, arr[temp]*(n-s.top()-1));
// cout<<res<<"\n";
}
cout<<res;
return 0;
}