Skip to content

Commit 22b2d3b

Browse files
authored
Create [12]_2.cpp
1 parent fac671c commit 22b2d3b

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Solutions/[12]_2.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import java.util.Scanner;
2+
3+
public class Main {
4+
5+
public static void main(String[] args) {
6+
Scanner sc = new Scanner(System.in);
7+
8+
int n = sc.nextInt();
9+
int k = sc.nextInt();
10+
int[][] dp = new int[n + 1][k + 1];
11+
12+
for (int i = 1; i <= n; i++) {
13+
int weight = sc.nextInt();
14+
int value = sc.nextInt();
15+
for (int j = 1; j <= k; j++) {
16+
if (j < weight) {
17+
dp[i][j] = dp[i - 1][j];
18+
}
19+
else {
20+
dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - weight] + value);
21+
}
22+
}
23+
}
24+
25+
System.out.println(dp[n][k]);
26+
}
27+
28+
}

0 commit comments

Comments
 (0)