-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrecycling.java
51 lines (40 loc) · 1.23 KB
/
recycling.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.util.*;
public class Main {
public static int mathod(int[] arrayy) {
Stack<Integer> st = new Stack<>();
int answer = 0;
for (int i = 0; i < arrayy.length; i++) {
while (!st.isEmpty() && arrayy[i] < arrayy[st.peek()]) {
int r = i;
int h = arrayy[st.pop()];
if (st.isEmpty()) {
answer = Math.max(answer, h * r);
} else {
int l = st.peek();
answer = Math.max(answer, h * (r - l - 1));
}
}
st.push(i);
}
int r = arrayy.length;
while (!st.isEmpty()) {
int h = arrayy[st.pop()];
if (st.isEmpty()) {
answer = Math.max(answer, h * r);
} else {
int l = st.peek();
answer = Math.max(answer, h * (r - l - 1));
}
}
return answer;
}
public static void main (String args[]) {
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();
}
System.out.println(mathod(arr));
}
}