#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]; long long dp[N], dq[N];
void dfs(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) {
dfs(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);
}
ans = 0;
for (i = 0; i < n; i++) {
dfs(-1, i);
for (b = 0; b < 1 << eo[i]; b++) {
sum1 = sum2 = 0;
for (o = 0; o < eo[i]; o++) {
j = ej[i][o];
if ((b & 1 << o) == 0)
sum1 += dp[j];
else
sum2 += dp[j];
}
ans = max(ans, dq[i] + (long long) sum1 * sum2);
}
}
for (i = 0; i < n; i++)
ans -= aa[i];
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:39:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
39 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
Main.c:43:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
43 | scanf("%d", &aa[i]);
| ^~~~~~~~~~~~~~~~~~~
Main.c:45:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
45 | scanf("%d", &i), i--;
| ^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
0 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 |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
296 KB |
Output is correct |
10 |
Correct |
0 ms |
300 KB |
Output is correct |
11 |
Correct |
0 ms |
300 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
300 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
340 KB |
Output is correct |
2 |
Correct |
4 ms |
340 KB |
Output is correct |
3 |
Correct |
8 ms |
340 KB |
Output is correct |
4 |
Correct |
14 ms |
340 KB |
Output is correct |
5 |
Correct |
7 ms |
340 KB |
Output is correct |
6 |
Correct |
13 ms |
340 KB |
Output is correct |
7 |
Correct |
5 ms |
300 KB |
Output is correct |
8 |
Correct |
12 ms |
376 KB |
Output is correct |
9 |
Correct |
4 ms |
280 KB |
Output is correct |
10 |
Correct |
8 ms |
340 KB |
Output is correct |
11 |
Correct |
14 ms |
340 KB |
Output is correct |
12 |
Correct |
8 ms |
340 KB |
Output is correct |
13 |
Correct |
15 ms |
340 KB |
Output is correct |
14 |
Correct |
17 ms |
392 KB |
Output is correct |
15 |
Correct |
15 ms |
392 KB |
Output is correct |
16 |
Correct |
15 ms |
396 KB |
Output is correct |
17 |
Correct |
14 ms |
428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
340 KB |
Output is correct |
2 |
Correct |
4 ms |
340 KB |
Output is correct |
3 |
Correct |
8 ms |
340 KB |
Output is correct |
4 |
Correct |
14 ms |
340 KB |
Output is correct |
5 |
Correct |
7 ms |
340 KB |
Output is correct |
6 |
Correct |
13 ms |
340 KB |
Output is correct |
7 |
Correct |
5 ms |
300 KB |
Output is correct |
8 |
Correct |
12 ms |
376 KB |
Output is correct |
9 |
Correct |
4 ms |
280 KB |
Output is correct |
10 |
Correct |
8 ms |
340 KB |
Output is correct |
11 |
Correct |
14 ms |
340 KB |
Output is correct |
12 |
Correct |
8 ms |
340 KB |
Output is correct |
13 |
Correct |
15 ms |
340 KB |
Output is correct |
14 |
Correct |
17 ms |
392 KB |
Output is correct |
15 |
Correct |
15 ms |
392 KB |
Output is correct |
16 |
Correct |
15 ms |
396 KB |
Output is correct |
17 |
Correct |
14 ms |
428 KB |
Output is correct |
18 |
Execution timed out |
1044 ms |
14276 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 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 |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
296 KB |
Output is correct |
10 |
Correct |
0 ms |
300 KB |
Output is correct |
11 |
Correct |
0 ms |
300 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
300 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
10 ms |
340 KB |
Output is correct |
20 |
Correct |
4 ms |
340 KB |
Output is correct |
21 |
Correct |
8 ms |
340 KB |
Output is correct |
22 |
Correct |
14 ms |
340 KB |
Output is correct |
23 |
Correct |
7 ms |
340 KB |
Output is correct |
24 |
Correct |
13 ms |
340 KB |
Output is correct |
25 |
Correct |
5 ms |
300 KB |
Output is correct |
26 |
Correct |
12 ms |
376 KB |
Output is correct |
27 |
Correct |
4 ms |
280 KB |
Output is correct |
28 |
Correct |
8 ms |
340 KB |
Output is correct |
29 |
Correct |
14 ms |
340 KB |
Output is correct |
30 |
Correct |
8 ms |
340 KB |
Output is correct |
31 |
Correct |
15 ms |
340 KB |
Output is correct |
32 |
Correct |
17 ms |
392 KB |
Output is correct |
33 |
Correct |
15 ms |
392 KB |
Output is correct |
34 |
Correct |
15 ms |
396 KB |
Output is correct |
35 |
Correct |
14 ms |
428 KB |
Output is correct |
36 |
Execution timed out |
1044 ms |
14276 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |