**B - Maximal Continuous Rest **

Each day in Berland consists of n hours. Polycarp likes time management. That's why he has a fixed schedule for each day — it is a sequence a1,a2,…,an (each ai is either 0 or 1), where ai=0 if Polycarp works during the i-th hour of the day and ai=1 if Polycarp rests during the i-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 n (1≤n≤2⋅105) — number of hours per day.

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

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

正确代码

#include<bits/stdc++.h>
using namespace std;
const int MM = 2e5+5;
int a[MM];
int main()
{
int n, res = 0, maxx = 0;
cin >> n;
for(int i = 1; i <= n; i++)
{
cin >> a[i];
if(a[i] == 1)
{
res++;
if(i == n && maxx < res)
{
//if(maxx < res)
//{
maxx = res;
res = 0;
// }
}
}
else
{
if(maxx < res)
{
maxx = res;
}
res = 0;
}
}
int res1 = 0, res2 = 0;
for(int i = 1; i <= n; i++)
{
if(a[i] == 1) res1++;
else break;
}
for(int i = n; i >= 1; i--)
{
if(a[i] == 1) res2++;
else break;
}
int tt = 0;
if(res1 != n)
tt = res1+res2;
//cout << maxx << endl;
printf("%d\n", tt > maxx? tt:maxx);
return 0;
}

代码理解

该题不难,主要目的是判断1的连续个数,主要难点是如何判断开头和结尾1连续的个数,如果想到在两边同时进行判断则可以很迅速的编写完成代码,首先用if语句判断1的连续性,即用数组a进行储存1和0,若a[i]1则进行下一个循环,若a[i]!=1即a[i]0则跳出循环,与此同时进行两边同时判断1的连续性函数,即从开始a[0]判断1连续的个数,若a[i]==0则结束循环进行从后向前判断a[n-1]是否等于零,判断出前后连续1的个数则进行相加即代码中的res1+res2,最后用最大值max和res1+res2比较大小,最后输出最大的那一项。

#C++初学记录(算法考试1)的更多相关文章

  1. #C++初学记录(算法4)

    A - Serval and Bus It is raining heavily. But this is the first day for Serval, who just became 3 ye ...

  2. #C++初学记录(贪心算法#结构体#贪心算法)

    贪心算法#结构体 Problem Description "今年暑假不AC?" "是的." "那你干什么呢?" "看世界杯呀,笨蛋 ...

  3. #C++初学记录(算法效率与度量)

    时间性能 算法复杂性函数: \[ f(n)=n^2 +1000n+\log_{10}n+1000 \] 当n的数据规模逐渐增大时,f(n)的增长趋势: 当n增大到一定值以后,计算公式中影响最大的就是n ...

  4. #C++初学记录(贪心算法#二分查找)

    D - Aggressive cows 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x1,...,xN (0 < ...

  5. #C++初学记录(算法3)

    C - 不要62 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer). 杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司 ...

  6. #C++初学记录(算法2)

    A - Game 23 Polycarp plays "Game 23". Initially he has a number n and his goal is to trans ...

  7. #C++初学记录(算法测试2019/5/5)(深度搜索)

    深度搜索:Oil Deposits GeoSurvComp地质调查公司负责探测地下石油储藏. GeoSurvComp现在在一块矩形区域探测石油,并把这个大区域分成了很多小块.他们通过专业设备,来分析每 ...

  8. #C++初学记录(sort函数)

    sort函数 前言:当进行贪心算法的学习时,需要用到sort函数,因为初学c++汇编语言,sort的具体用法没有深入学习,所以这里进行sort学习记录并只有基础用法并借用贪心算法题目的代码. 百度百科 ...

  9. #C++初学记录(动态规划(dynamic programming)例题1 钞票)

    浅入动态规划 dynamic programming is a method for solving a complex problem by breaking it down into a coll ...

随机推荐

  1. sencha touch 常见问题解答(1-25)

    欢迎留言补充,持续更新中... 1.sencha touch 是什么? 答:Sencha touch框架是世界上第一个基于HTML 5的移动应用框架.它可以让你的Web应用看起来像网络应用.美丽的用户 ...

  2. remote: fatal: could not read Username for 'http://spapa.wicp.net:3000': No such device ors

    解决办法: git remote add origin https://{username}:{password}@github.com/{username}/project.git in my ca ...

  3. CentOS 安装Sqlite3

    wget http://www.sqlite.org/sqlite-autoconf-3070500.tar.gz tar xvzf sqlite-autoconf-3070500.tar.gz cd ...

  4. dhroid - eventbus 事件总线

    你听过onClick 事件,onItemClick 事件,事件总线不一定听过吧, eventbus 事件总线也是一个编程思想,为什么要设计EventBus了,因为他是领域驱动设计中比不可少的模块,它承 ...

  5. react+babel+webpack初试

    在上一篇,我们简单学习了webpack学习,现在这里我们简单学习一下react+babel+webpack,进行编译react语法jsx以及结合es6写法. 这里我就简单的直接上demo: packa ...

  6. ztree学习---将默认勾选的展开

    这里只给出前台页面上的代码,数据可以从后台获取,注意,在封装数据的时候,注意:Id(节点的id,可以是数字也可以是字符串) , pid(父亲节点的id) , name(节点名称)不能少 . html页 ...

  7. linux 系统信息查看

    查看系统版本:lsb_release -a 查看内核版本:uname -a 查看cpu型号:cat /proc/cpuinfo 查看硬盘空间情况df -lm 查看内存:free -m  VGA显卡:l ...

  8. iOS 截屏分享(包含状态栏与不包含状态栏)

    iOS8以上的新方法PhotoKit 监听截图相册变化,取最后一张图片:http://www.hangge.com/blog/cache/detail_1515.html PhotoKit 获取本机相 ...

  9. 字符串-回文-Manacher算法

    http://blog.csdn.net/zzkksunboy/article/details/72600679 https://segmentfault.com/a/1190000008484167 ...

  10. Linux下pip使用国内源

    pip国内的一些镜像   阿里云 http://mirrors.aliyun.com/pypi/simple/   中国科技大学 https://pypi.mirrors.ustc.edu.cn/si ...