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.
Pick a final equal value first. Once is fixed, the number of operations is not flexible at all: it is , where is the array sum.
Instead of counting coins directly, count the operations that give no coin. A no-coin operation happens exactly when you increment an element that is currently equal to the array minimum.
Let be the initial minimum and let be how many elements equal . If the final value is , the minimum must be raised through levels, causing at least no-coin operations. Also, the other initial minimum elements must each leave with no coin.
That lower bound is actually reachable: keep one original minimum as an anchor, raise every other element to , then raise the anchor last. The anchor is the whole scam.
For fixed , coins are . This increases as increases, so after checking that reaching the current maximum is possible, use the largest reachable .
The clean way to solve this is to stop thinking about individual operations first. Pick the final value, then the scheduling problem becomes much smaller.
Suppose the final equal value is . Since we can only increase values, we need . The number of operations is forced:
where is the sum of the array. So feasibility for this just means .
Now the real question is: among those forced operations, how many can earn coins?
Coin operations vs coinless operations
An operation earns a coin exactly when the chosen element is strictly greater than the current minimum before the increment.
So the only bad operations are the ones where we increment an element currently equal to the minimum. Call those coinless operations.
Then:
So we want to minimize the number of coinless operations.
Let:
Assume first that . If , the array is already equal and the answer is just .
The unavoidable coinless operations
The array minimum starts at and must end at . Since each operation increases one element by exactly , the global minimum must move from:
For each of these level increases, some element that is currently minimum must be incremented. That operation gives no coin. So we already need at least:
coinless operations.
But there is one more annoying detail. If elements start at the minimum , then all of them must eventually leave value . The operation that makes the minimum finally rise from to accounts for one of them. The other minimum elements also need their first increment from to , and those increments are also coinless.
So any strategy needs at least:
That is the tax. No clever scheduling dodges it.
Why this lower bound is tight
Now for the fun part: we can actually hit that bound exactly.
Pick one element that initially equals and keep it untouched as an anchor. While this anchor stays at , every element above can be increased all the way to , and every one of those operations earns a coin.
For the other elements that also started at :
After everyone else reaches , raise the anchor from to . It is the minimum the whole time, so those operations give no coins.
Total coinless operations:
Exactly the lower bound. So this is optimal.
Therefore, for a fixed final value :
Choosing the best final value
First, we must be able to make the array equal at all. The smallest possible final value is the current maximum . The required operations are:
If , print .
Otherwise, which final value should we choose?
For , increasing by adds total operations. But it adds only extra unavoidable coinless operation, because the anchor must climb one more step. So coins increase by .
Since , larger feasible is always at least as good, and usually strictly better. So we take the largest equal value reachable with at most operations:
If , no full useful layer can be completed, so the answer is . Otherwise use the formula above.
Algorithm
For each test case:
Complexity
Sorting is not needed. We only scan the array once.
Time: per test case.
Memory: besides the input array, and you can even avoid storing it.
#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;
ll k;
cin >> n >> k;
ll sum = 0, mn = LLONG_MAX, mx = LLONG_MIN, cntMin = 0;
for (int i = 0; i < n; i++) {
ll x;
cin >> x;
sum += x;
mx = max(mx, x);
if (x < mn) {
mn = x;
cntMin = 1;
} else if (x == mn) {
cntMin++;
}
}
ll need = mx * n - sum;
if (need > k) {
cout << -1 << '\n';
continue;
}
ll target = (sum + k) / n;
if (target == mn) {
cout << 0 << '\n';
continue;
}
ll ops = target * n - sum;
ll coinless = (target - mn) + (cntMin - 1);
cout << ops - coinless << '\n';
}
return 0;
}#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;
ll k;
cin >> n >> k;
ll sum = 0, mn = LLONG_MAX, mx = LLONG_MIN, cntMin = 0;
for (int i = 0; i < n; i++) {
ll x;
cin >> x;
sum += x;
mx = max(mx, x);
if (x < mn) {
mn = x;
cntMin = 1;
} else if (x == mn) {
cntMin++;
}
}
ll need = mx * n - sum;
if (need > k) {
cout << -1 << '\n';
continue;
}
ll target = (sum + k) / n;
if (target == mn) {
cout << 0 << '\n';
continue;
}
ll ops = target * n - sum;
ll coinless = (target - mn) + (cntMin - 1);
cout << ops - coinless << '\n';
}
return 0;
}