题解
2023-12-28 22:09:42
发布于:江苏
39阅读
0回复
0点赞
#include <cstdio>
#include <algorithm>
using namespace std;
int n,m,t[505],mem[505][505];
int solve(int i,int st) {
if (i == n + 1) return 0;
if (st < t[i]) return solve(i, t[i]);
if (mem[i][st - t[i]]) return mem[i][st - t[i]];
int sum = 0,j = i;
while (j <= n && t[j] <= st)
sum += t[j++];
int best = st * (j - i) - sum + solve(j, st + m);
for (; j <= n; j++) {
sum += t[j];
best = min(t[j] * (j - i + 1) - sum + solve(j + 1, t[j] + m), best);
}
return mem[i][st - t[i]] = best;
}
int main() {
scanf("%d%d",&n,&m);
for (int i = 1; i <= n; i++)
scanf("%d",&t[i]);
sort(t + 1, t + n + 1);
printf("%d\n",solve(1, 0));
return 0;
}
这里空空如也
有帮助,赞一个