AcWing 1047. 糖果
原题链接
简单
作者:
不知名的fE
,
2024-11-25 17:34:39
,
所有人可见
,
阅读 2
import java.util.*;
public class Main {
static final int N = 110;
static int[] a = new int[N];
static int[][] f = new int[N][N];
static int n, k;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt(); k = sc.nextInt();
sc.nextLine();
for (int i = 1; i <= n; i++) a[i] = Integer.parseInt(sc.nextLine());
Arrays.fill(f[0], -0x3f3f3f3f);
f[0][0] = 0;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < k; j++) {
f[i][j] = Math.max(f[i - 1][j], f[i - 1][((j - a[i]) % k + k) % k] + a[i]);
}
}
System.out.println(f[n][0]);
}
}