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 group key covers up to students. So stop thinking about exact groups of only; the last key can be partially wasted, and that might still be cheaper.
For every full block of students, the only real comparison is between buying individual keys for or one group key for .
If , group keys are trash for full groups: just buy individual keys for everyone. The cost is simply .
If , use group keys for every full block of . For the leftover students, choose the cheaper of and one more group key .
Let and . The answer is
Use long long, because , so the answer can be around . Tiny problem, big numbers. Classic CF nonsense.
A group key can cover at most students, and it can also cover or students. So for each full block of students, compare:
If , then one group key is never better than three individual keys. Even for leftovers, a group key cannot magically become cheaper than all needed individual keys unless it is directly compared there. The all-individual option covers this cleanly.
Let
A natural strategy is:
The full triples cost:
For the leftover students, there are only two sane options:
because one group key can cover those students even if .
So this strategy costs:
We should also consider buying only individual keys:
Therefore the answer is:
This handles every case, including , , and cases where the last partially-used group key is still cheaper.
Each test case uses only constant-time arithmetic:
Total complexity:
Use long long, because values like do not fit in int. int would explode quietly, which is the worst kind of explosion.
#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 n, a, b;
cin >> n >> a >> b;
ll q = n / 3;
ll r = n % 3;
ll all_individual = n * a;
ll use_groups = q * b + min(r * a, b);
cout << min(all_individual, use_groups) << '\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 n, a, b;
cin >> n >> a >> b;
ll q = n / 3;
ll r = n % 3;
ll all_individual = n * a;
ll use_groups = q * b + min(r * a, b);
cout << min(all_individual, use_groups) << '\n';
}
return 0;
}