AcWing 9. [Java 一维数组] 分组背包问题
原题链接
中等
作者:
Aranne
,
2020-07-07 11:24:31
,
所有人可见
,
阅读 1011
import java.util.*;
class Main {
Scanner sc = new Scanner(System.in);
int maxV = 105;
int maxN = 105;
int N, M, V;
int[] dp = new int[maxV];
int[] v = new int[maxN];
int[] w = new int[maxN];
void run() {
N = sc.nextInt(); V = sc.nextInt();
for (int i = 0; i < N; i++) {
M = sc.nextInt();
for (int j = 0; j < M; j++) {
v[j] = sc.nextInt();
w[j] = sc.nextInt();
}
for (int j = V; j >= 0; j--) {
for (int k = 0; k < M; k++) {
if (j >= v[k]) dp[j] = Math.max(dp[j], dp[j - v[k]] + w[k]);
}
}
}
System.out.println(dp[V]);
}
public static void main(String[] args) {
Main m = new Main();
m.run();
}
}