糖果
作者:
恒_41
,
2024-04-09 18:55:25
,
所有人可见
,
阅读 2
#include<iostream>
#include<cstring>
using namespace std;
const int N = 105;
int f[N][N];
int w[N];
int n, k;
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++)scanf("%d", &w[i]);
for (int i = 0; i < k; i++)f[0][i] = -2e7;
f[0][0] = 0;//只有f[0][i]中只有0有意义,其他没意义,要设为负无穷,否则会报错
for (int i = 1; i <= n; i++)//i表示从前i个物品当中选
for (int j = 0; j < k; j++)//j表示这些物品的价值模k等于j因此j是从0到k
f[i][j] = max(f[i - 1][j], f[i - 1][(j - w[i] % k + k) % k] + w[i]);//求j-w[i]模以k的余数要让w[i]先模k
printf("%d\n", f[n][0]);//输出从前n个产品中选,且总价是k的余数
return 0;
}