#include <stdio.h>
#include <stdlib.h>
#define N 200000
long long max(long long a, long long b) { return a > b ? a : b; }
int *ej[N], eo[N];
void append(int i, int j) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0)
ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
ej[i][o] = j;
}
int aa[N], c; long long dp[N], dq[N], sum;
void dfs1(int p, int i) {
int o, centroid;
dp[i] = aa[i], centroid = 1;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p) {
dfs1(i, j);
dp[i] += dp[j];
if (dp[j] * 2 > sum)
centroid = 0;
}
}
if ((sum - dp[i]) * 2 > sum)
centroid = 0;
if (centroid)
c = i;
}
void dfs2(int p, int i) {
int o;
dp[i] = aa[i], dq[i] = 0;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p) {
dfs2(i, j);
dp[i] += dp[j], dq[i] += dq[j];
}
}
dq[i] += aa[i] * dp[i];
}
int main() {
int n, i, j, o, b;
long long ans, sum1, sum2;
scanf("%d", &n);
for (i = 0; i < n; i++)
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
for (i = 0; i < n; i++)
scanf("%d", &aa[i]);
for (j = 1; j < n; j++) {
scanf("%d", &i), i--;
append(i, j), append(j, i);
}
sum = 0;
for (i = 0; i < n; i++)
sum += aa[i];
dfs1(-1, 0);
dfs2(-1, c);
ans = 0;
for (b = 0; b < 1 << eo[c]; b++) {
sum1 = sum2 = 0;
for (o = 0; o < eo[c]; o++) {
j = ej[c][o];
if ((b & 1 << o) == 0)
sum1 += dp[j];
else
sum2 += dp[j];
}
ans = max(ans, dq[c] + (long long) sum1 * sum2);
}
ans -= sum;
printf("%lld\n", ans);
return 0;
}
Compilation message
Main.c: In function 'append':
Main.c:13:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
13 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
Main.c: In function 'main':
Main.c:59:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
59 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
Main.c:63:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
63 | scanf("%d", &aa[i]);
| ^~~~~~~~~~~~~~~~~~~
Main.c:65:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
65 | scanf("%d", &i), i--;
| ^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
292 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 |
292 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
292 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
292 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
296 KB |
Output is correct |
8 |
Correct |
1 ms |
296 KB |
Output is correct |
9 |
Correct |
1 ms |
296 KB |
Output is correct |
10 |
Correct |
1 ms |
300 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
304 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
292 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
296 KB |
Output is correct |
8 |
Correct |
1 ms |
296 KB |
Output is correct |
9 |
Correct |
1 ms |
296 KB |
Output is correct |
10 |
Correct |
1 ms |
300 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
304 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
360 KB |
Output is correct |
18 |
Correct |
108 ms |
12424 KB |
Output is correct |
19 |
Correct |
100 ms |
12892 KB |
Output is correct |
20 |
Correct |
111 ms |
14336 KB |
Output is correct |
21 |
Correct |
102 ms |
14416 KB |
Output is correct |
22 |
Correct |
113 ms |
14392 KB |
Output is correct |
23 |
Correct |
113 ms |
14452 KB |
Output is correct |
24 |
Correct |
94 ms |
12096 KB |
Output is correct |
25 |
Correct |
72 ms |
12192 KB |
Output is correct |
26 |
Correct |
81 ms |
12188 KB |
Output is correct |
27 |
Correct |
80 ms |
12196 KB |
Output is correct |
28 |
Correct |
102 ms |
13716 KB |
Output is correct |
29 |
Correct |
119 ms |
15220 KB |
Output is correct |
30 |
Correct |
68 ms |
16064 KB |
Output is correct |
31 |
Correct |
73 ms |
15980 KB |
Output is correct |
32 |
Correct |
68 ms |
15948 KB |
Output is correct |
33 |
Correct |
71 ms |
16056 KB |
Output is correct |
34 |
Correct |
71 ms |
15948 KB |
Output is correct |
35 |
Correct |
70 ms |
16044 KB |
Output is correct |
36 |
Correct |
69 ms |
16028 KB |
Output is correct |
37 |
Correct |
69 ms |
16028 KB |
Output is correct |
38 |
Correct |
64 ms |
30792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
292 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 |
292 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
292 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
296 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
292 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
296 KB |
Output is correct |
26 |
Correct |
1 ms |
296 KB |
Output is correct |
27 |
Correct |
1 ms |
296 KB |
Output is correct |
28 |
Correct |
1 ms |
300 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
304 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
360 KB |
Output is correct |
36 |
Correct |
108 ms |
12424 KB |
Output is correct |
37 |
Correct |
100 ms |
12892 KB |
Output is correct |
38 |
Correct |
111 ms |
14336 KB |
Output is correct |
39 |
Correct |
102 ms |
14416 KB |
Output is correct |
40 |
Correct |
113 ms |
14392 KB |
Output is correct |
41 |
Correct |
113 ms |
14452 KB |
Output is correct |
42 |
Correct |
94 ms |
12096 KB |
Output is correct |
43 |
Correct |
72 ms |
12192 KB |
Output is correct |
44 |
Correct |
81 ms |
12188 KB |
Output is correct |
45 |
Correct |
80 ms |
12196 KB |
Output is correct |
46 |
Correct |
102 ms |
13716 KB |
Output is correct |
47 |
Correct |
119 ms |
15220 KB |
Output is correct |
48 |
Correct |
68 ms |
16064 KB |
Output is correct |
49 |
Correct |
73 ms |
15980 KB |
Output is correct |
50 |
Correct |
68 ms |
15948 KB |
Output is correct |
51 |
Correct |
71 ms |
16056 KB |
Output is correct |
52 |
Correct |
71 ms |
15948 KB |
Output is correct |
53 |
Correct |
70 ms |
16044 KB |
Output is correct |
54 |
Correct |
69 ms |
16028 KB |
Output is correct |
55 |
Correct |
69 ms |
16028 KB |
Output is correct |
56 |
Correct |
64 ms |
30792 KB |
Output is correct |
57 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
58 |
Halted |
0 ms |
0 KB |
- |