-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
40 lines (31 loc) · 1.17 KB
/
Main.java
File metadata and controls
40 lines (31 loc) · 1.17 KB
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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int[] juice = new int[N];
int[] dp = new int[N];
for(int i=0; i<N; i++){
st = new StringTokenizer(br.readLine());
juice[i] = Integer.parseInt(st.nextToken());
}
if(N==1) {
System.out.println(juice[0]);
} else if(N==2) {
System.out.println(juice[0]+juice[1]);
} else {
dp[0] = juice[0];
dp[1] = juice[0]+juice[1];
dp[2] = Math.max(dp[1], Math.max(juice[0]+juice[2], juice[1]+juice[2]));
for(int i=3; i<N; i++){
dp[i] = Math.max(dp[i-3]+juice[i-1]+juice[i], Math.max(dp[i-2]+juice[i], dp[i-1]));
}
System.out.println(dp[N-1]);
}
br.close();
}
}