注意会卡空间...
/* Footprints In The Blood Soaked Snow */
#include <cstdio>
#include <algorithm>
using namespace std;
typedef unsigned int uint;
typedef long long LL;
const int maxn = 1000005, inf = 0x3f3f3f3f;
int n, last[maxn], next[maxn], ans;
uint sum[maxn];
bool vis[maxn];
inline int iread() {
int f = 1, x = 0; char ch = getchar();
for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
return f * x;
}
inline void calc(int x) {
int len = 0, r = inf;
for(int i = x + 1; i <= n && sum[i] - sum[i - 1] != 1; i++) {
int now = sum[i] - sum[i - 1];
if(!vis[now]) vis[now] = 1;
else {
r = i;
break;
}
}
for(int i = x; i >= 1; i--) {
int now = sum[i] - sum[i - 1];
if(now == 1 && i != x) break;
len = max(len, now);
r = min(r, next[i]);
if(i + len - 1 < r && i + len - 1 <= n)
if(sum[i + len - 1] - sum[i - 1] == (LL)len * (len + 1) >> 1)
ans = max(ans, len);
}
for(int i = x + 1; i <= n && sum[i] - sum[i - 1] != 1; i++)
vis[sum[i] - sum[i - 1]] = 0;
}
inline void solve() {
for(int i = 1; i <= n; i++) last[i] = inf;
for(int i = n; i >= 1; i--) {
int now = sum[i] - sum[i - 1];
next[i] = last[now];
last[now] = i;
}
for(int i = 1; i <= n; i++) if(sum[i] - sum[i - 1] == 1)
calc(i);
}
int main() {
n = iread();
for(int i = 1; i <= n; i++) sum[i] = sum[i - 1] + iread();
solve();
for(int i = n; i >= 1; i--) sum[i] = sum[i] - sum[i - 1];
reverse(sum + 1, sum + 1 + n);
for(int i = 1; i <= n; i++) sum[i] += sum[i - 1];
solve();
printf("%d\n", ans);
return 0;
}