Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
Give every index a sign: even indices are positive, odd indices are negative. Then a subarray is valid exactly when its signed sum is at least .
Decreasing an even-indexed element only makes the signed sums smaller, so it never helps. All useful operations are on odd indices.
You do not need to check every subarray. Any interval of length at least can be split into chunks of length and .
So it is enough to make every length- and length- subarray valid. This collapses to one local rule: for every even index , the odd neighbors around it must satisfy , where missing neighbors count as .
Process even indices from left to right. If , the excess must be deleted from the odd neighbors; delete from the right neighbor first because it may help future constraints too.
Define a signed version of the array:
The condition says: every subarray of length at least must have signed sum at least .
So even indices are the good guys and odd indices are the debt. Decreasing an even-indexed value would decrease a positive term, making constraints harder while also costing an operation. That is paying money to punch yourself in the face. In an optimal solution, we only decrease odd-indexed elements.
Why local checks are enough
Suppose every subarray of length and has nonnegative signed sum.
Any longer subarray can be split into consecutive blocks of length and possibly one block of length :
Each block has nonnegative signed sum, so the whole subarray also has nonnegative signed sum. Therefore, checking all subarrays is unnecessary; length and length already force everything.
Now simplify those local constraints.
Look at an even index . Its odd neighbors are and , if they exist. The length- subarray
requires
At the boundary, a missing neighbor just contributes , so the same rule also covers length- edge cases:
This one rule for every even index is enough. Since all values are nonnegative, it also implies the adjacent length- constraints. For example, if , then certainly and .
So the problem becomes:
Greedy choice
Process even indices from left to right.
At an even index , suppose currently
Let
At least extra operations are unavoidable, because the sum of these two odd neighbors must go down by that much.
Which neighbor should we decrease?
Decrease the right neighbor first.
Why? The left neighbor will not appear in any future even-index constraint. It was only relevant to the current even index and maybe previous ones. Decreasing it now cannot help the future.
The right neighbor will appear in the next constraint around even index . Making it smaller can only help later. So spending deletions on the right side first is never worse.
If the right neighbor is not large enough to absorb all extra, delete the rest from the left neighbor.
This fixes the current constraint using exactly the minimum forced number of operations, and it makes future constraints as easy as possible. Repeating this left to right gives the global minimum.
Algorithm
For every even index :
extra = a[i-1] + a[i+1] - a[i], with .extra <= 0, this even index is already fine.extra as possible.extra remains, decrease by the rest.All arithmetic must use long long, since the answer can be much larger than .
Complexity
Each element is touched only a constant number of times, so the complexity is
per test case, and overall.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<ll> a(n + 2, 0);
for (int i = 1; i <= n; i++) cin >> a[i];
ll ans = 0;
for (int i = 2; i <= n; i += 2) {
ll extra = a[i - 1] + a[i + 1] - a[i];
if (extra <= 0) continue;
ll takeRight = min(extra, a[i + 1]);
a[i + 1] -= takeRight;
ans += takeRight;
extra -= takeRight;
a[i - 1] -= extra;
ans += extra;
}
cout << ans << '\n';
}
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<ll> a(n + 2, 0);
for (int i = 1; i <= n; i++) cin >> a[i];
ll ans = 0;
for (int i = 2; i <= n; i += 2) {
ll extra = a[i - 1] + a[i + 1] - a[i];
if (extra <= 0) continue;
ll takeRight = min(extra, a[i + 1]);
a[i + 1] -= takeRight;
ans += takeRight;
extra -= takeRight;
a[i - 1] -= extra;
ans += extra;
}
cout << ans << '\n';
}
}