AcWing 3. 完全背包问题
原题链接
简单
import java.util.Scanner;
import java.lang.Math;
public class Main{
private static final int N = 1010;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[] v = new int[N];
int[] w = new int[N];
for(int i=1;i<=n;i++){
v[i] = sc.nextInt();
w[i] = sc.nextInt();
}
int[][] dp = new int[N][N];
for(int i=1;i<=n;i++){
for(int j=0;j<=m;j++){
for(int k=0;k*v[i]<=j;k++){
dp[i][j] = Math.max(dp[i][j],dp[i][j-k*v[i]]+k*w[i]);
}
}
}
System.out.println(dp[n][m]);
}
}