-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ1912FailSad.java
More file actions
35 lines (30 loc) · 1004 Bytes
/
BOJ1912FailSad.java
File metadata and controls
35 lines (30 loc) · 1004 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
package quki.algorithm.dp;
import java.util.Scanner;
public class BOJ1912FailSad {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a[] = new int[n + 1];
for (int i = 1; i <= n; i++) {
a[i] = sc.nextInt();
}
int d[][] = new int[2][n + 1];
int INI = a[1];
d[0][1] = INI;
d[1][1] = INI;
// 분기가 너무 나뉘는 좋지 않은 코드이다.
for (int i = 2; i <= n; i++) {
if (a[i - 1] < a[i - 1] + a[i]) {
if (a[i - 1] + a[i] < a[i]) {
d[0][i] = Math.max(d[0][i - 1], a[i]);
} else {
d[0][i] = Math.max(d[0][i - 1], a[i - 1] + a[i]);
}
} else {
d[0][i] = d[0][i - 1];
}
d[1][i] = Math.max(d[1][i - 1] + a[i], a[i]);
}
System.out.println(Math.max(d[0][n], d[1][n]));
}
}