CodeForces 1006C Three Parts of the Array
题目链接:CodeForces 1006C Three Parts of the Array
题目大意:
题解:
两个指针分别指向头尾,向中间移动,计算\(sum1\)和\(sum2\),哪边小移动哪边的指针,记录\(sum1 = sum2\)时\(sum1\)的值,
#include <iostream>
using namespace std;
long long num[1000010];
int n;
int main() {
ios::sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> num[i];
}
int p = 1, q = n;
long long sum1 = 0, sum2 = 0, ans = 0;
while (p <= q) {
if (sum1 > sum2) {
sum2 += num[q--];
} else if (sum2 > sum1) {
sum1 += num[p++];
}
if (sum1 == sum2) {
ans = max(ans, sum1);
sum1 += num[p++];
}
}
cout << ans << endl;
return 0;
}