Please help me with the following code

package com.codechef.javaapp;

import java.util.*;
public class Main {

public static void main(String[] args) throws Exception {
    Scanner s = new Scanner(System.in);

    int n = s.nextInt();
    int[] arr = new int[n];

    for(int i = 0;i < n;i++)
        arr[i] = s.nextInt();

    Main mainobj = new Main();
    StacksUsingArrays stack = mainobj.new StacksUsingArrays(1000);
    System.out.println(hist(arr, stack));
}


public static long hist(int[] arr, StacksUsingArrays s) throws Exception
{
    s = new StacksUsingArrays();

    int max_area = 0; 
    int tp;   
    int area_with_top;
    int n = arr.length;

   
    int i = 0;
    while (i < n)
    {
       
        if (s.isEmpty() || arr[s.top()] <= arr[i])
            s.push(i++);
        else
        {
            tp = s.top();  
            s.pop();  
            
            area_with_top = arr[tp] * (s.isEmpty() ? i : i - s.top() - 1);
            
            if (max_area < area_with_top)
                max_area = area_with_top;
        }
    }
    while (s.isEmpty() == false)
    {
        tp = s.top();
        s.pop();
        area_with_top = arr[tp] * (s.isEmpty() ? i : i - s.top() - 1);

        if (max_area < area_with_top)
            max_area = area_with_top;
    }

    return max_area;

}

}
class StacksUsingArrays {
private int[] data;
private int tos;

public static final int DEFAULT_CAPACITY = 10;

public StacksUsingArrays() throws Exception {
    // TODO Auto-generated constructor stub
    this(DEFAULT_CAPACITY);
}

public StacksUsingArrays(int capacity) throws Exception {
    if (capacity <= 0) {
        System.out.println("Invalid Capacity");
    }
    this.data = new int[capacity];
    this.tos = -1;
}

public int size() {
    return this.tos + 1;
}

public boolean isEmpty() {
    if (this.size() == 0) {
        return true;
    } else {
        return false;
    }
}

public void push(int item) throws Exception {
    if (this.size() == this.data.length) {
        throw new Exception("Stack is Full");
    }
    this.tos++;
    this.data[this.tos] = item;
}

public int pop() throws Exception {
    if (this.size() == 0) {
        throw new Exception("Stack is Empty");
    }
    int retVal = this.data[this.tos];
    this.data[this.tos] = 0;
    this.tos--;
    return retVal;
}

public int top() throws Exception {
    if (this.size() == 0) {
        throw new Exception("Stack is Empty");
    }
    int retVal = this.data[this.tos];
    return retVal;
}

public void display() throws Exception {
    if (this.size() == 0) {
        throw new Exception("Stack is Empty");
    }
    for (int i = this.tos; i >= 0; i--) {
        System.out.println(this.data[i]);
    }

}

}

Hello @8006366388,
I have corrected your code for errors. Also you need to use long instead of int. https://ide.codingblocks.com/s/152536 Here is the code. Kindly understand your mistakes. We need to use long because test cases are huge and we get Wrong Answer if we use int.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.