Codeforces Round #547 (Div. 3)B. Maximal Continuous Rest

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Each day in Berland consists of nn hours. Polycarp likes time management. That's why he has a fixed schedule for each day — it is a sequence a1,a2,…,ana1,a2,…,an (each aiai is either 00 or 11), where ai=0ai=0 if Polycarp works during the ii-th hour of the day and ai=1ai=1 if Polycarp rests during the ii-th hour of the day.

Days go one after another endlessly and Polycarp uses the same schedule for each day.

What is the maximal number of continuous hours during which Polycarp rests? It is guaranteed that there is at least one working hour in a day.

Input

The first line contains nn (1≤n≤2⋅1051≤n≤2⋅105) — number of hours per day.

The second line contains nn integer numbers a1,a2,…,ana1,a2,…,an (0≤ai≤10≤ai≤1), where ai=0ai=0 if the ii-th hour in a day is working and ai=1ai=1 if the ii-th hour is resting. It is guaranteed that ai=0ai=0 for at least one ii.

Output

Print the maximal number of continuous hours during which Polycarp rests. Remember that you should consider that days go one after another endlessly and Polycarp uses the same schedule for each day.

Examples

input

Copy

5
1 0 1 0 1

output

Copy

2

input

Copy

6
0 1 0 1 1 0

output

Copy

2

input

Copy

7
1 0 1 1 1 0 1

output

Copy

3

input

Copy

3
0 0 0

output

Copy

0

Note

In the first example, the maximal rest starts in last hour and goes to the first hour of the next day.

In the second example, Polycarp has maximal rest from the 44-th to the 55-th hour.

In the third example, Polycarp has maximal rest from the 33-rd to the 55-th hour.

In the fourth example, Polycarp has no rest at all.

这个相当于一个循环数组,找到相连续最多的休息天数,遇到这种问题可以将数组扩大二倍来解决

#include<bits/stdc++.h>

using namespace std;
int a[500000];

int main()
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		for (int i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
			a[i+n] = a[i];
		}
		int cnt = 0,maxm = 0;
		for (int i=0;i<2*n-1;i++)
		{
			if (a[i]==1)
			{
				cnt++;
				maxm = max(cnt,maxm);
			}
			else
			{
				cnt = 0;
			}
		}
		printf("%d\n",maxm);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40912854/article/details/89367127