This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcount
const int N = 2e5 + 7, INF = 1e18 + 7;
int a[N];
bool used[N];
signed main() {
#ifdef HOME
//freopen("input.txt", "r", stdin);
#else
ios_base::sync_with_stdio(0); cin.tie(0);
#endif
int n; cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i];
int ans = 0;
for (int _ = 1; _ <= (n + 1) >> 1; ++_) {
int mx = -INF;
for (int i = 1; i <= n; ++i) {
if (!used[i] && !used[i - 1] && !used[i + 1]) mx = max(mx, a[i]);
}
vector <int> p;
for (int i = 1; i <= n; ++i) {
if (used[i]) p.app(i);
}
int l = 0;
while (l < p.size()) {
int r = l;
while (r + 1 < p.size() && p[r + 1] == p[l] + 2) ++r;
if (1 < p[l] && p[r] < n) {
int sum = a[p[l] - 1] + a[p[r] + 1];
for (int i = p[l] + 1; i < p[r]; i += 2) sum += a[i];
for (int i = p[l]; i <= p[r]; i += 2) sum -= a[i];
mx = max(mx, sum);
}
l = r + 1;
}
ans += mx;
bool mem = 0;
for (int i = 1; i <= n; ++i) {
if (!used[i] && !used[i - 1] && !used[i + 1] && a[i] == mx) {
used[i] = 1; mem = 1; break;
}
}
if (!mem) {
l = 0;
while (l < p.size()) {
int r = l;
while (r + 1 < p.size() && p[r + 1] == p[l] + 2) ++r;
if (1 < p[l] && p[r] < n) {
int sum = a[p[l] - 1] + a[p[r] + 1];
for (int i = p[l] + 1; i < p[r]; i += 2) sum += a[i];
for (int i = p[l]; i <= p[r]; i += 2) sum -= a[i];
if (sum == mx) {
used[p[l] - 1] = used[p[r] + 1] = 1;
for (int i = p[l] + 1; i < p[r]; i += 2) used[i] = 1;
for (int i = p[l]; i <= p[r]; i += 2) used[i] = 0;
break;
}
}
l = r + 1;
}
}
cout << ans << '\n';
}
}
Compilation message (stderr)
candies.cpp: In function 'int main()':
candies.cpp:29:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (l < p.size()) {
~~^~~~~~~~~~
candies.cpp:31:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (r + 1 < p.size() && p[r + 1] == p[l] + 2) ++r;
~~~~~~^~~~~~~~~~
candies.cpp:51:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (l < p.size()) {
~~^~~~~~~~~~
candies.cpp:53:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (r + 1 < p.size() && p[r + 1] == p[l] + 2) ++r;
~~~~~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |