File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed
Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change 1+ import java .io .*;
2+ import java .util .*;
3+
4+ public class Main {
5+
6+ public static void main (String [] args ) throws IOException {
7+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
8+ StringTokenizer st = new StringTokenizer (br .readLine ());
9+ int N = Integer .parseInt (st .nextToken ());
10+ int K = Integer .parseInt (st .nextToken ());
11+ // d[i][무게] = i번째 보급품에서의 최대 가치
12+ int [][] d = new int [N +1 ][K +1 ];
13+ int [] w = new int [N +1 ];
14+ int [] v = new int [N +1 ];
15+ for (int i =1 ; i <=N ; i ++) {
16+ st = new StringTokenizer (br .readLine ());
17+ // 무게
18+ w [i ] = Integer .parseInt (st .nextToken ());
19+ // 가치
20+ v [i ] = Integer .parseInt (st .nextToken ());
21+ }
22+ for (int i =1 ; i <=N ; i ++) {
23+ for (int j =1 ; j <=K ; j ++) {
24+ if (j -w [i ] >= 0 ) {
25+ d [i ][j ] = Math .max (d [i -1 ][j ], d [i -1 ][j -w [i ]] + v [i ]);
26+ }else {
27+ d [i ][j ] = d [i -1 ][j ];
28+ }
29+ }
30+ }
31+ System .out .println (d [N ][K ]);
32+ }
33+ }
You can’t perform that action at this time.
0 commit comments