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
//fast
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;

#define rep(n) for(int i = 0 ; i<n ; i++)
#define all(x) x.begin(),x.end()
#define pb push_back

const int base = (1<<20);
int tree[2*base];

void upd(int x, int c){
	x+=base;
	while (x>=1){
		tree[x] = c;
		x/=2;
	}
}

int zap(int a, int b){
	a+=base-1;
	b+=base+1;
	int w = 0;
	while (b-a>1){
		if (a%2==0) w = max(w,tree[a+1]);
		if (b%2) w = max(w,tree[b-1]);
		a/=2;
		b/=2;
	}
	return w;
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n;
	cin >> n;
	int a[2*n];
	rep(n) cin >> a[i+1];
	rep(n-1) a[i+n+1] = a[i+1];
	int w[2*n] = {0};
	for (int i = 1 ; i<2*n ; i++){
		w[zap(a[i],base-1)+1]++;
		upd(a[i],i);
	}
	int sum = 0;
	int maks = 0;
	for (int i = 1 ; i<2*n ; i++){
		sum+=w[i];
		maks = max(maks,sum);
		sum--;
	}
	cout << maks << '\n';
}