-
Notifications
You must be signed in to change notification settings - Fork 0
/
Backpack_dp.java
27 lines (26 loc) · 1.05 KB
/
Backpack_dp.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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Backpack_dp {
public static void main(String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int objectCount = Integer.parseInt(st.nextToken());
int fullWeight = Integer.parseInt(st.nextToken());
// index 0은 쓰지 않음.
int[] dp = new int[fullWeight+1];
for (int i=0; i<objectCount; i++){
StringTokenizer st2 = new StringTokenizer(br.readLine());
int weight = Integer.parseInt(st2.nextToken());
int value = Integer.parseInt(st2.nextToken());
if (weight <=fullWeight){
for (int j=fullWeight; j>=weight; j--){
dp[j] = Math.max(dp[j],dp[j-weight]+value);
}
}
}
System.out.println(dp[fullWeight]);
}
}