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 bank as the final bank and ask: how many transfers can the other banks send into it?
A bank with rubles can directly send exactly transfers before it drops below .
The annoying-looking part is chaining transfers through other banks. But one transfer from a non-target bank decreases one bank’s chunk count by and can increase another bank’s chunk count by at most .
So among all non-target banks, the total number of full -chunks never increases. Chaining can move capacity around, but it cannot create more transfers. No free money glitch, sadly.
Let and . If bank is the target, the best final value is . Take the maximum over all .
Fix some bank and pretend this is the bank we want to maximize.
The obvious plan is: every other bank sends as many transfers as it can directly into . Bank can send
transfers, and each one adds rubles to . This gives
The only real question is whether some clever chain of transfers can beat that by combining leftovers. That sounds plausible, but it is the trap.
For this fixed target , define
Think of as the current money in the target plus the value of all full transferable chunks outside it.
Now check what any operation can do to :
So is an upper bound on the final amount in bank . The direct strategy reaches that bound: make every bank send exactly transfers into .
Therefore, if bank is chosen as the answer bank, the optimum is exactly
Let
Then for each possible target , the value is
Take the maximum over all banks.
Edge cases are handled naturally:
The total sum of over all test cases is at most , so one linear pass per test case is plenty.
Complexity:
per test case, with storage, or extra storage besides the array.
#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 x, y;
cin >> n >> x >> y;
vector<ll> a(n), chunks(n);
ll totalChunks = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
chunks[i] = a[i] / x;
totalChunks += chunks[i];
}
ll ans = 0;
for (int i = 0; i < n; i++) {
ans = max(ans, a[i] + y * (totalChunks - chunks[i]));
}
cout << ans << '\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 x, y;
cin >> n >> x >> y;
vector<ll> a(n), chunks(n);
ll totalChunks = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
chunks[i] = a[i] / x;
totalChunks += chunks[i];
}
ll ans = 0;
for (int i = 0; i < n; i++) {
ans = max(ans, a[i] + y * (totalChunks - chunks[i]));
}
cout << ans << '\n';
}
return 0;
}