import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * @author fishcanfly */ public class Main { /** * main入口由OJ平台调用 */ static long[][] lcs = null; static int n, k, d; static long[] arr; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] words = br.readLine().split("\\s+"); n = Integer.valueOf(words[0]); k = Integer.valueOf(words[1]); d = Integer.valueOf(words[2]); arr = new long[n]; long[][][] dp = new long[n + 1][k + 1][d + 1]; for (int i = 0; i <= n; i++) { for (int j = 0; j <= k; j++) { for (int x = 0; x <= d; x++) { dp[i][j][x] = -1; } } } words = br.readLine().split("\\s+"); for (int i = 0; i < n; i++) { arr[i] = Long.valueOf(words[i]); } dp[0][0][0] = 0; for (int i = 0; i < n; i++) { for (int j = 0; j <= k; j++) { for (int m = 0; m < d; m++) { if (dp[i][j][m] == -1) continue; // transition when a_i isn't chosen dp[i + 1][j][m] = Math.max(dp[i + 1][j][m], dp[i][j][m]); // transition when a_i is chosen if( j+1<=k){ dp[i + 1][j + 1][(m + (int) arr[i])% d] = Math.max(dp[i + 1][j + 1][(m + (int) arr[i])% d], dp[i][j][m] + arr[i]); } } } } System.out.println(dp[n][k][0]); br.close(); } }
标签:atcoder,io,valueOf,long,static,words,new,动态,abc281 From: https://www.cnblogs.com/fishcanfly/p/16973813.html