This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2")
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
int INF = 2e9;
void solve() {
int n;
cin >> n;
vector<int> a(2 * n);
for (int i = 0; i < n; i++) {
cin >> a[i];
a[i + n] = a[i];
}
int sum = 0;
for (int i = 0; i < n / 2; i++) sum += a[i];
vector<int> range(2 * n);
range[0] = sum;
multiset<int, greater<int>> st;
int all_sum = 0, ans = 0, l = 1, r = 1;
for (int i = n / 2; i < 2 * n; i++) {
sum += a[i] - a[i - n / 2];
range[i - n / 2 + 1] = sum;
if (i < n) {
st.insert(sum);
r = i - n / 2 + 1;
}
}
for (int i = 0; i < n; i++) {
all_sum += a[i];
}
for (int i = 0; i < n; i++) {
ans = max(ans, all_sum - *st.begin());
st.erase(st.find(range[l++]));
st.insert(range[++r]);
}
cout << ans;
}
int main() {
::srand(::time(0));
ios_base::sync_with_stdio(false);
cout.tie(nullptr);
cin.tie(nullptr);
int queries = 1;
#ifdef test_cases
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
// cin >> queries;
#else
// cin >> queries;
#endif
for (int test_case = 1; test_case <= queries; test_case++) {
solve();
// cout << '\n';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |