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
#include <bits/stdc++.h>

using namespace std;

signed main(){
  ios_base::sync_with_stdio(0); cin.tie(0);
  int n; cin >> n;
  deque<int> A(n); int mx=0; for(auto &a : A) {cin >> a; mx=max(mx, a);}
  reverse(A.begin(), A.end());
  while(A[0] != mx){
    A.push_front(A.back());
    A.pop_back();
  }

  stack<int> S ;
  S.push(A[0]);
  int res = 1;
  for(int i=1; i<n; i++){
    while(S.size() && S.top() <= A[i]) S.pop();
    S.push(A[i]);
    res = max(res, (int)S.size());
  }
  cout << res << '\n';

}