-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path241109-2156.java
32 lines (29 loc) · 959 Bytes
/
241109-2156.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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int[] arr = new int[n + 1];
int[] dp = new int[n + 1];
for (int i = 1; i <= n; i++) {
arr[i] = Integer.parseInt(br.readLine());
}
dp[0] = arr[0];
dp[1] = arr[1];
if (n == 1) {
System.out.print(dp[1]);
return;
}
dp[2] = arr[1] + arr[2];
if (n == 2) {
System.out.print(dp[2]);
return;
}
for (int i = 3; i <= n; i++) {
dp[i] = Math.max(dp[i - 1], Math.max(dp[i - 2] + arr[i], dp[i - 3] + arr[i - 1] + arr[i]));
}
System.out.print(Math.max(dp[n - 1], dp[n]));
}
}