1006C 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 cc elements, 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],[ ].

题意:输入n个数,分成三部分,前中后三部分,中间可以为0,让前后两部分相等,而且尽量更大。

题解:从两边往中间加,如果相等就记录当前最大值,可以用前缀和,也可以暴力向中间找。

c++:

#include<bits/stdc++.h>
using namespace std;
long long n,a[200010],maxn,s1,s2;
int main()
{
    cin>>n;
    int l=0,r=n-1;
    for(int i=0; i<n; i++)
        cin>>a[i];
    s1+=a[l],s2+=a[r];
    while(l<r)
    {
        if(s1<s2) s1+=a[++l];
        else if (s1>s2) s2+=a[--r];
        else if (s1==s2) maxn=s1,s1+=a[++l],s2+=a[--r];

    }
    if(n==1) puts("0");
    else cout<<maxn<<endl;
    return 0;
}

前缀和:

#include<bits/stdc++.h>
using namespace std;
 long long a[200010],n,maxn;
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
     cin>>a[i],a[i]+=a[i-1];
    for(int i=1,j=n;i<j;)
        if(a[i]==a[n]-a[j-1]) maxn=a[i],i++,j--;
        else if(a[i]>a[n]-a[j-1]) j--;
        else i++;
    if(n==1)puts("0");
    else cout<<maxn<<endl;
    return 0;
}

python:

n=int(input())
a=list(map(int,input().split()))
l=maxn=0;r=len(a)-1
s1=a[l];s2=a[r]
while l<r:
    if s1<s2:
        l+=1;s1+=a[l]
    elif s1>s2:
        r-=1;s2+=a[r]
    else:
        maxn=s1;l+=1;s1+=a[l];r-=1;s2+=a[r]
if(n==1):print(0)
else:print(maxn)

猜你喜欢

转载自blog.csdn.net/memory_qianxiao/article/details/81092517