AcWing 3. 完全背包问题
原题链接
简单
作者:
ATK
,
2021-02-25 09:48:11
,
所有人可见
,
阅读 267
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[V + 1];
for(int i = 1; i <= N; i++){
for(int j = V; j >= v[i]; j--){
for(int k = 1; k * v[i] <= j; k++){
f[j] = Math.max(f[j], f[j - k * v[i]] + k * w[i]);
}
}
}
System.out.println(f[V]);
}
}