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
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve()
{
    ll n=0,ans=0; vector<ll> A,ANS; vector<pair<ll,ll>> W;
    cin>>n; ANS.assign(2*n+12,0);
    for(int i=0; i<n; ++i){ll KWA; cin>>KWA; A.push_back(KWA);}
    for(int i=0; i<n; ++i){A.push_back(A[i]);}
    for(int i=0; i<2*n; ++i)
    {
        while((int)W.size()>0 and W[(int)W.size()-1].first<A[i]) W.pop_back();
        if((int)W.size()==0) {if(i-n+1<0) ++ANS[0]; else ++ANS[i-n+2]; --ANS[i+1];}
        else
        {
            int dx=(int)W.size()-1;
            if(i-W[dx].second+1>n) ++ANS[i-n+2];
            else ++ANS[W[dx].second+1];
            --ANS[i+1];
        }
        W.push_back({A[i],i});
    }
    ll suma=0;
    for(int i=0; i<2*n; ++i)
    {
        suma+=ANS[i];
        ans=max(ans,suma);
    }
    cout<<ans;
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int MAKUMBA=1;
    //cin>>MAKUMBA;
    for(int KOKOJAMBO=0; KOKOJAMBO<MAKUMBA; ++KOKOJAMBO){solve();}
    return 0;
}