long maxArea = 0;
int top;
long topArea;
int i = 0;
while (i < n) {
if (stack.isEmpty() || arr[stack.top()] <= arr[i]) {
stack.push(i++);
} else {
top = stack.top();
stack.pop();
topArea = arr[top] * (stack.isEmpty() ? i : i - stack.top() - 1);
if (maxArea < topArea)
maxArea = topArea;
}
}
while (stack.isEmpty() == false) {
top = stack.top();
stack.pop();
topArea = arr[top] * (stack.isEmpty() ? i : i - stack.top() - 1);
if (maxArea < topArea)
maxArea = topArea;
}
return maxArea;