[DP] [백준 / 1535] 실버 2 - 안녕(java/자바)


package B_DP;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class boj_1535 {
public static void main(String[] args) throws IOException {
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(r.readLine());
int[] health = Arrays.stream(r.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
int[] happy = Arrays.stream(r.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
int[] dp = new int[101];
// 모든 사람에 대해 dp 배열 업데이트
for (int i = 0; i < N; i++) {
for (int j = 99; j >= 0; j--) { // 체력 100을 넘지 않도록 조정
if (j + health[i] < 100) { // 체력이 100을 넘지 않는 경우만 계산
dp[j + health[i]] = Math.max(dp[j + health[i]], dp[j] + happy[i]);
}
}
}
System.out.println(Arrays.stream(dp).max().getAsInt());
}
}