Algorithm


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,…,dn�1,�2,…,�� 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���1, the sum of elements of the second part be sum2���2 and the sum of elements of the third part be sum3���3. Among all possible ways to split the array you have to choose a way such that sum1=sum3���1=���3 and sum1���1 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,���1=∑1≤�≤���,
sum2=∑a+1≤i≤a+bdi,���2=∑�+1≤�≤�+���,
sum3=∑a+b+1≤i≤a+b+cdi.���3=∑�+�+1≤�≤�+�+���.

 

The sum of an empty array is 00.

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

Input

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

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

Output

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

Obviously, at least one valid way to split the array exists (use a=c=0�=�=0 and b=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 sum1���1: [1,3,1],[ ],[1,4][1,3,1],[ ],[1,4].

In the second example the only way to have sum1=4���1=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],[ ].

 



 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <bits/stdc++.h>

using namespace std;

int const N = 2e5 + 1;
int n, a[N];
long long cs[N];

int main() {
  cin >> n;
  for(int i = 0; i < n; ++i)
    cin >> a[i];
  
  for(int i = 1; i <= n; ++i)
    cs[i] = cs[i - 1] + a[i - 1];
  
  long long res = 0;
  for(int i = n, idx; i >= 1; --i) {
    long long cur = cs[n] - cs[i - 1];

    idx = lower_bound(cs, cs + n - (n - i + 1), cur) - cs;
    if(cs[idx] == cur)
      res = max(res, cur);
  }

  cout << res << endl;

  return 0;
}
Copy The Code & Try With Live Editor

Input

x
–
+
cmd
5
1 3 1 1 4

Output

x
–
+
cmd
5
Advertisements

Demonstration


Codeforces Solution-C. Three Parts of the Array-Solution in C, C++, Java, Python,Three Parts of the Array,Codeforces Solution

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+