-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path_2156.java
More file actions
45 lines (36 loc) ยท 800 Bytes
/
_2156.java
File metadata and controls
45 lines (36 loc) ยท 800 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
39
40
41
42
43
44
45
package backjoon;
// https://www.acmicpc.net/problem/2156
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class _2156 {
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[1] = arr[1];
if (N > 1) {
dp[2] = arr[1] + arr[2];
}
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.println(dp[N]);
}
}
/*
input
6
6
10
13
9
8
1
output
33
*/