-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain1077_배낭채우기1_유승아.java
53 lines (43 loc) · 1.21 KB
/
Main1077_배낭채우기1_유승아.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
43
44
45
46
47
48
49
50
51
52
53
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
//JUNGOL :: 1077 배낭채우기
//2019-03-25
public class Main1077_배낭채우기1_유승아 {
static class val{
int w;
int v;
public val(int w, int v) {
super();
this.w = w;
this.v = v;
}
}
private static int W;
private static int N;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
N = Integer.parseInt(st.nextToken()); // 보석의 가지 수
W = Integer.parseInt(st.nextToken()); // 배낭의 용량
val[] arr = new val[N];
for(int i=0;i<N;i++) {
st = new StringTokenizer(br.readLine(), " ");
int w = Integer.parseInt(st.nextToken());
int v = Integer.parseInt(st.nextToken());
arr[i] = new val(w,v);
}
///////input
///////dp
int[] values = new int[W+1];
//i는 보석 종류
for(int i=0;i<N;i++) {
//j는 보석 무게
//내 무게보다 작은 배낭은 이전에 구한 값을 참고한다.
for(int j=arr[i].w;j<=W;j++) {
values[j] = Math.max(values[j],values[j-arr[i].w]+arr[i].v);
}
}
System.out.println(values[W]);
}//end of main
}