티스토리 뷰
https://www.acmicpc.net/problem/12865
코드
import java.io.*;
import java.util.*;
public class Main {
static int n, k;
static int[][] dp;
static List<Bag> bags = new ArrayList<>();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
k = Integer.parseInt(st.nextToken());
dp = new int[n + 1][k + 1];
for (int i = 1; i <= n; i++) {
st = new StringTokenizer(br.readLine());
bags.add(new Bag(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken())));
}
System.out.println(solution());
}
static int solution() {
for (int i = 1; i <= n; i++) {
Bag bag = bags.get(i - 1);
for (int j = 1; j <= k; j++) {
dp[i][j] = dp[i - 1][j];
if (j - bag.w >= 0) {
dp[i][j] = Math.max(dp[i][j], bag.v + dp[i - 1][j - bag.w]);
}
}
}
return dp[n][k];
}
static class Bag {
int w, v;
public Bag(int w, int v) {
this.w = w;
this.v = v;
}
}
}