-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThreePartsOfArray.cpp
54 lines (49 loc) · 1.02 KB
/
ThreePartsOfArray.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/** http://codeforces.com/contest/1006/problem/C
#two-pointer #binary-search
*/
#include<iostream>
#include<vector>
using namespace std;
typedef long long ll;
int main() {
int n;
cin >> n;
vector<int> arr(n);
int tmp;
for (int i=0; i < n; i++) {
cin >> tmp;
arr[i] = tmp;
}
ll sum_left = 0;
ll sum_right = 0;
ll sum = 0;
int l=0;
int r=n-1;
bool leftFlg = true;
bool rightFlg = true;
while(l < r) {
if (leftFlg) {
sum_left += arr[l];
leftFlg = false;
}
if (rightFlg) {
sum_right += arr[r];
rightFlg = false;
}
if (sum_left == sum_right) {
sum = sum_left;
l++;
r--;
leftFlg = true;
rightFlg = true;
} else if (sum_left > sum_right) {
r--;
rightFlg = true;
} else {
l++;
leftFlg = true;
}
}
cout << sum << endl;
return 0;
}