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
55
56
#include <bits/stdc++.h>
#define un unsigned
#define rep(i, a, b) for(ll i = a; i < b; i++)
#define per(i, a, b) for(ll i = a; i >= b; i--)
#define all(v) begin(v), end(v)
#define st first
#define nd second
using namespace std;
using ll = long long;
using bigi = __int128;
using pii = pair<ll, ll>;
const ll N = 1e6 + 5;
int tab[N];
int gle[N];
int fmaa[N];
int sufma[N];
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    rep(i, 0, n){
        cin >> tab[i];
    }
    vector<pair<int, int>> sta;
    sta.push_back({1e9, n});
    sufma[n] = -1;
    per(i, n - 1, 0){
        sufma[i] = max(sufma[i + 1], tab[i]);
        while(sta.back().st <= tab[i]){
            sta.pop_back();
        }
        gle[i] = gle[sta.back().nd] + 1;
        fmaa[i] = sta.back().nd;
        // cout << i << ' ' << fmaa[i] << '\n';
        sta.push_back({tab[i], i});
    }
    int odp = gle[0];
    int maks = -1;
    vector<pair<int, int>> allv;
    rep(i, 0, n){
        if(tab[i] > maks){
            maks = tab[i];
            allv.push_back({tab[i], i});
        }
        int dlai = gle[i + 1];
        // cout << i << ' ' << dlai << '\n';
        int fm = sufma[i + 1];
        int g = upper_bound(all(allv), pair<int, int>{fm, 1e9}) - allv.begin();
        // if(int(dlai + allv.size() - g) > odp){
        //     cout << i << ' ' << dlai << '\n';
        // }
        odp = max(odp, int(dlai + allv.size() - g));
    }
    cout << odp << '\n';
}