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
#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
using pi = pair<int, int>;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n; cin >> n;
    vector<int> a(2 * n + 1), cum(2 * n + 2);
    stack<pi> stos; stos.push({1e9, 0});
    for (int i = 1; i <= 2 * n; i++) {
        if (i <= n) {
            cin >> a[i];
            a[i + n] = a[i];
        }
        while (stos.top().f < a[i]) stos.pop();
        int from = stos.top().s + 1;
        stos.push({a[i], i});
        cum[from]++;
        cum[i + 1]--;
    }
    int ans = 0;
    for (int i = 1; i <= 2 * n; i++) {
        // cout << i << " " << cum[i] << endl;
        cum[i] += cum[i - 1];
        ans = max(ans, cum[i]);
    }
    cout << ans << "\n";
}