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 separating the elements equal to from the elements greater than . The operation cost is a product, so behaves like that one friend who shows up and changes nothing.
What is the benefit of putting several numbers into the same nondecreasing subsequence? For two such numbers , compare removing them together (cost ) versus separately (cost ).
In fact, any chain containing numbers has cost . So combining numbers never beats paying for them one by one (at best, and tie). Thatβs the main kill shot.
So the only tricky part is the 's. A subsequence made only of 's costs exactly , no matter how many of them it contains. Also, a block of 's can be attached in front of some later number as , and the cost is still just .
Every block of 's that has some later element can be absorbed into the chain of the next such element. The only block that cannot be absorbed is the final suffix of 's, which exists iff . Therefore,
Yep, thatβs the whole problem. Pretty sneaky for an A.
Take any valid removed subsequence, and ignore all the 's inside it for a moment. Suppose the remaining numbers are
Its cost is
Now compare that with deleting those separately, which would cost
For two numbers ,
with equality only for .
So if you repeatedly βmergeβ numbers in a chain, the sum never decreases. That means for any chain containing numbers ,
So putting several numbers bigger than into the same subsequence is never better than paying for them separately. At best, for , it ties. Thatβs it. No black magic here.
A does not change a product:
So if a subsequence looks like
it costs just
Also, a subsequence consisting only of 's costs exactly
No matter how many 's you pack into it.
So the entire problem becomes:
Letβs prove a clean lower bound.
Every chain that contains at least one number costs at least the sum of those numbers in that chain. Summing over all such chains gives at least
What about chains made only of 's? Each such chain costs .
Now notice the crucial thing:
So if , at least one all- chain is unavoidable. Hence
If , such a forced leftover block does not exist, so we only get
Together,
Now we show this bound is achievable.
Look at every maximal block of 's.
This gives total cost exactly
So the lower bound is tight.
The minimum total cost is
where is if the last element is , and otherwise.
The modulus is basically decorative here, because the answer is tiny anyway. But sure, we can still mod it.
We just scan the array once.
For each test case:
With the given constraints, this is hilariously safe.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
const ll MOD = 676767677;
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i) cin >> a[i];
ll ans = 0;
for (int x : a) {
if (x > 1) ans += x;
}
if (a.back() == 1) ans += 1;
cout << (ans % MOD) << '\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();
const ll MOD = 676767677;
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i) cin >> a[i];
ll ans = 0;
for (int x : a) {
if (x > 1) ans += x;
}
if (a.back() == 1) ans += 1;
cout << (ans % MOD) << '\n';
}
}