相当于将 \(n\) 个数分成 \(k+1\) 组,将每组的最大收益相加。
容易发现组内的数不增最优。
考虑开个堆,维护当前 \(k+1\) 组的和即可。
code
/*
p_b_p_b txdy
AThousandSuns txdy
Wu_Ren txdy
Appleblue17 txdy
*/
#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mems(a, x) memset((a), (x), sizeof(a))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ldb;
typedef pair<ll, ll> pii;
const int maxn = 500100;
ll n, m, a[maxn], b[maxn];
void solve() {
scanf("%lld%lld", &n, &m);
for (int i = 1; i <= n; ++i) {
scanf("%lld", &a[i]);
}
sort(a + 1, a + n + 1, greater<ll>());
ll ans = 0;
priority_queue<ll> pq;
for (int i = 0; i <= m; ++i) {
pq.push(0);
}
for (int i = 1; i <= n; ++i) {
ll x = pq.top();
pq.pop();
ans += x;
pq.push(x + a[i]);
}
printf("%lld\n", ans);
}
int main() {
int T = 1;
// scanf("%d", &T);
while (T--) {
solve();
}
return 0;
}