You are given an array d1,d2,…,dn consisting of n 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 sum1, the sum of elements of the second part be sum2 and the sum of elements of the third part be sum3. Among all possible ways to split the array you have to choose a way such that sum1=sum3 and sum1 is maximum possible.

More formally, if the first part of the array contains a elements, the second part of the array contains b elements and the third part contains c elements, then:

sum1=∑1≤i≤adi,

sum2=∑a+1≤i≤a+bdi,

sum3=∑a+b+1≤i≤a+b+cdi.

The sum of an empty array is 0.

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

Input

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

The second line of the input contains n integers d1,d2,…,dn (1≤di≤109) — the elements of the array d.

Output

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

Obviously, at least one valid way to split the array exists (use a=c=0 and 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].

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

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

【题意】:将一个长度为n的数组划分为a,b,c三段(每一段都可以为空)使得a段和c段的和相等,问a段的和的最大值是多少?

【分析】:双指针求前缀和与后缀和进行大小比较,一遇到相等时打擂台求最大。注意要求和所以用LL,不然会WA10!

【代码】:

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
#define ll long long
ll n,m;
ll a[N];
/*
将一个长度为n的数组划分为a,b,c三段(每一段都可以为空)
使得a段和c段的和相等,问a段的和的最大值是多少?
*/
int main()
{
while(~scanf("%lld",&n))
{
ll Max = 0, ls, rs;
for(ll i=0; i<n; i++)
{
scanf("%lld",&a[i]);
}
ll L = 0, R = n-1;
ls = a[L];
rs = a[R];
while(L < R)
{
if(ls == rs)
{
Max = max(Max,ls);
L++;
R--;
ls += a[L];
rs += a[R];
}
else if(ls > rs)
{
R--;
rs += a[R];
}
else
{
L++;
ls += a[L];
}
}
cout<<Max<<endl;
}
}

【二分】:

#include<bits/stdc++.h>

using namespace std;
#define ll long long
const int maxn = 2*1e5+10;
ll n,a[maxn];
ll pre[maxn],suf[maxn];
/*
求前缀和,后缀和。然后从大到小枚举后缀,在前缀中查找相等,如果能找到且a,c两段不重叠,那么就是答案。注意开long long
*/
int main()
{
while(~scanf("%lld",&n))
{
ll Max = 0;
memset(pre,0,sizeof(pre));
memset(suf,0,sizeof(suf));
for(int i=0; i<n;i++)
{
scanf("%lld",&a[i]);
}
pre[0] = a[0];
for(int i=1;i<n;i++)
pre[i] = pre[i-1] + a[i]; suf[n-1] = a[n-1];
for(int i=n-2; i>=0; i--)
suf[i] = suf[i+1] + a[i];
for(int i=0; i<n; i++)
{
int pos = lower_bound(pre,pre+n,suf[i])-pre;
if(pos < i && pre[pos] == suf[i])
{
Max = max(Max,suf[i]); }
}
printf("%lld\n",Max);
} }
/*
n-1:4
0 1 2 3 4
1 2 3 4 5
5 9 12
*/

最新文章

  1. Python 学习之路 (前言)
  2. 【局部特征】ASIFT
  3. linux下用命令导出mysql表数据
  4. VMware的使用
  5. 洛谷P1736 创意吃鱼法
  6. js基础之arguments、css
  7. NEERC 2013, Eastern subregional contest
  8. Java 使用反射拷贝对象一般字段值
  9. xcode UIView常用方法属性动画
  10. handlebar.js使用
  11. CImage类
  12. .Net Core 中使用AutoMapper
  13. 随聊——Python的前世今生
  14. Node Express 初探
  15. bzoj 3669: [Noi2014]魔法森林 (LCT)
  16. spring3:多数据源配置使用
  17. mysql/oracle jdbc大数据量插入优化
  18. MySQL查询语句执行过程及性能优化-基本概念和EXPLAIN语句简介
  19. postgresql----数组类型和函数
  20. python3.4学习笔记(九) Python GUI桌面应用开发工具选择

热门文章

  1. Hibernate常用方法之_修改
  2. [CF1066C]Books Queries
  3. [NOI2016 D2T1]区间
  4. 机器学习模型-支持向量机(SVM)
  5. 菜单 &amp; 工具栏 &amp; 状态栏
  6. BZOJ1558 [JSOI2009]等差数列 【线段树】
  7. BZOJ3573 [Hnoi2014]米特运输 【贪心】
  8. 【BZOJ2134】单选错位 概率DP
  9. JAVA List 一边遍历一边删除元素
  10. [bzoj 2844]线性基+高斯消元