-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path_2579.java
More file actions
35 lines (25 loc) ยท 831 Bytes
/
_2579.java
File metadata and controls
35 lines (25 loc) ยท 831 Bytes
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
package backjoon;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class _2579 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
int[] DP = new int[N + 1];
int[] arr = new int[N + 1];
for (int i = 1; i <= N; i++) {
arr[i] = Integer.parseInt(br.readLine());
}
// index = 0 ์ ์์์ ์ด๋ค.
DP[1] = arr[1];
// N ์ด 1์ด ์
๋ ฅ๋ ์๋ ์๊ธฐ ๋๋ฌธ์ ์์ธ์ฒ๋ฆฌ๋ฅผ ํด์ค ํ์๊ฐ ์๋ค.
if (N >= 2) {
DP[2] = arr[1] + arr[2];
}
for (int i = 3; i <= N; i++) {
DP[i] = Math.max(DP[i - 2], DP[i - 3] + arr[i - 1]) + arr[i];
}
System.out.println(DP[N]);
}
}