-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboj2156.java
More file actions
38 lines (28 loc) · 903 Bytes
/
boj2156.java
File metadata and controls
38 lines (28 loc) · 903 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
36
37
38
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class boj2156 {
static int[] arr;
static Integer[] dp;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
arr = new int[n + 1];
dp = new Integer[n + 1];
for (int i = 1; i < n + 1; i++) {
arr[i] = Integer.parseInt(br.readLine());
}
dp[0] = 0;
dp[1] = arr[1];
if (n > 1) {
dp[2] = arr[1] + arr[2];
}
System.out.println(recur(n));
}
private static int recur(int n) {
if (dp[n] == null) {
dp[n] = Math.max(Math.max(recur(n - 2), recur(n - 3) + arr[n - 1]) + arr[n], recur(n - 1));
}
return dp[n];
}
}