朴素班三维dp
static int[][][] f = new int[N][4][M];
理论上192MB会MLE
import java.util.*;
public class Main {
static final int N = 100010, M = 1010;
static int[][][] f = new int[N][4][M];
static int[] a = new int[N];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), m = sc.nextInt();
sc.nextLine();
String[] s = sc.nextLine().split(" ");
for (int i = 1; i <= n; i++) a[i] = Integer.parseInt(s[i - 1]);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
if (j != 0) f[i][0][j] = -0x3f3f3f3f;
else f[i][0][j] = 0;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= 3; j++)
for (int k = 0; k < m; k++)
f[i][j][k] = Math.max(f[i - 1][j][k], f[i - 1][j - 1][((k - a[i]) % m + m) % m] + a[i]);
int ans = -1;
for (int i = 1; i <= n; i++)
if (ans < f[i][3][0]) ans = f[i][3][0];
System.out.println(ans);
}
}
10/12
import java.util.*;
public class Main {
static final int N = 1010;
static int[][][] f = new int[3 * N][4][N];
static ArrayList<Integer>[] list = new ArrayList[N];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), m = sc.nextInt();
sc.nextLine();
for (int i = 0; i < N; i++) list[i] = new ArrayList<>();
String[] s = sc.nextLine().split(" ");
for (int i = 1; i <= n; i++) {
int x = Integer.parseInt(s[i - 1]);
list[x % m].add(x);
}
for (int i = 0; i < 3 * N; i++)
Arrays.fill(f[i][0], -0x3f3f3f3f);
for (int i = 0; i <= 3 * m; i++) f[i][0][0] = 0;
int cnt = 0;
for (int i = 0; i < m; i++) {
list[i].sort(Comparator.reverseOrder());
for (int u = 0; u < Math.min(3, list[i].size()); u++) {
int x = list[i].get(u);
cnt++;
for (int j = 1; j <= 3; j++)
for (int k = 0; k < m; k++)
f[cnt][j][k] = Math.max(f[cnt - 1][j][k], f[cnt - 1][j - 1][((k - x) % m + m) % m] + x);
}
}
int ans = -1;
for (int i = 1; i <= 3 * m; i++)
if (ans < f[i][3][0]) ans = f[i][3][0];
System.out.println(ans);
}
}