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
#include <bits/stdc++.h>

typedef long long ll;

using namespace std;

int main(){
    cin.tie(0)->sync_with_stdio(0);

    int n;
    cin >> n;

    vector<int> tab(2*n+1);
    vector<int> add(2*n+1);

    for(int i = 1; i <= n; i++){
        cin >> tab[i];
        tab[n+i] = tab[i];
    }

    set<pair<int,int>> s;
    s.insert({1e9, 0});
    for(int i = 1; i <= 2*n; i++){
        auto it = s.lower_bound({tab[i], -1});
        // cout << i << " " << tab[i] << " " << it->second+1 << endl;
        add[it->second+1]++;
        while(s.begin()->first <= tab[i])s.erase(s.begin());
        s.insert({tab[i], i});
    }

    int res = 0;
    int ans = 0;
    for(int i = 1; i <= n; i++){
        res += add[i];
        ans = max(ans, res);
        res--;
    }
    cout << ans << '\n';

    return 0;
}