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.
Try to forget the whole array for a second and focus on one index . What is the largest value this position could ever end up with if you are free to operate on the suffix to its right however you want?
For index , the only useful thing the right side can do is provide some value of to add once. If that contribution is positive, great. If it is non-positive, adding it is basically volunteering to eat garbage.
Define as the maximum final value that position can achieve using only operations on indices . Then What are the only two choices for position ?
Those two choices are: So from right to left, if the optimized value of the next position is positive, absorb it; otherwise skip the operation.
Now the punchline: if , then index can never be positive in any valid final array. And the array itself is actually achievable by processing from right to left with that greedy rule. So the answer is just the count of indices where the backward recurrence ends up positive.
Instead of directly maximizing the number of positive elements, first ask:
What is the maximum value each position can possibly become?
Let be the maximum final value that index can obtain if we are allowed to perform any valid operations on the suffix .
For the last element, nothing can affect it:
Now look at some index .
There are only two sane options:
If you want to maximize index , then obviously you should apply operation when index is as large as possible. That best possible value is exactly .
So: Which is the same as:
That recurrence is the whole problem. Everything else is just paperwork.
If , then adding it to is strictly better than ignoring it.
If , then adding it is useless or harmful, so the best choice is to skip operation .
So while scanning from right to left:
This is basically: take free money, refuse debt. Revolutionary stuff.
If , then even the best possible final value of index is non-positive. So in any valid final array, position cannot be positive.
Therefore the answer is at most:
Now the nice part: the array itself is achievable.
Process indices from right to left. At step :
This never changes positions to the right, so the construction is valid. Hence we can make every index with positive simultaneously.
So the answer is exactly:
For each test case:
Here is just the current while moving left.
We proved:
Therefore the maximum possible number of positive elements is exactly the number of positive values among the .
Done. No black magic, no cursed DP state explosion, just a very polite backward sweep.
Each test case is processed in one pass from right to left.
Over all test cases, this is , which easily fits the limits.
#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);
for (ll &x : a) cin >> x;
ll cur = a[n - 1]; // cur = mx_i while sweeping from right to left
int ans = (cur > 0);
for (int i = n - 2; i >= 0; --i) {
cur = a[i] + max(0LL, cur);
if (cur > 0) ++ans;
}
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);
for (ll &x : a) cin >> x;
ll cur = a[n - 1]; // cur = mx_i while sweeping from right to left
int ans = (cur > 0);
for (int i = n - 2; i >= 0; --i) {
cur = a[i] + max(0LL, cur);
if (cur > 0) ++ans;
}
cout << ans << '\n';
}
}