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
#include<bits/stdc++.h>
using namespace std;
vector<int> monotonic_queue;
int res = 0;

void PutOnMonotonicQueue(int a) {
    while(a >= monotonic_queue.back()) {
        monotonic_queue.pop_back();
        if(monotonic_queue.empty()) break;
    }
    monotonic_queue.push_back(a);
    res = max(res, (int)monotonic_queue.size());
}

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

    int n, counter = 2, i;
    cin >> n;
    vector<int> arr(n);
    monotonic_queue.reserve(n), monotonic_queue.push_back(0);
    
    for(i = 0; i < n; ++i) cin >> arr[i];
    while(counter--)
        for(i = n - 1; i >= 0; --i)
            PutOnMonotonicQueue(arr[i]);

    cout << res;
}