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
52
53
54
#include<bits/stdc++.h>
using namespace std;

const int N = 1e6 + 2;
int tab[2 * N];
int fistBiggerToR[2 * N];
stack<int> S;

void construct(int l, int r){
    vector<int> vec;
    while(l < r){
        vec.push_back(l);
        l = fistBiggerToR[l];
    }
    for(int i = vec.size() - 1 ; i >= 0 ; i--){
        S.push(vec[i]);
    }
}

int solve(int n){
    vector<int> candidates;
    for(int i = 2 * n - 1 ; i >= 0 ; i--){
        while(!candidates.empty() && tab[candidates.back()] <= tab[i]){
            candidates.pop_back();
        }
        if(!candidates.empty()) fistBiggerToR[i] = candidates.back();
        else fistBiggerToR[i] = 2 * n + 2;
        candidates.push_back(i);
    }
    construct(0, n);
    int ans = S.size();
    for(int i = 0 ; i < n ; i++){
        S.pop();
        if(!S.empty()){
            construct(i + 1, S.top());
        }else{
            construct(i + 1, i + n + 1);
        }
        ans = max(ans, (int)S.size());
    }
    return ans;
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    for(int i = 0 ; i < n ; i++){
        cin >> tab[i];
        tab[i + n] = tab[i];
    }
    cout << solve(n) << '\n';
}