AcWing 3. 完全背包问题
原题链接
简单
import java.util.Scanner;
import java.lang.Math;
public class Main {
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[][] f = 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++){
f[i][j] = Math.max(f[i][j],f[i-1][j-v[i]*k]+w[i]*k);
}
}
}
System.out.println(f[n][m]);
}
}