#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
#define all(x) begin(x), end(x)
#define sz(x) (int) x.size()
const int PRIME = 1e9 + 7;
int n;
vector<int> prefSum;
// i1 inclusive, i2 exclusive
int getCumSum(int i1, int i2){
i1 %= n; i2 %= n;
if(i1 > i2){
return prefSum[n] - prefSum[i1] + prefSum[i2];
}
return prefSum[i2] - prefSum[i1];
// use prefix sum vector
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
// freopen("TASK_NAME.in", "r", stdin);
// freopen("TASK_NAME.out", "w", stdout);
// Use scanf printf
cin >> n;
vector<int> scores(n);
prefSum.resize(n + 1);
prefSum[0] = 0;
int maxNumHacked = (n + 1) / 2;
int splitLen = (n-1) / 2;
int totSum = 0;
for(int i = 0; i < n; i++){
cin >> scores[i];
totSum += scores[i];
prefSum[i+1] = prefSum[i] + scores[i];
}
multiset<int> sums;
vector<int> minScores(n);
for(int i = 0; i < 2 * n; i++){
if(i >= maxNumHacked){
sums.erase(prefSum[i % n] - prefSum[(i - maxNumHacked) % n]);
// remove sum
}
sums.insert(prefSum[(i + maxNumHacked) % n] - prefSum[i % n]);
minScores[i % n] = *sums.begin();
// compare
}
int ans = INT32_MIN;
for(int minScore: minScores){
ans = max(ans, minScore);
}
cout << ans << '\n';
// if(n == 2){
// cout << max(scores[0], scores[1]) << '\n';
// return 0;
// }
// int maxScore = INT32_MIN;
// for(int i = 0; i < n; i++){
// // cout << i + 1 << ' ' << i + 1 + splitLen << ' ' << getCumSum(i + 1, i + 1 + splitLen) << '\n';
// // cout << i + (n+1)/2 << ' ' << i + (n+1)/2 + splitLen << ' ' << getCumSum(i + (n+1)/2, i + (n+1)/2 + splitLen) << '\n';
// // cout << '\n';
// maxScore = max(maxScore, min(getCumSum(i + 1, i + 1 + splitLen), getCumSum(i + (n+1)/2, i + (n+1)/2 + splitLen)) + scores[i]);
// }
// // for(int el: prefSum){
// // cout << el << ' ';
// // }
// cout << maxScore << '\n';
// int windowScore = 0;
// for(int i = 0; i < maxNumHacked; i++){
// windowScore += scores[i];
// }
// int minDiff = windowScore * 2 - totSum;
// int bestScore = windowScore;
// for(int i = 0; i < n; i++){
// // compare window starting at i
// // subtract element at i, add next element
// if(abs(windowScore * 2 - totSum) < minDiff && windowScore > totSum - windowScore){
// minDiff = windowScore * 2 - totSum;
// bestScore = windowScore;
// }
// // maxScore = min(minDiff, windowScore);
// windowScore -= scores[i];
// windowScore += scores[(i + maxNumHacked) % n];
// }
// cout << bestScore << '\n';
}
Compilation message
hac.cpp: In function 'int main()':
hac.cpp:42:9: warning: unused variable 'splitLen' [-Wunused-variable]
42 | int splitLen = (n-1) / 2;
| ^~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |