-
Notifications
You must be signed in to change notification settings - Fork 0
/
28325.java
67 lines (51 loc) · 1.79 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
57
58
59
60
61
62
63
64
65
66
67
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
boolean[] visited = new boolean[N];
int[] empty = new int[N]; // 만약 절대 안들어가는 노드면 -1
long ans = 0;
int start = -1;
st = new StringTokenizer(br.readLine());
for (int i = 0; i < N; i++) {
long num = Long.parseLong(st.nextToken());
ans += num;
if (num != 0) {
empty[i] = -1;
visited[i] = true;
start = i;
}
}
if(start == -1){
start = 0;
}
for (int i = 0; i < N; i++) {
int idx = i + start >= N ? (i + start) % N : i + start;
if (!visited[idx]) {
visited[idx] = true;
int left = idx - 1;
int right = idx + 1;
if (idx == 0) {
left = N - 1;
} else if (idx == N - 1) {
right = 0;
}
if (empty[left] == -1 && empty[right] == -1) {
empty[idx] = 1;
ans++;
} else if (empty[left] == 1 || empty[right] == 1) {
} else {
empty[idx] = 1;
ans++;
}
//bw.write(i + " " + ans+" " + empty[left] + " " + empty[right] + "\n");
}
}
bw.write(ans + "\n");
bw.flush();
}
}