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.
Ignore the randomness for a second. Ask: what quantity definitely changes every time step succeeds?
Step only touches positions where . Each successful step reduces one such excess by exactly .
Step cannot create a new position, because it only increases values with by . At worst they become equal.
So the number of iterations where step succeeds is exactly the total initial excess:
The Lever also performs one final iteration where step is ignored, then stops. So the answer is total positive excess plus .
The key is to not get baited by the word "random". The random choices do not matter. Like, genuinely zero drama here.
For each position, define its excess as:
This is how many times that position still needs to be decreased before it is no longer greater than .
Now look at the two steps.
Step 1: choose some index with and decrease it by .
That always reduces the total excess by exactly .
If , it becomes . If , it becomes . Either way, total positive excess drops by one.
Step 2: choose some index with and increase it by .
This never increases the positive excess. Since the chosen value is below , after adding , it is still below or exactly equal to . It cannot jump past and become greater. So step is basically irrelevant for deciding when step runs out of work.
So if initially
then step succeeds exactly times. After those successful decreases, there is no index left with .
But the Lever does not stop immediately after the last successful decrease. It starts one more iteration. In that final iteration, step is ignored, and only after the iteration does the Lever stop.
Therefore the answer is:
That extra is the whole trick. Miss it and the sample punches you in the face.
#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<int> a(n), b(n);
for (int &x : a) cin >> x;
for (int &x : b) cin >> x;
int excess = 0;
for (int i = 0; i < n; i++) {
excess += max(0, a[i] - b[i]);
}
cout << excess + 1 << '\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;
cin >> n;
vector<int> a(n), b(n);
for (int &x : a) cin >> x;
for (int &x : b) cin >> x;
int excess = 0;
for (int i = 0; i < n; i++) {
excess += max(0, a[i] - b[i]);
}
cout << excess + 1 << '\n';
}
return 0;
}