#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e5 + 7;
int sumpref[MAXN];
int n;
int sum(int a, int b){
return sumpref[b] - sumpref[a-1];
}
int solve(){
int res = 0;
int bestabs = 2e9;
int k = n/2;
for(int i = 1; i + k-1 <= n; i++){
if(abs(sum(i, k+i-1) - (sumpref[n] - sum(i, k+i-1))) < bestabs){
res = max(sum(i, k+i-1), sumpref[n] - sum(i, k+i-1));
bestabs = abs(sum(i, k+i-1) - (sumpref[n] - sum(i, k+i-1)));
}
else if(abs(sum(i, k+i-1) - (sumpref[n] - sum(i, k+i-1))) <= bestabs)
res = max(max(res, sum(i, k+i-1)), sumpref[n] - sum(i, k+i-1));
}
if(n%2){
for(int i = 1; i + k <= n; i++){
if(abs(sum(i, k+i) - (sumpref[n] - sum(i, k+i))) < bestabs){
res = max(sum(i, k+i), sumpref[n] - sum(i, k+i));
bestabs = abs(sum(i, k+i) - (sumpref[n] - sum(i, k+i)));
}
else if(abs(sum(i, k+i) - (sumpref[n] - sum(i, k+i))) <= bestabs)
res = max(max(res, sum(i, k+i)), sumpref[n] - sum(i, k+i));
}
}
return res;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for(int i = 1; i <= n; i++){
cin >> sumpref[i];
sumpref[i] += sumpref[i-1];
}
cout << solve() << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |