Codeforces Round #498 (Div. 3) C. Three Parts of the Array

C. Three Parts of the Array

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given an array d1,d2,…,dnd1,d2,…,dn consisting of nn integer numbers.

Your task is to split this array into three parts (some of which may be empty) in such a way that each element of the array belongs to exactly one of the three parts, and each of the parts forms a consecutive contiguous subsegment (possibly, empty) of the original array.

Let the sum of elements of the first part be sum1sum1, the sum of elements of the second part be sum2sum2 and the sum of elements of the third part be sum3sum3. Among all possible ways to split the array you have to choose a way such that sum1=sum3sum1=sum3 and sum1sum1 is maximum possible.

More formally, if the first part of the array contains aa elements, the second part of the array contains bb elements and the third part contains ccelements, then:

sum1=∑1≤i≤adi,sum1=∑1≤i≤adi,sum2=∑a+1≤i≤a+bdi,sum2=∑a+1≤i≤a+bdi,sum3=∑a+b+1≤i≤a+b+cdi.sum3=∑a+b+1≤i≤a+b+cdi.

The sum of an empty array is 00.

Your task is to find a way to split the array such that sum1=sum3sum1=sum3 and sum1sum1 is maximum possible.

Input

The first line of the input contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of elements in the array dd.

The second line of the input contains nn integers d1,d2,…,dnd1,d2,…,dn (1≤di≤1091≤di≤109) — the elements of the array dd.

Output

Print a single integer — the maximum possible value of sum1sum1, considering that the condition sum1=sum3sum1=sum3 must be met.

Obviously, at least one valid way to split the array exists (use a=c=0a=c=0 and b=nb=n).

Examples

input

Copy

5
1 3 1 1 4

output

Copy

5

input

Copy

5
1 3 2 1 4

output

Copy

4

input

Copy

3
4 1 2

output

Copy

0

Note

In the first example there is only one possible splitting which maximizes sum1sum1: [1,3,1],[ ],[1,4][1,3,1],[ ],[1,4].

In the second example the only way to have sum1=4sum1=4 is: [1,3],[2,1],[4][1,3],[2,1],[4].

In the third example there is only one way to split the array: [ ],[4,1,2],[ ][ ],[4,1,2],[ ].

 

将数组分为三段

若头段和 = 尾段和 更新答案为头端和(尾端和)

双指针往中间指

头大加尾

尾大加头

注:

虽然所有数据都在1e9之内

但是连续加和是会爆int的

错了一发 

#include <iostream>
using namespace std;

const int MAXN = 2e5 + 10;

long long arr[MAXN] = {0};

int main()
{	
	int N;
	
	cin>>N;
	
	for(int i = 0; i < N; i++)
	{
		cin>>arr[i];
	}
	
	long long a = arr[0], b = 0, ans = 0;
	
	int i = 1, j = N - 1;
	
	while(i <= j)
	{
		if(a > b)
		{
			b += arr[j];
			j--;
		}
		else if(a < b)
		{
			a +=  arr[i];
			i++; 
		}
		if(a == b)
		{
			ans = max(ans, a);
			a += arr[i];
			i++; 
		}
			
	}
	
	cout<<ans<<endl;
	return 0;
}

 

posted @ 2018-07-17 10:55  张浦  阅读(117)  评论(0编辑  收藏  举报