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=sum3and 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.

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

很简单的一道题,开始的时候想多了,两边同时向里面

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
const int MAX = 2e5 + ;
long long n, a[MAX], x;
int main() { long long sum1 = ,sum2 = ,sum0 = ;
memset(a,,sizeof(a));
scanf("%d", &n );
for( int i = ; i <= n; i++ ) {
scanf("%d",&a[i]);
}
int left = , right = n+;
while(left<right){ if(sum1>sum2){
right--;
sum2 += a[right];
if(left==right)break;
}
if(sum2>sum1){
left++;
sum1 += a[left];
if(left==right)break;
}
if(sum1==sum2){
sum0 = sum1;
left++;
right--;
if(left==right)break;
sum1+=a[left];
sum2+=a[right];
} }
cout<<sum0<<endl; return ;
}

[codeForce-1006C]-Three Parts of the Array (简单题)的更多相关文章

  1. CF 1006C Three Parts of the Array【双指针/前缀和/后缀和/二分】

    You are given an array d1,d2,-,dn consisting of n integer numbers. Your task is to split this array ...

  2. 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 s ...

  3. leetcode简单题6

    今天的华师 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, fro ...

  4. BZOJ 2683: 简单题

    2683: 简单题 Time Limit: 50 Sec  Memory Limit: 128 MBSubmit: 913  Solved: 379[Submit][Status][Discuss] ...

  5. 【BZOJ-1176&2683】Mokia&简单题 CDQ分治

    1176: [Balkan2007]Mokia Time Limit: 30 Sec  Memory Limit: 162 MBSubmit: 1854  Solved: 821[Submit][St ...

  6. Bzoj4066 简单题

    Time Limit: 50 Sec  Memory Limit: 20 MBSubmit: 2185  Solved: 581 Description 你有一个N*N的棋盘,每个格子内有一个整数,初 ...

  7. Bzoj2683 简单题

    Time Limit: 50 Sec  Memory Limit: 128 MBSubmit: 1071  Solved: 428 Description 你有一个N*N的棋盘,每个格子内有一个整数, ...

  8. 这样leetcode简单题都更完了

    这样leetcode简单题都更完了,作为水题王的我开始要更新leetcode中等题和难题了,有些挖了很久的坑也将在在这个阶段一一揭晓,接下来的算法性更强,我就要开始分专题更新题目,而不是再以我的A题顺 ...

  9. [BZOJ2683][BZOJ4066]简单题

    [BZOJ2683][BZOJ4066]简单题 试题描述 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: 命令 参数限制 内容 1 x y A 1<=x ...

随机推荐

  1. 【译文】InnoDB 的不同的SQL如何加锁

    http://dev.mysql.com/doc/refman/5.6/en/innodb-locks-set.html 前置:检索如果用不到索引,会扫描全表,并根据策略加锁.所以,这就是我们合理建立 ...

  2. 4-3 R语言函数 mapply

    #mapply(函数/函数名,数据,函数相关的函数) > list(rep(1,4),rep(2,3),rep(3,2),rep(4,1)) [[1]] [1] 1 1 1 1 [[2]] [1 ...

  3. ElasticSearch学习之——基本的文档CURD

    一.文档的添加 POST http://127.0.0.1:9200/{index}/{type}/{id} { "key":"value", "ke ...

  4. ethereumjs/ethereumjs-common-1-简介

    为了了解ethereumjs/ethereumjs-block-3-代码的使用需要了解的一个模块 https://github.com/ethereumjs/ethereumjs-common Com ...

  5. Node.js 连接 MongoDB-7

    先安装模块: npm install --save mongodb 当然,首先你要打开mongodb服务端: mongod --bind_ip 127.0.0.1 创建数据库 要在 MongoDB 中 ...

  6. (转)添加Template(模板)并基于模板部署应用

    通过Template,可以定义一个或多个需要部署的镜像,定义依赖的对象,定义可供用户输入的配置参数项. 以cakephp-mysql.json为例. # oc create -f https://ra ...

  7. SQL语句查询关键字中含有特殊符号怎么处理, 例如 'SMI_'

    SQL语句查询关键字中含有特殊符号怎么处理, 例如 'SMI_' 错误:select * from emp  where ename like '%SML_%' 正确:select * from em ...

  8. OpenGL 球体世界

    一.类似公自转 二.核心代码 //图形渲染 void RenderScene() { //清楚缓存区:颜色缓存区.深度缓存区.模版缓存区 glClear(GL_COLOR_BUFFER_BIT|GL_ ...

  9. Linux文本编辑器-vi/vim

    vi是Linux命令行界面下的文字编辑器,vim是vi的增强版(Vi IMproved),完全兼容 可以理解成普通的txt文本与word文档之间的差距. 注:还有一款全屏编辑器是nano,可以了解下 ...

  10. windows下搭建permeate漏洞测试系统实战

    最近一直在搭建漏洞测试环境练习. 在此期间遇到很多问题,但是通过学习都一一解决.通过写此文来记录遇到的问题和解决方法. 首先,在github上看到了一个不错的permeate渗透测试系统.于是想搭建拿 ...