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.
Only odd positions need to be valleys. Even positions are peaks, and making a peak bigger never hurts.
Think about the biggest value position can ever have for free. Operation 1 can copy a prefix maximum, but it can never create something larger than the original prefix maximum up to .
Let . Any final value at position is at most , no matter how you order the operations.
For an odd position , its left neighbor is an even peak, so finally . Therefore .
The lower bound is achievable: first use operation 1 on every even index to make it , then only decrease odd positions that are too large. So each odd pays exactly enough to become below its relevant even peak.
The annoying-looking part is operation 1 using the current prefix max. The trick is that we can do all useful increases first, before decreasing anything. So operation order is not a curse; it is basically free peak inflation.
Prefix max cap
Define
.
Claim: position can never become larger than .
Initially this is true. Decreasing obviously keeps it true. If we apply operation 1 at position , the new value becomes the current maximum among positions . But every one of those positions is still at most its own original prefix maximum, and all of those are at most . So the result is also at most .
So is a hard ceiling for position .
Who needs to be changed?
The final pattern is
Even positions are peaks. Bigger peaks are always good, because they only need to be greater than their neighbors. Since operation 1 is free, we should make every even position as large as possible: set even position to .
Odd positions are valleys. They may need to be decreased.
For odd , we only need
.
Since , we must have
.
For odd , the left neighbor is even, so
.
And because ,
.
If also has a right neighbor, that right even peak has cap . Since prefix maxima never decrease, , so the left peak is the tighter or equal bound. Nice. No DP, no drama.
Cost for one odd position
Let the largest allowed final value for odd position be:
If is already at most this value, cost is .
Otherwise, we must decrease it to that value, costing
.
So we add
over all odd positions.
Why this is actually achievable
Do this construction:
Now every odd position is strictly smaller than the even peak to its left, or for , strictly smaller than position . If it also has a right even neighbor, that right peak is at least as large as the left peak because prefix maxima are nondecreasing.
So the array is awesome, and the cost exactly matches the lower bound. That means it is optimal.
Complexity
Compute prefix maxima once and scan odd indices.
Time: per test case.
Memory: , or extra if streamed carefully. The simple array version is already totally fine.
#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 + 1), pref(n + 1, 0);
for (int i = 1; i <= n; i++) {
cin >> a[i];
pref[i] = max(pref[i - 1], a[i]);
}
ll ans = 0;
for (int i = 1; i <= n; i += 2) {
ll limit;
if (i == 1) limit = pref[2] - 1;
else limit = pref[i - 1] - 1;
if (a[i] > limit) ans += a[i] - limit;
}
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 + 1), pref(n + 1, 0);
for (int i = 1; i <= n; i++) {
cin >> a[i];
pref[i] = max(pref[i - 1], a[i]);
}
ll ans = 0;
for (int i = 1; i <= n; i += 2) {
ll limit;
if (i == 1) limit = pref[2] - 1;
else limit = pref[i - 1] - 1;
if (a[i] > limit) ans += a[i] - limit;
}
cout << ans << '\n';
}
}