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 cc elements, then:
The sum of an empty array is 0.
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
n(1≤n≤2⋅105)
n
(
1
≤
n
≤
2
⋅
10
5
)
— the number of elements in the array
d
d
.
The second line of the input contains nn integers — the elements of the array d d .
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 and b=n b = n ).
Examples
Input
5
1 3 1 1 4
Output
5
Input
5
1 3 2 1 4
Output
4
Input
3
4 1 2
Output
0
Note
In the first example there is only one possible splitting which maximizes
sum1:[1,3,1],[ ],[1,4]
s
u
m
1
:
[
1
,
3
,
1
]
,
[
]
,
[
1
,
4
]
In the second example the only way to have sum1 s u m 1 =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 ] , [ ] .
左右逼近法,那边小就往那边加数
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<iostream>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
const int MAXN=2*1e5+9;
int a[MAXN];
int main()
{
int n;
while(~scanf("%d",&n))
{
memset(a,0,sizeof(a));
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
ll ans=0;
ll front=a[0];
ll rear=0;
ll k=1;
ll j=n-1;
while(k<=j)
{
if(front<rear)
{
front+=a[k];
k++;
}
else if(front>rear)
{
rear +=a[j];
j--;
}
if(front==rear)
{
ans=max(ans,front);
front+=a[k];
k++;
}
}
printf("%lld\n",ans);
}
return 0;
}