static long[] arr; > static ArrayList<Integer> stack; > public static void main(String[] argh) throws Exception{ > BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); > while(true){ > int n= Integer.parseInt(br.readLine().trim()); > if(n == 0) break; > String[] sr= br.readLine().trim().split(" "); > arr= new long[n]; > for(int kk=0; kk<n; kk++){ > arr[kk]= Long.parseLong(sr[kk]); > } > stack= new ArrayList<>(); > long max= -1; > int i=0; > long area_with_top=0; > for(i=0; i<n; ){ > if(stack.size() == 0 || arr[stack.get(0)] <= arr[i]) stack.add(0, i++); > else { > int pI= stack.get(0); > stack.remove(0); > area_with_top = (long)(arr[pI] * (stack.size() == 0 ? (long)(i) : (long)(i - stack.get(0) - 1))); > if(max < area_with_top) max= area_with_top; > } > } > while(stack.size() != 0) { > int pI= stack.get(0); > stack.remove(0); > area_with_top = (long)(arr[pI] * (stack.size() == 0 ? (long)(i) : (long)(i - stack.get(0) - 1))); > if(max < area_with_top) max= area_with_top; > } > System.out.println(max); > } > } I have used long in there, but still RE. :frowning:
I have tried it with big integer. but getting same result
Test it with the input example in the problem, then take another look at the input format.
It is giving correct output with all the test case I tried. RE is when I submit.
And you tried it with the test cases from the problem statement? In the same format as given?
The code has two readLines, so it looks like you expect each test case to be on two lines, not one.
now its TLE after correcting the mistake.