#include <stdio.h>
#define N 200000
long long max(long long a, long long b) { return a > b ? a : b; }
long long aa[N + 1], bb[N + 1];
void update(int l, int r, int a, int b) {
aa[l] += a, bb[l] += b, aa[r + 1] -= a, bb[r + 1] -= b;
}
int main() {
static char cc[N * 2 + 1];
static int iiw[N * 2], iib[N * 2];
int n, nw, nb, i, iw, ib, iwl, iwr, ibl, ibr, l, r;
long long ans;
scanf("%d%s", &n, cc);
iwl = iwr = ib = 0;
iwl = iwr = ib = iwl;
nw = nb = 0;
for (i = 0; i < n * 2; i++)
if (cc[i] == 'W')
iiw[nw++] = i;
else
iib[nb++] = i;
for (i = 0; i < n; i++) {
iiw[n + i] = n * 2 + iiw[i];
iib[n + i] = n * 2 + iib[i];
}
for (iw = 0, ibl = 0, ibr = 0; iw < n; iw++) {
while (ibl < n * 2 && iib[ibl] < iiw[n + iw] - n)
ibl++;
while (ibr < n * 2 && iib[ibr] < iiw[n + iw])
ibr++;
if (ibl < ibr) {
l = (ibl - iw + n * 2) % n, r = (ibr - 1 - iw + n * 2) % n;
if (l <= r)
update(l, r, -1, r);
else
update(0, r, -1, r), update(l, n - 1, -1, n + r);
}
}
for (ib = 0, iwl = 0, iwr = 0; ib < n; ib++) {
while (iwl < n * 2 && iiw[iwl] <= iib[n + ib] - n)
iwl++;
while (iwr < n * 2 && iiw[iwr] < iib[n + ib])
iwr++;
if (iwl < iwr) {
l = (ib - iwr + 1 + n * 2) % n, r = (ib - iwl + n * 2) % n;
if (l <= r)
update(l, r, 1, -l);
else
update(l, n - 1, 1, -l), update(0, r, 1, n - l);
}
}
for (i = 1; i < n; i++)
aa[i] += aa[i - 1], bb[i] += bb[i - 1];
ans = 0;
for (i = 0; i < n; i++)
ans = max(ans, aa[i] * i + bb[i]);
printf("%lld\n", ans);
return 0;
}
Compilation message
monochrome.c: In function 'main':
monochrome.c:19:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
19 | scanf("%d%s", &n, cc);
| ^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
288 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
292 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
292 KB |
Output is correct |
29 |
Correct |
0 ms |
292 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
292 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
288 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
292 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
292 KB |
Output is correct |
29 |
Correct |
0 ms |
292 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
292 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
288 KB |
Output is correct |
34 |
Correct |
1 ms |
292 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
0 ms |
288 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
292 KB |
Output is correct |
43 |
Correct |
0 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
0 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
0 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
0 ms |
340 KB |
Output is correct |
51 |
Correct |
0 ms |
340 KB |
Output is correct |
52 |
Correct |
1 ms |
352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
288 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
292 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
292 KB |
Output is correct |
29 |
Correct |
0 ms |
292 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
292 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
288 KB |
Output is correct |
34 |
Correct |
1 ms |
292 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
0 ms |
288 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
292 KB |
Output is correct |
43 |
Correct |
0 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
0 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
0 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
0 ms |
340 KB |
Output is correct |
51 |
Correct |
0 ms |
340 KB |
Output is correct |
52 |
Correct |
1 ms |
352 KB |
Output is correct |
53 |
Correct |
18 ms |
7336 KB |
Output is correct |
54 |
Correct |
18 ms |
7316 KB |
Output is correct |
55 |
Correct |
18 ms |
7000 KB |
Output is correct |
56 |
Correct |
17 ms |
7016 KB |
Output is correct |
57 |
Correct |
10 ms |
7136 KB |
Output is correct |
58 |
Correct |
12 ms |
7308 KB |
Output is correct |
59 |
Correct |
10 ms |
6920 KB |
Output is correct |
60 |
Correct |
11 ms |
7012 KB |
Output is correct |
61 |
Correct |
10 ms |
7124 KB |
Output is correct |
62 |
Correct |
12 ms |
7104 KB |
Output is correct |
63 |
Correct |
12 ms |
6996 KB |
Output is correct |
64 |
Correct |
11 ms |
7064 KB |
Output is correct |
65 |
Correct |
12 ms |
6748 KB |
Output is correct |
66 |
Correct |
12 ms |
6976 KB |
Output is correct |
67 |
Correct |
12 ms |
6988 KB |
Output is correct |
68 |
Correct |
10 ms |
6624 KB |
Output is correct |
69 |
Correct |
11 ms |
7184 KB |
Output is correct |
70 |
Correct |
10 ms |
6652 KB |
Output is correct |
71 |
Correct |
12 ms |
7264 KB |
Output is correct |
72 |
Correct |
11 ms |
7336 KB |
Output is correct |
73 |
Correct |
11 ms |
7276 KB |
Output is correct |