-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path12865.java
30 lines (26 loc) · 972 Bytes
/
12865.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class NormalBag {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] input = br.readLine().split(" ");
int N = Integer.parseInt(input[0]);
int K = Integer.parseInt(input[1]);
int[] weights = new int[N + 1];
int[] values = new int[N + 1];
long[] dp = new long[K + 1];
for(int i = 0; i < N; i++) {
input = br.readLine().split(" ");
weights[i] = Integer.parseInt(input[0]);
values[i] = Integer.parseInt(input[1]);
}
br.close();
for(int i = 0; i < N; i++) {
for(int j = K; j - weights[i]>= 0; j--) {
dp[j] = Math.max(dp[j], dp[j - weights[i]] + values[i]);
}
}
System.out.println(dp[K]);
}
}