第十三届JAVAB组决赛——6.背包与魔法
import java.io.*;
import java.util.*;
public class Main{
public static BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
public static PrintWriter stdOut = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
public static final int N = 2010, M = 10010;
public static int[] w = new int[N];
public static int[] v = new int[N];
public static int[][] f1 = new int[N][M]; //不使用魔法的情况
public static int[][] f2 = new int[N][M]; //使用魔法的情况
public static int n, m, K;
public static void main(String[] args) throws IOException{
String[] sp = stdIn.readLine().split(" ");
n = Integer.parseInt(sp[0]); m = Integer.parseInt(sp[1]); K = Integer.parseInt(sp[2]);
for(int i = 1; i <= n; i++) {
sp = stdIn.readLine().split(" ");
w[i] = Integer.parseInt(sp[0]); v[i] = Integer.parseInt(sp[1]);
}
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
//都不使用魔法
f1[i][j] = f1[i-1][j];
if(j >= w[i]) f1[i][j] = Math.max(f1[i][j], f1[i-1][j-w[i]]+v[i]);
//对i不使用魔法且前面使用了魔法
f2[i][j] = f2[i-1][j];
if(j >= w[i]) f2[i][j] = Math.max(f2[i][j], f2[i-1][j-w[i]]+v[i]);
//前面没使用魔法且对i使用了魔法
f2[i][j] = Math.max(f2[i][j], f1[i-1][j]);
if(j >= w[i] + K) f2[i][j] = Math.max(f2[i][j], f1[i-1][j-w[i]-K]+2*v[i]);
}
}
stdOut.println(Math.max(f1[n][m], f2[n][m])); stdOut.flush();
}
}