코드와이
[SW Expert Academy] 3282. 0/1 Knapsack 본문
DP(Knapsack)
문제링크
swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWBJAVpqrzQDFAWr
package D3;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class knapsack_01 {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
StringBuilder sb = new StringBuilder();
int T = Integer.parseInt(br.readLine());
for(int tc = 1 ; tc <= T ; tc++) {
sb.append("#").append(tc).append(" ");
st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
int[][] NK = new int[n+1][2];
for(int i = 1 ; i <= n; i++) {
st = new StringTokenizer(br.readLine());
NK[i][0] = Integer.parseInt(st.nextToken());
NK[i][1] = Integer.parseInt(st.nextToken());
}
int[][] dp = new int[n+1][k+1];
for(int i = 1 ; i <= n ; i++) {
int cur_v = NK[i][0];
int cur_c = NK[i][1];
for(int j = 1 ; j <= k ; j++) {
if(j >= cur_v) {
dp[i][j] = Math.max(cur_c + dp[i-1][j-cur_v], dp[i-1][j]);
} else {
dp[i][j] = dp[i-1][j];
}
}
}
sb.append(dp[n][k]).append("\n");
}
sb.setLength(sb.length()-1);
System.out.println(sb);
}
}
'SW_Expert' 카테고리의 다른 글
[SW Expert Academy] 8659. GCD (0) | 2021.03.31 |
---|---|
[SW Expert Academy] 4672. 수진이의 팰린드롬 (0) | 2021.03.31 |
[SW Expert Academy] 3307. 최장 증가 부분 수열 (0) | 2021.03.25 |
[SW Expert Academy] 1263. [S/W 문제해결 응용] 8일차 - 사람 네트워크2 (0) | 2021.03.25 |
[SW Expert Academy] 5550. 나는 개구리로소이다 (0) | 2021.03.24 |