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