-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathp2156.java
More file actions
30 lines (26 loc) · 809 Bytes
/
p2156.java
File metadata and controls
30 lines (26 loc) · 809 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
import java.io.*;
public class p2156 {
static int N, arr[];
static Integer dp[];
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
arr = new int[N + 1];
dp = new Integer[N + 1];
for (int i = 1; i <= N; 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(find(N));
}
static int find(int n) {
if (dp[n] == null) {
dp[n] = Math.max(Math.max(find(n - 2), find(n - 3) + arr[n - 1]) + arr[n], find(n - 1));
}
return dp[n];
}
}