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
#include <iostream>
#include <vector>

using namespace std;
typedef long long ll;

const size_t N = 1'000'000;
vector<int> a, b;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int i, n;
    size_t max = 0;
    cin >> n;
    a.resize(2*n); b.reserve(2*n+1);
    for (i=0; i<n; i++) {
        cin >> a[i];
        a[n+i] = a[i];
    }
    for (i=2*n-1; i>=0; i--) {
        while (b.size()>0 && b.back()<=a[i])
            b.pop_back();
        b.push_back(a[i]);
        if (b.size()>max)
            max = b.size();
    }
    cout << max << '\n';
    return 0;
}