#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 200000
#define M 200000
#define N_ (N + M - 1)
int max(int a, int b) { return a > b ? a : b; }
int *el[N_], *ea[N_], eo[N_];
void append(int r, int l, int a) {
int o = eo[r]++;
if (o >= 2 && (o & o - 1) == 0) {
el[r] = (int *) realloc(el[r], o * 2 * sizeof *el[r]);
ea[r] = (int *) realloc(ea[r], o * 2 * sizeof *ea[r]);
}
el[r][o] = l, ea[r][o] = a;
}
int aa[N_], bb[N_];
int ds[N_], rr[N_]; char marked[N_]; int n, m, n_;
int find(int i) {
return ds[i] < 0 ? i : (ds[i] = find(ds[i]));
}
void join(int i, int j) {
i = find(i);
j = find(j);
if (i == j)
return;
if (ds[i] > ds[j])
ds[i] = j, rr[j] = max(rr[j], rr[i]);
else {
if (ds[i] == ds[j])
ds[i]--;
ds[j] = i, rr[i] = max(rr[i], rr[j]);
}
}
void mark(int i) {
marked[i] = 1;
if (i >= 2 && marked[i - 2])
join(i - 2, i);
if (i + 2 < n_ && marked[i + 2])
join(i, i + 2);
}
int nxt(int i) {
return !marked[i] ? i : rr[find(i)] + 2;
}
int main() {
int h, l, r, a, o;
long long ans;
scanf("%d%d", &n, &m), n_ = n + m - 1;
for (h = 0; h < n_; h++)
scanf("%d", &aa[h]);
for (h = 0; h < n_; h++)
scanf("%d", &bb[h]);
for (r = 0; r < n_; r++) {
el[r] = (int *) malloc(2 * sizeof *el[r]);
ea[r] = (int *) malloc(2 * sizeof *ea[r]);
}
for (h = 0; h < n_; h++) {
l = h < m ? m - 1 - h : h - m + 1, r = h < n ? m - 1 + h : n_ - 1 + n - 1 - h;
append(r, l, aa[h]);
}
memset(ds, -1, n_ * sizeof *ds);
for (h = 0; h < n_; h++)
rr[h] = h;
ans = 0;
for (r = 0; r < n_; r++)
for (o = eo[r]; o--; ) {
l = el[r][o], a = ea[r][o];
h = l;
while ((h = nxt(h)) <= r)
if (a >= bb[h])
a -= bb[h], ans += bb[h], bb[h] = 0, mark(h);
else {
bb[h] -= a, ans += a;
break;
}
}
printf("%lld\n", ans);
return 0;
}
Compilation message
colouring.c: In function 'append':
colouring.c:16:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
16 | if (o >= 2 && (o & o - 1) == 0) {
| ~~^~~
colouring.c: In function 'main':
colouring.c:60:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
60 | scanf("%d%d", &n, &m), n_ = n + m - 1;
| ^~~~~~~~~~~~~~~~~~~~~
colouring.c:62:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
62 | scanf("%d", &aa[h]);
| ^~~~~~~~~~~~~~~~~~~
colouring.c:64:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
64 | scanf("%d", &bb[h]);
| ^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
296 KB |
Output is correct |
3 |
Correct |
0 ms |
296 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
300 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
296 KB |
Output is correct |
3 |
Correct |
0 ms |
296 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
300 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
296 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
292 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
296 KB |
Output is correct |
19 |
Correct |
1 ms |
296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
296 KB |
Output is correct |
3 |
Correct |
0 ms |
296 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
300 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
296 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
292 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
296 KB |
Output is correct |
19 |
Correct |
1 ms |
296 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
300 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
296 KB |
Output is correct |
29 |
Correct |
0 ms |
300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
296 KB |
Output is correct |
3 |
Correct |
0 ms |
296 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
300 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
296 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
292 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
296 KB |
Output is correct |
19 |
Correct |
1 ms |
296 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
300 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
296 KB |
Output is correct |
29 |
Correct |
0 ms |
300 KB |
Output is correct |
30 |
Correct |
2 ms |
724 KB |
Output is correct |
31 |
Correct |
2 ms |
724 KB |
Output is correct |
32 |
Correct |
2 ms |
724 KB |
Output is correct |
33 |
Correct |
2 ms |
596 KB |
Output is correct |
34 |
Correct |
2 ms |
620 KB |
Output is correct |
35 |
Correct |
1 ms |
564 KB |
Output is correct |
36 |
Correct |
1 ms |
568 KB |
Output is correct |
37 |
Correct |
1 ms |
560 KB |
Output is correct |
38 |
Correct |
2 ms |
468 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
1 ms |
476 KB |
Output is correct |
41 |
Correct |
1 ms |
596 KB |
Output is correct |
42 |
Correct |
1 ms |
472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
23864 KB |
Output is correct |
2 |
Correct |
73 ms |
23884 KB |
Output is correct |
3 |
Correct |
67 ms |
23864 KB |
Output is correct |
4 |
Correct |
74 ms |
24188 KB |
Output is correct |
5 |
Correct |
70 ms |
24272 KB |
Output is correct |
6 |
Correct |
70 ms |
23368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
141 ms |
47600 KB |
Output is correct |
2 |
Correct |
138 ms |
46752 KB |
Output is correct |
3 |
Correct |
155 ms |
46680 KB |
Output is correct |
4 |
Correct |
143 ms |
46952 KB |
Output is correct |
5 |
Correct |
135 ms |
47032 KB |
Output is correct |
6 |
Correct |
122 ms |
44080 KB |
Output is correct |
7 |
Correct |
143 ms |
46316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
296 KB |
Output is correct |
3 |
Correct |
0 ms |
296 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
300 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
296 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
292 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
296 KB |
Output is correct |
19 |
Correct |
1 ms |
296 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
300 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
296 KB |
Output is correct |
29 |
Correct |
0 ms |
300 KB |
Output is correct |
30 |
Correct |
2 ms |
724 KB |
Output is correct |
31 |
Correct |
2 ms |
724 KB |
Output is correct |
32 |
Correct |
2 ms |
724 KB |
Output is correct |
33 |
Correct |
2 ms |
596 KB |
Output is correct |
34 |
Correct |
2 ms |
620 KB |
Output is correct |
35 |
Correct |
1 ms |
564 KB |
Output is correct |
36 |
Correct |
1 ms |
568 KB |
Output is correct |
37 |
Correct |
1 ms |
560 KB |
Output is correct |
38 |
Correct |
2 ms |
468 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
1 ms |
476 KB |
Output is correct |
41 |
Correct |
1 ms |
596 KB |
Output is correct |
42 |
Correct |
1 ms |
472 KB |
Output is correct |
43 |
Correct |
72 ms |
23864 KB |
Output is correct |
44 |
Correct |
73 ms |
23884 KB |
Output is correct |
45 |
Correct |
67 ms |
23864 KB |
Output is correct |
46 |
Correct |
74 ms |
24188 KB |
Output is correct |
47 |
Correct |
70 ms |
24272 KB |
Output is correct |
48 |
Correct |
70 ms |
23368 KB |
Output is correct |
49 |
Correct |
141 ms |
47600 KB |
Output is correct |
50 |
Correct |
138 ms |
46752 KB |
Output is correct |
51 |
Correct |
155 ms |
46680 KB |
Output is correct |
52 |
Correct |
143 ms |
46952 KB |
Output is correct |
53 |
Correct |
135 ms |
47032 KB |
Output is correct |
54 |
Correct |
122 ms |
44080 KB |
Output is correct |
55 |
Correct |
143 ms |
46316 KB |
Output is correct |
56 |
Correct |
78 ms |
23316 KB |
Output is correct |
57 |
Correct |
84 ms |
23592 KB |
Output is correct |
58 |
Correct |
69 ms |
23884 KB |
Output is correct |
59 |
Correct |
62 ms |
22528 KB |
Output is correct |
60 |
Correct |
73 ms |
24012 KB |
Output is correct |
61 |
Correct |
71 ms |
23832 KB |
Output is correct |
62 |
Correct |
82 ms |
22604 KB |
Output is correct |
63 |
Correct |
71 ms |
23908 KB |
Output is correct |
64 |
Correct |
71 ms |
24132 KB |
Output is correct |
65 |
Correct |
79 ms |
25076 KB |
Output is correct |
66 |
Correct |
95 ms |
27636 KB |
Output is correct |
67 |
Correct |
95 ms |
30300 KB |
Output is correct |
68 |
Correct |
124 ms |
44068 KB |
Output is correct |
69 |
Correct |
145 ms |
45756 KB |
Output is correct |
70 |
Correct |
126 ms |
35336 KB |
Output is correct |