public static long hist(int[] arr, StacksUsingArrays stack)
throws Exception {
int i=0;
long max=0;
while(i<arr.length){
if(stack.isEmpty()||arr[i]>=arr[stack.top()]){
stack.push(i++);
}
else{
int top=stack.pop();
int area=(stack.isEmpty()?arr[top]*1:arr[top]*(i-stack.top()-1));
max=Math.max(max,area);
}
}
while(!stack.isEmpty()){
int top=stack.pop();
int area=(stack.isEmpty()?arr[top]*1:arr[top]*(i-stack.top()-1));
max=Math.max(max,area);
}
return max;
}