#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#define int long long
using namespace std;
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> a(n);
for(int i = 0; i < n; ++i){
cin >> a[i];
}
vector<vector<int>> way(n);
for(int i = 1; i < n; ++i){
int x;
cin >> x;
--x;
way[x].push_back(i);
way[i].push_back(x);
}
int ans = 0;
auto sol = [&](int rt){
int allsum = 0, cnt = 0;
auto dfs = [&](auto&&self, int x, int pr, int psum)->void{
allsum += a[x] * psum;
cnt += a[x];
for(auto&nxt:way[x]){
if(nxt == pr){
continue;
}
self(self, nxt, x, psum + a[x]);
}
};
vector<int> vc;
for(int i = 0; i < (int)way[rt].size(); ++i){
cnt = 0;
dfs(dfs, way[rt][i], rt, a[rt]);
vc.push_back(cnt);
}
vector<int> l, r;
int lsz = (int)vc.size() / 2;
int rsz = (int)vc.size() - lsz;
for(int i = 0; i < (1 << lsz); ++i){
int sum = 0;
for(int j = 0; j < lsz; ++j){
if(i & (1 << j)){
sum += vc[j];
}
}
l.push_back(sum);
}
for(int i = 0; i < (1 << rsz); ++i){
int sum = 0;
for(int j = 0; j < rsz; ++j){
if(i & (1 << j)){
sum += vc[j + lsz];
}
}
r.push_back(sum);
}
sort(l.begin(), l.end());
sort(r.begin(), r.end());
int allvc = accumulate(vc.begin(), vc.end(), 0ll);
int mx = 0;
for(int i = 0, j = (int)r.size() - 1; i < (int)l.size(); ++i){
while(j && (l[i] + r[j]) * (allvc - l[i] - r[j]) <= (l[i] + r[j - 1]) * (allvc - l[i] - r[j - 1])){
--j;
}
mx = max(mx, (l[i] + r[j]) * (allvc - l[i] - r[j]));
}
ans = max(ans, allsum + mx);
};
vector<int> sz(n);
auto dfssz = [&](auto&&self, int x, int pr)->void{
sz[x] = a[x];
for(auto&nxt:way[x]){
if(nxt == pr){
continue;
}
self(self, nxt, x);
sz[x] += sz[nxt];
}
};
dfssz(dfssz, 0, -1);
auto getcent = [&](auto&&self, int x, int pr)->int{
for(auto&nxt:way[x]){
if(nxt == pr){
continue;
}
if(sz[nxt] * 2 > sz[0]){
return self(self, nxt, x);
}
}
return x;
};
int ct = getcent(getcent, 0, -1);
sol(ct);
// for(int i = 0; i < n; ++i){
// sol(i);
// }
for(int i = 0; i < n; ++i){
ans += a[i] * (a[i] - 1);
}
cout << ans << '\n';
return 0;
}
# |
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 |
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 |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 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 |
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 |
0 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 |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 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 |
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 |
0 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 |
340 KB |
Output is correct |
18 |
Correct |
70 ms |
14600 KB |
Output is correct |
19 |
Correct |
66 ms |
14972 KB |
Output is correct |
20 |
Correct |
67 ms |
16624 KB |
Output is correct |
21 |
Correct |
62 ms |
16620 KB |
Output is correct |
22 |
Correct |
83 ms |
16660 KB |
Output is correct |
23 |
Correct |
67 ms |
16732 KB |
Output is correct |
24 |
Correct |
55 ms |
14020 KB |
Output is correct |
25 |
Correct |
48 ms |
14000 KB |
Output is correct |
26 |
Correct |
46 ms |
13960 KB |
Output is correct |
27 |
Correct |
66 ms |
14100 KB |
Output is correct |
28 |
Correct |
57 ms |
15644 KB |
Output is correct |
29 |
Correct |
92 ms |
17424 KB |
Output is correct |
30 |
Correct |
37 ms |
19020 KB |
Output is correct |
31 |
Correct |
38 ms |
19032 KB |
Output is correct |
32 |
Correct |
44 ms |
19028 KB |
Output is correct |
33 |
Correct |
39 ms |
19144 KB |
Output is correct |
34 |
Correct |
38 ms |
19056 KB |
Output is correct |
35 |
Correct |
39 ms |
19080 KB |
Output is correct |
36 |
Correct |
38 ms |
19016 KB |
Output is correct |
37 |
Correct |
41 ms |
19008 KB |
Output is correct |
38 |
Correct |
53 ms |
24148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
5464 KB |
Output is correct |
2 |
Correct |
63 ms |
5448 KB |
Output is correct |
3 |
Correct |
67 ms |
5444 KB |
Output is correct |
4 |
Correct |
66 ms |
5416 KB |
Output is correct |
5 |
Correct |
62 ms |
5452 KB |
Output is correct |
6 |
Correct |
69 ms |
5440 KB |
Output is correct |
7 |
Correct |
63 ms |
5416 KB |
Output is correct |
8 |
Correct |
62 ms |
5404 KB |
Output is correct |
9 |
Correct |
62 ms |
5416 KB |
Output is correct |
10 |
Correct |
61 ms |
5448 KB |
Output is correct |
11 |
Correct |
63 ms |
5428 KB |
Output is correct |
12 |
Correct |
62 ms |
5468 KB |
Output is correct |
13 |
Correct |
63 ms |
5448 KB |
Output is correct |
14 |
Correct |
66 ms |
5412 KB |
Output is correct |
15 |
Correct |
73 ms |
5464 KB |
Output is correct |
16 |
Correct |
61 ms |
5424 KB |
Output is correct |
17 |
Correct |
67 ms |
5348 KB |
Output is correct |
18 |
Correct |
63 ms |
5452 KB |
Output is correct |
19 |
Correct |
62 ms |
5440 KB |
Output is correct |
20 |
Correct |
63 ms |
5388 KB |
Output is correct |
21 |
Correct |
63 ms |
5440 KB |
Output is correct |
22 |
Correct |
62 ms |
5440 KB |
Output is correct |
23 |
Correct |
68 ms |
5432 KB |
Output is correct |
24 |
Correct |
62 ms |
5408 KB |
Output is correct |
25 |
Correct |
73 ms |
5456 KB |
Output is correct |
26 |
Correct |
67 ms |
5356 KB |
Output is correct |
27 |
Correct |
63 ms |
5388 KB |
Output is correct |
28 |
Correct |
62 ms |
5436 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 |
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 |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 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 |
340 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 |
0 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
70 ms |
14600 KB |
Output is correct |
37 |
Correct |
66 ms |
14972 KB |
Output is correct |
38 |
Correct |
67 ms |
16624 KB |
Output is correct |
39 |
Correct |
62 ms |
16620 KB |
Output is correct |
40 |
Correct |
83 ms |
16660 KB |
Output is correct |
41 |
Correct |
67 ms |
16732 KB |
Output is correct |
42 |
Correct |
55 ms |
14020 KB |
Output is correct |
43 |
Correct |
48 ms |
14000 KB |
Output is correct |
44 |
Correct |
46 ms |
13960 KB |
Output is correct |
45 |
Correct |
66 ms |
14100 KB |
Output is correct |
46 |
Correct |
57 ms |
15644 KB |
Output is correct |
47 |
Correct |
92 ms |
17424 KB |
Output is correct |
48 |
Correct |
37 ms |
19020 KB |
Output is correct |
49 |
Correct |
38 ms |
19032 KB |
Output is correct |
50 |
Correct |
44 ms |
19028 KB |
Output is correct |
51 |
Correct |
39 ms |
19144 KB |
Output is correct |
52 |
Correct |
38 ms |
19056 KB |
Output is correct |
53 |
Correct |
39 ms |
19080 KB |
Output is correct |
54 |
Correct |
38 ms |
19016 KB |
Output is correct |
55 |
Correct |
41 ms |
19008 KB |
Output is correct |
56 |
Correct |
53 ms |
24148 KB |
Output is correct |
57 |
Correct |
62 ms |
5464 KB |
Output is correct |
58 |
Correct |
63 ms |
5448 KB |
Output is correct |
59 |
Correct |
67 ms |
5444 KB |
Output is correct |
60 |
Correct |
66 ms |
5416 KB |
Output is correct |
61 |
Correct |
62 ms |
5452 KB |
Output is correct |
62 |
Correct |
69 ms |
5440 KB |
Output is correct |
63 |
Correct |
63 ms |
5416 KB |
Output is correct |
64 |
Correct |
62 ms |
5404 KB |
Output is correct |
65 |
Correct |
62 ms |
5416 KB |
Output is correct |
66 |
Correct |
61 ms |
5448 KB |
Output is correct |
67 |
Correct |
63 ms |
5428 KB |
Output is correct |
68 |
Correct |
62 ms |
5468 KB |
Output is correct |
69 |
Correct |
63 ms |
5448 KB |
Output is correct |
70 |
Correct |
66 ms |
5412 KB |
Output is correct |
71 |
Correct |
73 ms |
5464 KB |
Output is correct |
72 |
Correct |
61 ms |
5424 KB |
Output is correct |
73 |
Correct |
67 ms |
5348 KB |
Output is correct |
74 |
Correct |
63 ms |
5452 KB |
Output is correct |
75 |
Correct |
62 ms |
5440 KB |
Output is correct |
76 |
Correct |
63 ms |
5388 KB |
Output is correct |
77 |
Correct |
63 ms |
5440 KB |
Output is correct |
78 |
Correct |
62 ms |
5440 KB |
Output is correct |
79 |
Correct |
68 ms |
5432 KB |
Output is correct |
80 |
Correct |
62 ms |
5408 KB |
Output is correct |
81 |
Correct |
73 ms |
5456 KB |
Output is correct |
82 |
Correct |
67 ms |
5356 KB |
Output is correct |
83 |
Correct |
63 ms |
5388 KB |
Output is correct |
84 |
Correct |
62 ms |
5436 KB |
Output is correct |
85 |
Correct |
61 ms |
16460 KB |
Output is correct |
86 |
Correct |
54 ms |
14944 KB |
Output is correct |
87 |
Correct |
60 ms |
16628 KB |
Output is correct |
88 |
Correct |
62 ms |
16628 KB |
Output is correct |
89 |
Correct |
60 ms |
16696 KB |
Output is correct |
90 |
Correct |
78 ms |
16620 KB |
Output is correct |
91 |
Correct |
55 ms |
14012 KB |
Output is correct |
92 |
Correct |
73 ms |
14036 KB |
Output is correct |
93 |
Correct |
50 ms |
14028 KB |
Output is correct |
94 |
Correct |
47 ms |
14052 KB |
Output is correct |
95 |
Correct |
63 ms |
15768 KB |
Output is correct |
96 |
Correct |
76 ms |
17344 KB |
Output is correct |
97 |
Correct |
102 ms |
24216 KB |
Output is correct |
98 |
Correct |
101 ms |
24192 KB |
Output is correct |
99 |
Correct |
99 ms |
24224 KB |
Output is correct |
100 |
Correct |
102 ms |
24180 KB |
Output is correct |
101 |
Correct |
101 ms |
24124 KB |
Output is correct |
102 |
Correct |
105 ms |
24192 KB |
Output is correct |
103 |
Correct |
99 ms |
24176 KB |
Output is correct |
104 |
Correct |
100 ms |
24184 KB |
Output is correct |
105 |
Correct |
103 ms |
24144 KB |
Output is correct |
106 |
Correct |
101 ms |
24188 KB |
Output is correct |
107 |
Correct |
100 ms |
24132 KB |
Output is correct |
108 |
Correct |
97 ms |
24132 KB |
Output is correct |
109 |
Correct |
99 ms |
24120 KB |
Output is correct |
110 |
Correct |
102 ms |
24192 KB |
Output is correct |
111 |
Correct |
103 ms |
24168 KB |
Output is correct |
112 |
Correct |
103 ms |
24132 KB |
Output is correct |
113 |
Correct |
59 ms |
24036 KB |
Output is correct |