codeforces1006C——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],[ ].

 

 

Codeforces (c) Copyright 2010-2018 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Jul/21/2018 22:08:23UTC+8 (d2).

Desktop version, switch to mobile version.

Privacy Policy

题目大意:给你一个数组,让你把这组数据分成三份并保证第一组数据和第三组数据的总和相等,并且第一组数据的总和最大。

题目思路:这道题最开始的思路是,直接是第二组数据的元素个数最少,且保证其总和最小,后来仔细一想不对。再然后的思路是,把这组数据从头和尾分别开始查找,并记录他们的总和,当两边的和相等时,在用一个变量记录一次。

#include<bits/stdc++.h>
using namespace std;
const int MAXN=200005;
int a[MAXN];
int n;
int main(){
	while(~scanf("%d",&n)){
		for(int i=0;i<n;i++){
			scanf("%d",&a[i]);
		}
		long long sum1=a[0],sum3=a[n-1];
		int left=0,right=n-1;
		long long ans=0;
		while(left<right){
			if(sum1==sum3){
				ans=sum1;
				left++;
				right--;//注意当两组数据相等时还要继续向前搜索,应为要保证sum1最大
				sum1=sum1+a[left];
				sum3=sum3+a[right];
			}
			else{
				if(sum1<sum3){
					sum1=sum1+a[left+1];
					left++;
				}
				if(sum1>sum3){
					sum3=sum3+a[right-1];
					right--;
				}
			}
		}
		printf("%lld\n",ans);
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值