-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcbarn2.java
90 lines (71 loc) · 2.16 KB
/
cbarn2.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import java.io.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.StringTokenizer;
public class cbarn2 {
public static void main(String[] args) throws IOException {
// write your code here
BufferedReader f = new BufferedReader(new FileReader("cbarn.in"));
// input file name goes above
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("cbarn.out")));
int n = Integer.parseInt(f.readLine());
int[] s = new int[n];
HashSet<Integer> zeros = new HashSet<Integer>();
for(int i = 0; i<n;i++){
s[i] = Integer.parseInt(f.readLine());
if(s[i]==0){
zeros.add(i);
}
}
int num = 0;
outerloop:
for(int end:zeros){
int prev = 0;
for(int i = 1; i<n+1;i++){
if(s[(end+i)%s.length] > 1){
prev+=s[(end+i)%s.length]-1;
}
else if (s[(end+i)%s.length] < 1){
if(prev>0) {
prev--;
}
else{
continue outerloop;
}
}
}
num = end;
break;
}
System.out.println(num);
int[] dup = new int[n];
for(int i = 0; i<s.length;i++){
dup[(i+n-num-1)%n]=s[i];
}
long count = 0;
for(int j = n-1;j>0;j--){
if(dup[j]==0){
int cur = j-1;
while(dup[cur]==0){
s[cur]=1;
cur--;
}
int counts = dup[cur];
//end is j
//System.out.println(cur);
for(int i = 1; i<=counts;i++){
count+=(long)(i+j-counts-cur)*(i+j-counts-cur);
dup[i+j-counts] = 1;
}
if(counts<=j-cur) {
dup[cur] = 0;
}
else{
dup[cur]=1;
}
}
}
out.println(count);
out.close();
}
}