AcWing 3. 完全背包问题
原题链接
简单
作者:
ATK
,
2021-02-25 09:46:02
,
所有人可见
,
阅读 274
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int V = sc.nextInt();
int[] v = new int[N + 1];
int[] w = new int[N + 1];
for(int i = 1; i <= N; i++) {
v[i] = sc.nextInt();
w[i] = sc.nextInt();
}
int[][] f = new int[N + 1][V + 1];
for(int i = 1; i <= N; i++){
for(int j = 0; j <= V; j++){
f[i][j] = f[i - 1][j];
if(j >= v[i]){
for(int k = 1; k * v[i] <= j; k++){
f[i][j] = Math.max(f[i][j], f[i][j - k * v[i]] + k * w[i]);
}
}
}
}
System.out.println(f[N][V]);
}
}