- Notifications
You must be signed in to change notification settings - Fork 846
/
Copy path3.java
35 lines (29 loc) · 1.15 KB
/
3.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
importjava.util.*;
publicclassMain {
staticintn; // 전체 상담 개수
staticint[] t = newint[15]; // 각 상담을 완료하는데 걸리는 기간
staticint[] p = newint[15]; // 각 상담을 완료했을 때 받을 수 있는 금액
staticint[] dp = newint[16]; // 다이나믹 프로그래밍을 위한 1차원 DP 테이블 초기화
staticintmaxValue;
publicstaticvoidmain(String[] args) {
Scannersc = newScanner(System.in);
n = sc.nextInt();
for (inti = 0; i < n; i++) {
t[i] = sc.nextInt();
p[i] = sc.nextInt();
}
// 배열을 뒤에서부터 거꾸로 확인
for (inti = n - 1; i >= 0; i--) {
inttime = t[i] + i;
// 상담이 기간 안에 끝나는 경우
if (time <= n) {
// 점화식에 맞게, 현재까지의 최고 이익 계산
dp[i] = Math.max(p[i] + dp[time], maxValue);
maxValue = dp[i];
}
// 상담이 기간을 벗어나는 경우
elsedp[i] = maxValue;
}
System.out.println(maxValue);
}
}