01背包问题(Java)
Java题解 1073ms
// 通过滚动数组优化
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int m = s.nextInt();
int[] dp = new int[1010];
for (int i = 1;i <= n;i++){
int v = s.nextInt();
int w = s.nextInt();
// 边输入边处理
// 逆序遍历
for (int j = m;j >= v;j--){
dp[j] = Math.max(dp[j],dp[j-v]+w);
}
}
s.close();
System.out.println(dp[m]);
}
}