答案
2023-11-05 15:37:42
发布于:浙江
40阅读
0回复
0点赞
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> wealth(n);
for (int i = 0; i < n; i++) {
cin >> wealth[i];
}
int totalWealth = 0;
for (int i = 0; i < n; i++) {
totalWealth += wealth[i];
}
int Wealth1 = wealth[0];
int Wealth2 = wealth[n - 1];
int remainingWealth = totalWealth - Wealth1 - Wealth2;
if (Wealth1 == Wealth2) {
totalWealth = remainingWealth;
}
int leftIndex = 0;
int rightIndex = n - 1;
while (leftIndex < rightIndex) {
if (Wealth1 == Wealth2) {
totalWealth = remainingWealth;
leftIndex++;
Wealth1 += wealth[leftIndex];
remainingWealth -= wealth[leftIndex];
}
while (leftIndex < rightIndex && Wealth1 < Wealth2) {
leftIndex++;
Wealth1 += wealth[leftIndex];
remainingWealth -= wealth[leftIndex];
}
while (leftIndex < rightIndex && Wealth1 > Wealth2) {
rightIndex--;
Wealth2 += wealth[rightIndex];
remainingWealth -= wealth[rightIndex];
}
}
cout << totalWealth << endl;
return 0;
}
这里空空如也
有帮助,赞一个