Codeforces 1138 A 思维水题

http://codeforces.com/problemset/problem/1138/A

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.

Examples

Input

7
2 2 2 1 1 2 2

Output

4

Input

6
1 2 1 2 1 2

Output

2

Input

9
2 2 1 1 1 2 2 2 2

Output

6

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].

题目大意:给一段只包含1和2的序列,求一段连续的子序列,在该子序列中1、2的数目一致且满足所有的1相连,所有的2相连。(即1在左半部分 2在右半部分 或1在右半部分 2在左半部分)

思路:遍历序列,cnt1记录1的数量,cnt2记录2的数量,flag记录分界点的次数,即若a[i]!=a[i-1],则++flag,若flag=2说明这一段子序列已经确定,且值为min(cnt1,cnt2)*2,用变量MAX来维护;此时若a[i-1]=1,则置cnt2=0,(cnt=1不变),反之置cnt1=0,同时置flag=1,继续遍历。

#include
#include
#include
typedef long long ll;
using namespace std;

int cnt1,cnt2;
int a[100005];
int n;

int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	int flag=0;
	int MAX=0;
	for(int i=1;i<=n;i++)
	{
		if(i!=1&&a[i]!=a[i-1])
			++flag;
		if(flag==2)
		{
			MAX=max(MAX,min(cnt1,cnt2)*2);
			if(a[i-1]==1)
				cnt2=0;
			else
				cnt1=0;
			flag=1;
		}
		if(a[i]==1)
			++cnt1;
		else
			++cnt2;
	}
	MAX=max(MAX,min(cnt1,cnt2)*2);
	printf("%d\n",MAX);
	return 0;
}

 

你可能感兴趣的:(水题,思维)