1141B Maximal Continuous Rest

小咪咪 2022-03-02 09:08 160阅读 0赞

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

  1. 5
  2. 1 0 1 0 1

output

Copy

  1. 2

input

Copy

  1. 6
  2. 0 1 0 1 1 0

output

Copy

  1. 2

input

Copy

  1. 7
  2. 1 0 1 1 1 0 1

output

Copy

  1. 3

input

Copy

  1. 3
  2. 0 0 0

output

Copy

  1. 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的最大长度,也就是休息时间。

题解:直接模拟 不过首尾为1那种要注意,最好是把原始串在复制一次连接一起,这样就好处理了,遍历所有,有1就标记加加,然后取最大值,没有标记清0,继续这样,直到遍历完。python的话, 就更方便了,直接拼接,分割0,求最大长度就行。

c++:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5. int n,cnt=0,maxn=0,c[400010];
  6. cin>>n;
  7. for(int i=0;i<n;i++)
  8. cin>>c[i],c[i+n]=c[i];//拼接
  9. for(int i=0;i<2*n;i++)
  10. {
  11. if(c[i]==1) cnt++,maxn=max(maxn,cnt);
  12. else cnt=0;
  13. }
  14. cout<<maxn<<endl;
  15. return 0;
  16. }

python:

  1. n=int(input())
  2. s=(''.join(input().split()))*2
  3. s=s.split('0')
  4. s=[len(i) for i in s]
  5. print(max(s))

发表评论

表情:
评论列表 (有 0 条评论,160人围观)

还没有评论,来说两句吧...

相关阅读

    相关 HDU 1141(数学题)

    题意:1960年的计算机是4位处理器,1970年是8位,没过10年翻一倍,求当前年份的计算机处理器能存储的阶乘n!中n的大小。   思路:假设处理器的位数为x位,则n! <

    相关 1141: 进制转换

    Description 将十进制整数n转换成二进制,并保存在字符数组中,最后输出。要求定义并调用convert()函数, 将十进制整数n对应的二进制数存入字符数组str中