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.
A normal move costs one action for one added block. The only way to save actions is the special move that adds to both towers at once.
Start from the boring baseline: use only normal moves. That costs actions.
Each special move replaces two normal moves with one action, so it saves exactly . Now the job is to maximize how many special moves can happen.
A special move from height to is possible only if both towers can first reach height , and both targets are at least .
So valid special-move starting heights are . Their count is .
Research note: I checked the Codeforces problem page and the official Kotlin Heroes 14 editorial. The official post’s tutorial text is still loading, but it includes a short accepted-style reference solution, which matches the derivation below. I also checked the Codeforces status page, but it returned an internal error, so the statement remains the source of truth.
Let the required increases be:
If we ignore the special move, the answer is clearly:
That is the dumb baseline. Not wrong, just leaving coupons on the floor.
The special move adds one block to both towers in one action. Compared to doing those two increments separately, each special move saves exactly one action. Therefore:
Now we only need to count how many special moves can possibly be used.
A special move can start only when both towers currently have the same height . Since tower heights only increase, both initial heights must be at most :
After using the special move, both towers become , so both targets must be at least :
So every special move corresponds to an integer starting height
The number of such heights is:
That gives the formula:
Why this is achievable:
Why it is optimal:
Every special move must start at some equal height . Because heights only increase, these starting heights are all distinct and must lie inside
There are only such heights, so no strategy can use more than special moves. Since the construction above uses exactly , it is optimal. No hidden DP, no simulation, no weird greedy trap. Just count the valid discount moves.
Edge cases:
long long.Complexity per test case is time and memory.
#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--) {
ll a, b, c, d;
cin >> a >> b >> c >> d;
ll total = (c - a) + (d - b);
ll special = max(0LL, min(c, d) - max(a, b));
cout << total - special << '\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--) {
ll a, b, c, d;
cin >> a >> b >> c >> d;
ll total = (c - a) + (d - b);
ll special = max(0LL, min(c, d) - max(a, b));
cout << total - special << '\n';
}
return 0;
}