Arkady invited Anna for a dinner to a sushi restaurant. The restaurant is a bit unusual: it offers nn pieces of sushi aligned in a row, and a customer has to choose a continuous subsegment of these sushi to buy.

The pieces of sushi are of two types: either with tuna or with eel. Let's denote the type of the ii-th from the left sushi as titi, where ti=1ti=1 means it is with tuna, and ti=2ti=2 means it is with eel.

Arkady does not like tuna, Anna does not like eel. Arkady wants to choose such a continuous subsegment of sushi that it has equal number of sushi of each type and each half of the subsegment has only sushi of one type. For example, subsegment [2,2,2,1,1,1][2,2,2,1,1,1] is valid, but subsegment [1,2,1,2,1,2][1,2,1,2,1,2] is not, because both halves contain both types of sushi.

Find the length of the longest continuous subsegment of sushi Arkady can buy.

Input

The first line contains a single integer nn (2≤n≤1000002≤n≤100000) — the number of pieces of sushi.

The second line contains nn integers t1t1, t2t2, ..., tntn (ti=1ti=1, denoting a sushi with tuna or ti=2ti=2, denoting a sushi with eel), representing the types of sushi from left to right.

It is guaranteed that there is at least one piece of sushi of each type. Note that it means that there is at least one valid continuous segment.

Output

Print a single integer — the maximum length of a valid continuous segment.

Note

In the first example Arkady can choose the subsegment [2,2,1,1][2,2,1,1] or the subsegment [1,1,2,2][1,1,2,2] with length 44.

In the second example there is no way but to choose one of the subsegments [2,1][2,1] or [1,2][1,2] with length 22.

In the third example Arkady's best choice is the subsegment [1,1,1,2,2,2][1,1,1,2,2,2].

题解

 #include<iostream>
#include<math.h>
using namespace std;
int main() {
int n,ans,max=;
cin>>n;
int a[n],b[n-];
for(int i=; i<n; i++) {
cin>>a[i];
}
for(int i=; i<n-; i++) {
b[i]=abs(a[i+]-a[i]);
}
for(int i=; i<n-; i++) {
if(b[i]==) {
ans=;
for(int j=; j<=n/; j++) {
if((i-j)>=&&(i+j)<n-&&b[i-j]==&&b[i+j]==) {
ans+=;
if(ans>=max) {
max=ans;
}
} else {
break;
}
}
}
}
cout<<max+;
}

思路分析:题目是1和2两个数字代表两种寿司。要求选区连续的对称的如12或21或1122或2211或111222的字段并输出长度。

因为由1和2组成,它们的差就是0或1,如果是1122,它们的差就是010,遍历字符串找到所有为1的字符,再找它左边一个右边一个判断是否相等,再左边两个右边两个直到不相等,然后存取最大长度到max变量里。

最新文章

  1. 在Qt Creator 和在 vs2012 里添加信号和槽
  2. Android标题栏最右边添加按钮
  3. 1018LINUX中crontab的用法
  4. [Linux]centOS7-1-1503-x86_64下安装VM-TOOLS
  5. Bata版本冲刺计划及安排
  6. ExtJs之Ext.apply
  7. STL中heap算法(堆算法)
  8. RedHat7笔记
  9. Xamarin.Forms本地化多语言
  10. juicer模板引擎使用
  11. wpf的datagrid和winform的datagridview刷新
  12. 一篇完整的FlexBox布局指南
  13. 【Spark2.0源码学习】-4.Master启动
  14. Mybatis中如何查询时间段内的数据
  15. JavaSE编程题
  16. Sudoku(POJ2676/3074)
  17. 20165305 苏振龙《Java程序设计》第八周课上测试补做
  18. springmvc入门(1)
  19. 质量能量等效的泛化--物理学定律方程与等效原理的对应关系 Generalization of Mass-Energy Equivalence--Corresponding Relations between Equations of Physical Laws and Equiva
  20. docker 安装MySQL远程连接

热门文章

  1. php 常量的使用
  2. PHPSTORM快捷键On Mac
  3. [Qt] 通过socket将另一个程序的某个窗口调到最前端
  4. LeetCode7-ReverseInteger
  5. Spring5参考指南:SpringAOP简介
  6. Spring Boot Starters介绍
  7. Scala教程之:面向对象的scala
  8. xml文件错误
  9. 日日算法:Kruskal算法
  10. 关于SpringBoot集成myBatis时,mapper接口注入失败的问题