CodeForces 1141B Maximal Continuous Rest题解

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

5
1 0 1 0 1

Output

2

Input

6
0 1 0 1 1 0

Output

2

Input

7
1 0 1 1 1 0 1

Output

3

Input

3
0 0 0

Output

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.

 

题意:第一行输入一天几小时,第二行对应每个小时的状态,1代表休息,0代表工作,求最大休息时间,且第二天和第一天是连起来的,也就是说第一天最后休息的和第二天刚开始休息的时间可以加在一起

思路:直接遍历就好了,设置一个ans记录连续休息了多少小时,遇到1,ans++,遇到0,ans清0,注意一下第一天结尾和第二天开始的情况就好了

代码:

#include <bits/stdc++.h>
using namespace std;
#define M 200010
int n,a[M],last;
int maxn;
int main()
{
	ios::sync_with_stdio(false);
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	int ans = 0;
	maxn = 0;
	int p =0; 
	for (int i = 1; i <= n; i++) {
		if (a[i]) {
			ans++;
			if (i == n) {
				if (a[1] == 1) {
					ans+=last;	
				}
				maxn = max(ans, maxn);
			}
		}else{
			maxn = max(ans, maxn);
			if(p==0){
				last = ans;
			}
			p = 1;
			ans = 0;
		}
	}
	cout << maxn << endl;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值