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.
For a fixed difficulty , the exact identities of the swords do not matter anymore. Only the number of swords with matters.
If you have usable swords, you can complete exactly the largest prefix of levels whose total required strikes is at most .
So precompute prefix sums . Then for any usable-sword count , completed levels are upper_bound(p, m).
You never need to try random difficulties. Between two sword strengths, the usable set is unchanged, so increasing only helps. The best is one of the values in .
Sort sword strengths. For each distinct strength , find how many swords are at least , then maximize . That is the whole trick; no monster combat simulator needed.
For a chosen difficulty , every sword with strength at least becomes just “one usable strike,” and every weaker sword is trash. Brutal, but simple.
Let
With usable swords, we can only spend them from level onward. Level costs swords, so completing the first levels costs
Therefore, for this , the number of completed levels is the largest such that
The score is then
Now the important false assumption to kill: we do not need to binary search the answer. The difficulty should only be checked at sword strengths.
Why? Suppose is between two adjacent sword strengths. Then the set of usable swords does not change, so does not change, and the completed level count does not change. In that whole interval, increasing increases the score. So the best value in the interval is at the right endpoint, which is some sword strength.
So the plan is:
If the sorted strengths are increasing, and we are at index , then all swords from to have strength at least , so
For duplicate values, only the first occurrence matters, because it gives the largest usable count for that same difficulty. Later duplicate positions have the same but fewer counted swords if handled naively, which would only make the result worse. So skip repeated strengths after processing their first occurrence.
To compute completed levels, store prefix sums:
Then
which is exactly upper_bound(p.begin(), p.end(), m) - p.begin().
Edge cases:
long long.Sorting costs per test case, and each distinct strength uses one binary search, so total complexity is
per test case. Across all test cases, this is fine because the total is at most .
#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<ll> a(n), pref(n);
for (ll &x : a) cin >> x;
for (int i = 0; i < n; i++) {
ll b;
cin >> b;
pref[i] = b + (i ? pref[i - 1] : 0LL);
}
sort(a.begin(), a.end());
ll ans = 0;
for (int i = 0; i < n; i++) {
if (i > 0 && a[i] == a[i - 1]) continue;
ll usable = n - i;
ll levels = upper_bound(pref.begin(), pref.end(), usable) - pref.begin();
ans = max(ans, a[i] * levels);
}
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;
cin >> n;
vector<ll> a(n), pref(n);
for (ll &x : a) cin >> x;
for (int i = 0; i < n; i++) {
ll b;
cin >> b;
pref[i] = b + (i ? pref[i - 1] : 0LL);
}
sort(a.begin(), a.end());
ll ans = 0;
for (int i = 0; i < n; i++) {
if (i > 0 && a[i] == a[i - 1]) continue;
ll usable = n - i;
ll levels = upper_bound(pref.begin(), pref.end(), usable) - pref.begin();
ans = max(ans, a[i] * levels);
}
cout << ans << '\n';
}
return 0;
}