#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 200000
#define M 36
#define K (1 << M / 2)
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, 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];
}
void brute(long long *ww, long long *ss, int m) {
static long long ss_[K];
int g1, g2, g, h;
ss[0] = 0;
for (h = 0; h < m; h++) {
g1 = 0, g2 = 0, g = 0;
while (g < (1 << h + 1))
ss_[g++] = g2 == (1 << h) || g1 < (1 << h) && ss[g1] < ss[g2] + ww[h] ? ss[g1++] : ss[g2++] + ww[h];
memcpy(ss, ss_, (1 << h + 1) * sizeof *ss_);
}
}
int main() {
static long long ww[M], ss1[K], ss2[K];
int n, m, m1, m2, g1, g2, h, i, j;
long long ans;
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);
}
for (i = 0; i < n; i++)
sum += aa[i];
dfs1(-1, 0);
dfs2(-1, c);
m = eo[c], m1 = m / 2, m2 = m - m1;
for (h = 0; h < m; h++) {
j = ej[c][h];
sum_ += ww[h] = dp[j];
}
brute(ww, ss1, m1);
brute(ww + m1, ss2, m2);
g1 = 0, g2 = (1 << m2) - 1, ans = 0;
while (g1 < 1 << m1 && g2 >= 0) {
ans = max(ans, dq[c] + (ss1[g1] + ss2[g2]) * (sum_ - (ss1[g1] + ss2[g2])) - sum);
if ((ss1[g1] + ss2[g2]) * 2 <= sum_)
g1++;
else
g2--;
}
printf("%lld\n", ans);
return 0;
}
Compilation message
Main.c: In function 'append':
Main.c:16:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
16 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
Main.c: In function 'brute':
Main.c:65:22: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
65 | while (g < (1 << h + 1))
| ~~^~~
Main.c:66:47: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
66 | ss_[g++] = g2 == (1 << h) || g1 < (1 << h) && ss[g1] < ss[g2] + ww[h] ? ss[g1++] : ss[g2++] + ww[h];
| ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
Main.c:67:27: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
67 | memcpy(ss, ss_, (1 << h + 1) * sizeof *ss_);
| ~~^~~
Main.c: In function 'main':
Main.c:76:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
76 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
Main.c:80:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
80 | scanf("%d", &aa[i]);
| ^~~~~~~~~~~~~~~~~~~
Main.c:82:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
82 | scanf("%d", &i), i--;
| ^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
0 ms |
296 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
296 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
296 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
300 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 |
340 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 |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
280 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 |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
428 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
300 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 |
340 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 |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
280 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 |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
428 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
109 ms |
12536 KB |
Output is correct |
19 |
Correct |
86 ms |
11452 KB |
Output is correct |
20 |
Correct |
119 ms |
12580 KB |
Output is correct |
21 |
Correct |
104 ms |
12708 KB |
Output is correct |
22 |
Correct |
106 ms |
12656 KB |
Output is correct |
23 |
Correct |
103 ms |
12716 KB |
Output is correct |
24 |
Correct |
92 ms |
10640 KB |
Output is correct |
25 |
Correct |
79 ms |
10700 KB |
Output is correct |
26 |
Correct |
89 ms |
10716 KB |
Output is correct |
27 |
Correct |
83 ms |
10708 KB |
Output is correct |
28 |
Correct |
130 ms |
12012 KB |
Output is correct |
29 |
Correct |
123 ms |
13248 KB |
Output is correct |
30 |
Correct |
56 ms |
14348 KB |
Output is correct |
31 |
Correct |
66 ms |
14300 KB |
Output is correct |
32 |
Correct |
76 ms |
14236 KB |
Output is correct |
33 |
Correct |
62 ms |
14236 KB |
Output is correct |
34 |
Correct |
57 ms |
14288 KB |
Output is correct |
35 |
Correct |
57 ms |
14284 KB |
Output is correct |
36 |
Correct |
57 ms |
14288 KB |
Output is correct |
37 |
Correct |
59 ms |
14248 KB |
Output is correct |
38 |
Correct |
63 ms |
28576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
6436 KB |
Output is correct |
2 |
Correct |
9 ms |
6440 KB |
Output is correct |
3 |
Correct |
9 ms |
6480 KB |
Output is correct |
4 |
Correct |
10 ms |
6480 KB |
Output is correct |
5 |
Correct |
10 ms |
6484 KB |
Output is correct |
6 |
Correct |
9 ms |
6484 KB |
Output is correct |
7 |
Correct |
10 ms |
6484 KB |
Output is correct |
8 |
Correct |
12 ms |
6440 KB |
Output is correct |
9 |
Correct |
10 ms |
6484 KB |
Output is correct |
10 |
Correct |
14 ms |
6484 KB |
Output is correct |
11 |
Correct |
11 ms |
6484 KB |
Output is correct |
12 |
Correct |
10 ms |
6356 KB |
Output is correct |
13 |
Correct |
9 ms |
6436 KB |
Output is correct |
14 |
Correct |
10 ms |
6484 KB |
Output is correct |
15 |
Correct |
10 ms |
6480 KB |
Output is correct |
16 |
Correct |
9 ms |
6484 KB |
Output is correct |
17 |
Correct |
9 ms |
6484 KB |
Output is correct |
18 |
Correct |
9 ms |
6476 KB |
Output is correct |
19 |
Correct |
10 ms |
6484 KB |
Output is correct |
20 |
Correct |
9 ms |
6484 KB |
Output is correct |
21 |
Correct |
10 ms |
6480 KB |
Output is correct |
22 |
Correct |
11 ms |
6440 KB |
Output is correct |
23 |
Correct |
12 ms |
6444 KB |
Output is correct |
24 |
Correct |
10 ms |
6356 KB |
Output is correct |
25 |
Correct |
10 ms |
6484 KB |
Output is correct |
26 |
Correct |
10 ms |
6484 KB |
Output is correct |
27 |
Correct |
9 ms |
6480 KB |
Output is correct |
28 |
Correct |
9 ms |
6484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
0 ms |
296 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
296 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
296 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
300 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 |
340 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 |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
2 ms |
280 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 |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
428 KB |
Output is correct |
35 |
Correct |
1 ms |
468 KB |
Output is correct |
36 |
Correct |
109 ms |
12536 KB |
Output is correct |
37 |
Correct |
86 ms |
11452 KB |
Output is correct |
38 |
Correct |
119 ms |
12580 KB |
Output is correct |
39 |
Correct |
104 ms |
12708 KB |
Output is correct |
40 |
Correct |
106 ms |
12656 KB |
Output is correct |
41 |
Correct |
103 ms |
12716 KB |
Output is correct |
42 |
Correct |
92 ms |
10640 KB |
Output is correct |
43 |
Correct |
79 ms |
10700 KB |
Output is correct |
44 |
Correct |
89 ms |
10716 KB |
Output is correct |
45 |
Correct |
83 ms |
10708 KB |
Output is correct |
46 |
Correct |
130 ms |
12012 KB |
Output is correct |
47 |
Correct |
123 ms |
13248 KB |
Output is correct |
48 |
Correct |
56 ms |
14348 KB |
Output is correct |
49 |
Correct |
66 ms |
14300 KB |
Output is correct |
50 |
Correct |
76 ms |
14236 KB |
Output is correct |
51 |
Correct |
62 ms |
14236 KB |
Output is correct |
52 |
Correct |
57 ms |
14288 KB |
Output is correct |
53 |
Correct |
57 ms |
14284 KB |
Output is correct |
54 |
Correct |
57 ms |
14288 KB |
Output is correct |
55 |
Correct |
59 ms |
14248 KB |
Output is correct |
56 |
Correct |
63 ms |
28576 KB |
Output is correct |
57 |
Correct |
9 ms |
6436 KB |
Output is correct |
58 |
Correct |
9 ms |
6440 KB |
Output is correct |
59 |
Correct |
9 ms |
6480 KB |
Output is correct |
60 |
Correct |
10 ms |
6480 KB |
Output is correct |
61 |
Correct |
10 ms |
6484 KB |
Output is correct |
62 |
Correct |
9 ms |
6484 KB |
Output is correct |
63 |
Correct |
10 ms |
6484 KB |
Output is correct |
64 |
Correct |
12 ms |
6440 KB |
Output is correct |
65 |
Correct |
10 ms |
6484 KB |
Output is correct |
66 |
Correct |
14 ms |
6484 KB |
Output is correct |
67 |
Correct |
11 ms |
6484 KB |
Output is correct |
68 |
Correct |
10 ms |
6356 KB |
Output is correct |
69 |
Correct |
9 ms |
6436 KB |
Output is correct |
70 |
Correct |
10 ms |
6484 KB |
Output is correct |
71 |
Correct |
10 ms |
6480 KB |
Output is correct |
72 |
Correct |
9 ms |
6484 KB |
Output is correct |
73 |
Correct |
9 ms |
6484 KB |
Output is correct |
74 |
Correct |
9 ms |
6476 KB |
Output is correct |
75 |
Correct |
10 ms |
6484 KB |
Output is correct |
76 |
Correct |
9 ms |
6484 KB |
Output is correct |
77 |
Correct |
10 ms |
6480 KB |
Output is correct |
78 |
Correct |
11 ms |
6440 KB |
Output is correct |
79 |
Correct |
12 ms |
6444 KB |
Output is correct |
80 |
Correct |
10 ms |
6356 KB |
Output is correct |
81 |
Correct |
10 ms |
6484 KB |
Output is correct |
82 |
Correct |
10 ms |
6484 KB |
Output is correct |
83 |
Correct |
9 ms |
6480 KB |
Output is correct |
84 |
Correct |
9 ms |
6484 KB |
Output is correct |
85 |
Correct |
110 ms |
14372 KB |
Output is correct |
86 |
Correct |
99 ms |
12996 KB |
Output is correct |
87 |
Correct |
106 ms |
14500 KB |
Output is correct |
88 |
Correct |
129 ms |
14424 KB |
Output is correct |
89 |
Correct |
106 ms |
14460 KB |
Output is correct |
90 |
Correct |
114 ms |
14520 KB |
Output is correct |
91 |
Correct |
96 ms |
12164 KB |
Output is correct |
92 |
Correct |
91 ms |
12152 KB |
Output is correct |
93 |
Correct |
100 ms |
12212 KB |
Output is correct |
94 |
Correct |
85 ms |
12204 KB |
Output is correct |
95 |
Correct |
110 ms |
13604 KB |
Output is correct |
96 |
Correct |
114 ms |
15128 KB |
Output is correct |
97 |
Correct |
72 ms |
22196 KB |
Output is correct |
98 |
Correct |
70 ms |
22148 KB |
Output is correct |
99 |
Correct |
71 ms |
22124 KB |
Output is correct |
100 |
Correct |
73 ms |
22188 KB |
Output is correct |
101 |
Correct |
73 ms |
22208 KB |
Output is correct |
102 |
Correct |
76 ms |
22212 KB |
Output is correct |
103 |
Correct |
70 ms |
22216 KB |
Output is correct |
104 |
Correct |
73 ms |
22180 KB |
Output is correct |
105 |
Correct |
69 ms |
22104 KB |
Output is correct |
106 |
Correct |
72 ms |
22164 KB |
Output is correct |
107 |
Correct |
68 ms |
22180 KB |
Output is correct |
108 |
Correct |
69 ms |
22096 KB |
Output is correct |
109 |
Correct |
70 ms |
22188 KB |
Output is correct |
110 |
Correct |
66 ms |
22160 KB |
Output is correct |
111 |
Correct |
65 ms |
22212 KB |
Output is correct |
112 |
Correct |
64 ms |
22224 KB |
Output is correct |
113 |
Correct |
68 ms |
30828 KB |
Output is correct |