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 one oven, think about what happens if you collect it multiple times. Does the exact pattern of visits matter, or only something simpler?
If oven is last collected at time , then across all visits to that oven you collect exactly cakes from it. The earlier visits just split that amount into chunks.
So the problem becomes: assign some ovens distinct last-collection times from to to maximize . Extra visits before those last times are just filler.
Big rates should get big last times. If but oven gets an earlier last time than oven , swapping them only helps. Classic exchange argument, no drama.
Only the best ovens can have positive last-collection times. Sort descending, give them times , and sum the products.
The annoying-looking part is that Maple can revisit ovens, and each oven keeps accumulating cakes between visits. That sounds dynamic. It is not. The trick is to stop caring about every visit and care only about the last visit to each oven.
Suppose oven is collected at times
The collected amount from this oven is
Everything telescopes. Earlier visits do not change the total contribution of that oven; they only split the same pile into smaller piles. So oven contributes exactly
If an oven is never collected, its last collection time is , so it contributes .
Reduced problem
Now we just need to choose last collection times.
There are seconds, so there are only collection actions. Therefore at most ovens can have a positive last collection time. Also, of course, there are only ovens. So only
ovens matter.
Their last collection times must be distinct seconds from to . To maximize the answer, we should clearly use the largest possible times:
But which oven gets which time?
Greedy ordering
The largest baking rates should get the latest times.
If , but oven gets time and oven gets later time where , their contribution is
After swapping:
The difference is
So the swap improves the answer. Therefore any optimal solution must put bigger on bigger times. Greedy wins; the problem politely steps aside.
Algorithm
The answer can be large: up to about , so use long long.
Complexity
Sorting dominates:
per test case, with total bounded by , so this is easily fine.
#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 m;
cin >> n >> m;
vector<ll> a(n);
for (ll &x : a) cin >> x;
sort(a.rbegin(), a.rend());
int k = min<ll>(n, m);
ll ans = 0;
for (int i = 0; i < k; i++) {
ans += a[i] * (m - 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 m;
cin >> n >> m;
vector<ll> a(n);
for (ll &x : a) cin >> x;
sort(a.rbegin(), a.rend());
int k = min<ll>(n, m);
ll ans = 0;
for (int i = 0; i < k; i++) {
ans += a[i] * (m - i);
}
cout << ans << '\n';
}
return 0;
}