-
Notifications
You must be signed in to change notification settings - Fork 0
/
28325.java
56 lines (48 loc) · 1.26 KB
/
28325.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
long []ants = new long[N];
String []input = br.readLine().split(" ");
for (int i = 0; i < N; i++) {
ants[i] = Long.parseLong(input[i]);
}
boolean []visited = new boolean[N];
long answer1 = ants[0];
if (ants[0] == 0) {
visited[0] = true;
answer1 = 1;
}
for (int i = 1; i < N; i++) {
if (ants[i] > 0) {
answer1 += ants[i];
} else if (ants[i] == 0 && !visited[getIndex(N, i, 0)] && !visited[getIndex(N, i, 1)]) {
visited[i] = true;
answer1++;
}
}
long answer2 = 0;
visited = new boolean[N];
for (int i = 1; i < N; i++) {
if (ants[i] > 0) {
answer2 += ants[i];
} else if (ants[i] == 0 && !visited[getIndex(N, i, 0)] && !visited[getIndex(N, i, 1)]) {
visited[i] = true;
answer2++;
}
}
System.out.println(Math.max(answer2, answer1));
}
public static int getIndex(int N, int index, int direction) {
if (direction == 0) {
if (index == 0)
return N -1;
return index - 1;
}
if (index == N -1)
return 0;
return index + 1;
}
}