반응형
문제
https://www.acmicpc.net/problem/12865
JAVA 코드
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int K = Integer.parseInt(st.nextToken());
int[] weights = new int[N + 1];
int[] values = new int[N + 1];
for (int idx = 1; idx <= N; idx++) {
st = new StringTokenizer(br.readLine());
weights[idx] = Integer.parseInt(st.nextToken());
values[idx] = Integer.parseInt(st.nextToken());
} // end of input
int[][] dp = new int[N + 1][K + 1]; // 개수, 무게
for (int idx = 1; idx <= N; idx++) {
for (int weight = 1; weight <= K; weight++) {
// 무게를 초과할 경우
if (weights[idx] > weight) {
dp[idx][weight] = dp[idx - 1][weight];
}
// 무게가 남는 경우
else {
dp[idx][weight] = Math.max(dp[idx - 1][weight], dp[idx - 1][weight - weights[idx]] + values[idx]);
}
}
}
bw.write(String.valueOf(dp[N][K]));
br.close();
bw.flush();
bw.close();
}
}
반응형
'Algorithm > 문제 풀이' 카테고리의 다른 글
[BAEKJOON_11726 - JAVA] 2×n 타일링 (0) | 2022.12.07 |
---|---|
[BAEKJOON_17615 - JAVA] 볼 모으기 (0) | 2022.12.07 |
[BAEKJOON_26042 - JAVA] 식당 입구 대기 줄 (0) | 2022.11.24 |
[BAEKJOON_26005 - JAVA] 나뭇잎 학회 (0) | 2022.11.23 |
[BAEKJOON_26043 - JAVA] 식당 메뉴 (0) | 2022.11.23 |