日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

C. Three Parts of the Array(切割字符串)

發布時間:2025/4/16 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C. Three Parts of the Array(切割字符串) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

????????????????????????????????????????? ? ? ? ? ? ? ? ? ? ? ? ?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(切割字符串)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。