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

using namespace std;

int main(){
    cin.tie(0)->sync_with_stdio(0);
    int n; cin>>n;
    vector<int> values(2 * n);
    for(int i = 0; i<n; i++){
        int a; cin>>a;
        values[i] = a;
        values[i + n] = a;
    }

    deque<pair<int, int>> stck;
    int out = 0;

    for(int i = values.size() - 1; i >= 0; i--){
        while(stck.size() and stck.front().first <= values[i])
            stck.pop_front();
        if(stck.size() and stck.back().second % n == i % n) stck.pop_back();
        stck.push_front({values[i], i});
        out = max(out, (int)stck.size());
    }

    cout<<out<<endl;
    return 0;   
}