-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBag.java
42 lines (32 loc) · 1.21 KB
/
Bag.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
31
32
33
34
35
36
37
38
39
40
41
42
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Bag {
static int[][] arr;
static int[] w;
static int[] feel;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int quantity = Integer.parseInt(st.nextToken());
int weight = Integer.parseInt(st.nextToken());
arr = new int[quantity + 1][weight + 1];
w = new int[weight + 1];
feel = new int[quantity + 1];
for(int i = 1; i <= quantity; i++) {
st = new StringTokenizer(br.readLine(), " ");
w[i] = Integer.parseInt(st.nextToken());
feel[i] = Integer.parseInt(st.nextToken());
}
for(int i = 1; i <= quantity; i++) {
for(int j = 1; j <= weight; j++) {
arr[i][j] = arr[i-1][j];
if(j >= w[i]) {
arr[i][j] = Math.max(arr[i][j], arr[i-1][j - w[i]] + feel[i]);
}
}
}
System.out.println(arr[quantity][weight]);
}
}