import
java.io.*;
import
java.util.*;
class
GFG {
public
static
void
leftindex(
int
heights[],
int
l_heights[],
int
n)
{
Stack<Integer> st =
new
Stack<Integer>();
for
(
int
i =
0
; i < n; i++) {
while
(st.empty() ==
false
&& heights[st.peek()] < heights[i])
st.pop();
l_heights[i]
= (st.empty() ==
false
) ? st.peek() : (-
1
);
st.push(i);
}
return
;
}
public
static
void
rightindex(
int
heights[],
int
r_heights[],
int
n)
{
Stack<Integer> st =
new
Stack<Integer>();
for
(
int
i = n -
1
; i >=
0
; i--) {
while
(st.empty() ==
false
&& heights[st.peek()] < heights[i])
st.pop();
r_heights[i]
= (st.empty() ==
false
) ? st.peek() : n;
st.push(i);
}
return
;
}
public
static
int
max_people(
int
heights[],
int
n)
{
int
ans =
0
;
int
r_heights[] =
new
int
[n];
int
l_heights[] =
new
int
[n];
leftindex(heights, l_heights, n);
rightindex(heights, r_heights, n);
for
(
int
i =
0
; i < n; i++) {
int
l_index = l_heights[i];
int
r_index = r_heights[i];
ans = Math.max(ans, r_index - l_index -
1
);
}
return
ans;
}
public
static
void
main(String[] args)
{
int
N =
7
;
int
heights[] = {
6
,
2
,
5
,
4
,
5
,
1
,
6
};
System.out.println(max_people(heights, N));
}
}