C. Three Parts of the Array

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

题意:将一个字符串划分为三段,使得子字符串的和分别为sum1、sum2、sum3,使得sum1 == sum3,且sum1尽可能的大,输出sum1.

题解:这道题刚开始自己思绪很混乱,不知道该如何下手,后来才知道。从两边向中间推,找到最大的sum1.(感觉代码很考验技巧性。需要学习这种技巧)

#include<cstdio>
#include<iostream>
#include<algorithm>using namespace std;const int maxn = 2*1e5+5;
long long d[maxn];
int main()
{int n;scanf("%d", &n);for(int i = 0; i < n; i++) scanf("%lld", &d[i]);long long sum1 = 0, sum2 = 0, mx = 0;int i = 0, j = n-1;//从两边往中间推 while(i <= j){if(sum1 < sum2){      //当sum1较小时,i向前推,同时sum1加值 sum1 += d[i];i++;}if(sum1 > sum2){sum2 += d[j];    //当sum2较小时,j向后推,同时sum2加值 j--;}if(sum1 == sum2){if(sum1 > mx){   //当两边相等时,取最大的sum值 mx = sum1;}sum1 += d[i];i++;}                //一次类推,一直到跳出循环 }printf("%lld\n", mx);return 0;
}

C. Three Parts of the Array(切割字符串)相关推荐

  1. 小程序——切割字符串

    小程序--切割字符串 做完项目整理了下小程序的字符串切割的常见几种方法 JS对字符串进行切割截取 1.函数:split() 功能:使用一个指定的分隔符把一个字符串分割存储到数组 例子: str=&qu ...

  2. Java 切割字符串的几种方式集合(亲测)

    这篇文章主要介绍了Java 切割字符串的几种方式集合,具有很好的参考价值,希望对大家有所帮助.如有错误或未考虑完全的地方,望不吝赐教 Java 切割字符串的几种方式 1.StringTokenizer ...

  3. C#中切割字符串之正则表达式应用

    这个是我做的使用正则表达式来切割字符串的一个案例部分代码: 功能简介:一条物流信息字符串中截取出多种数据详情 需要切割出的数据包括: 起始地,目的地,货物数量,货物单位,货物类型,车数量,车长度,车类 ...

  4. 【Groovy】Groovy 方法调用 ( 字符串切割 | 使用 Java 语法切割字符串 | 使用 Groovy 语法切割字符串直接为变量赋值 | 数组赋值给变量 变量个数小于等于数组长度 )

    文章目录 一.字符串切割 1.使用 Java 语法切割字符串 2.使用 Groovy 语法切割字符串直接为变量赋值 3.数组赋值给变量 变量个数小于等于数组长度 二.完整代码示例 一.字符串切割 在 ...

  5. 按字符串长度切割字符串(支持汉字占2个长度)

    /// <summary> /// 按指定长度切割字符串(汉字算2个字符长度) /// </summary> /// <param name="input&qu ...

  6. python 切割字符串

    切割字符串,用split放法,分割符号为 ; , . import re line="3w.ναdΜāιι.com Provide you with a professional,platf ...

  7. php截取数组中的字符串,PHP 中使用explode()函数切割字符串为数组的示例

    explode()函数的作用:使用一个字符串分割另一个字符串,打散为数组. 例如: 字符串 PHP;"> $pizza = "第1 第2 第3 第4 第5 第6"; ...

  8. php 提取字段为key,从一个serialize过的array的字符串中取出中取对应KEY的value

    有时会在数据表中保存一个serizlie()过的一个php的array变量字符串,当需要使用SQL语言取出某个key的值时可以用到.比如: a:3:{s:1:a;s:12:asdfasdfasdf;s ...

  9. mysql 切割字符串的妙用

    为什么80%的码农都做不了架构师?>>>    mysql 切割字符串的妙用 在优化以前写的一些mysql语句的时候,发现有些时候小伙伴竟然对于一个需求是用java代码统计的.速度虽 ...

最新文章

  1. 探索Julia(part14)--学生得分描述性统计案例
  2. RHEL7 -- NetworkManager
  3. 记录一次被DDOS攻击,攻击类型:UDPFLOOD
  4. 那么问题来了,什么才是正确的?我们就是为问题而生的
  5. .net微信开发吐血总结
  6. C语言求两点之间的距离程序,C语言求空间两点之间的距离
  7. Running Median
  8. VisualNet地税管网综合资源管理系统
  9. oracle12c数据库命令,oracle 12c 常用命令
  10. Let‘s Go Rust 系列之定时器 Ticker Timer
  11. 局域网本地连接计算机,Windows XP下让电脑通过无线网卡共享本地连接实现局域网共享宽带上网...
  12. 让古天乐心动的这位女星,鼻子塌却美得风华绝代
  13. wordpress更换模板影响网站SEO吗?
  14. haxm intel庐_如何开启Intel HAXM功能
  15. F5的SSL加解密和负载均衡器如何提高安全性?
  16. 为什么越来越多的人不排斥参加职业培训?
  17. 傅盛离职内情:从360叛将到腾讯马前卒 --学习周鸿祎的优点+360怎么做起来的
  18. 8090的你该知道的......很长,但看完后,你会成长
  19. 百度站长资源平台自动批量抓取诊断工具(站长站群seo工具)
  20. 佳能80d有人脸识别吗_3250万像素+10张/秒连拍!佳能90D单反曝光

热门文章

  1. codelite14中文语言包_Windows下CodeLite完美支持中文的正确设置方法
  2. Java Spring Boot 2.0 实战之制作Docker镜像并推送到Docker Hub和阿里云仓库
  3. 外网访问FTP出错200 Type set to A
  4. HTML DOM全解和案例
  5. 检测数据类型的几种方式
  6. ARC下的所有权修饰符
  7. Cocos2d-x 基础元素
  8. Windows Server 2003 简介
  9. 转:Oracle SQL 内置函数大全 (一)
  10. InfoPath表单实战