#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){
for(int i = 0; i < (1 << (int)way[rt].size()); ++i){
// cout << rt << ' ' << i << endl;
vector<int> sum(2);
int allsum = 0, cnt = 0;
auto dfs = [&](auto&&self, int x, int pr, int psum)->void{
// cout << rt << ' ' << x << ' ' << pr << endl;
allsum += a[x] * psum;
cnt += a[x];
for(auto&nxt:way[x]){
if(nxt == pr){
continue;
}
self(self, nxt, x, psum + a[x]);
}
};
for(int j = 0; j < (int)way[rt].size(); ++j){
cnt = 0;
dfs(dfs, way[rt][j], rt, a[rt]);
sum[!!(i & 1 << j)] += cnt;
}
ans = max(ans, allsum + sum[0] * sum[1]);
}
};
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
216 KB |
Output is correct |
5 |
Correct |
0 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
324 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
324 KB |
Output is correct |
11 |
Correct |
0 ms |
316 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
316 KB |
Output is correct |
17 |
Correct |
0 ms |
272 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
340 KB |
Output is correct |
2 |
Correct |
15 ms |
364 KB |
Output is correct |
3 |
Correct |
31 ms |
340 KB |
Output is correct |
4 |
Correct |
64 ms |
376 KB |
Output is correct |
5 |
Correct |
31 ms |
352 KB |
Output is correct |
6 |
Correct |
57 ms |
340 KB |
Output is correct |
7 |
Correct |
21 ms |
340 KB |
Output is correct |
8 |
Correct |
52 ms |
364 KB |
Output is correct |
9 |
Correct |
22 ms |
340 KB |
Output is correct |
10 |
Correct |
32 ms |
340 KB |
Output is correct |
11 |
Correct |
61 ms |
368 KB |
Output is correct |
12 |
Correct |
34 ms |
352 KB |
Output is correct |
13 |
Correct |
615 ms |
388 KB |
Output is correct |
14 |
Correct |
606 ms |
392 KB |
Output is correct |
15 |
Correct |
603 ms |
384 KB |
Output is correct |
16 |
Correct |
601 ms |
392 KB |
Output is correct |
17 |
Correct |
24 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
340 KB |
Output is correct |
2 |
Correct |
15 ms |
364 KB |
Output is correct |
3 |
Correct |
31 ms |
340 KB |
Output is correct |
4 |
Correct |
64 ms |
376 KB |
Output is correct |
5 |
Correct |
31 ms |
352 KB |
Output is correct |
6 |
Correct |
57 ms |
340 KB |
Output is correct |
7 |
Correct |
21 ms |
340 KB |
Output is correct |
8 |
Correct |
52 ms |
364 KB |
Output is correct |
9 |
Correct |
22 ms |
340 KB |
Output is correct |
10 |
Correct |
32 ms |
340 KB |
Output is correct |
11 |
Correct |
61 ms |
368 KB |
Output is correct |
12 |
Correct |
34 ms |
352 KB |
Output is correct |
13 |
Correct |
615 ms |
388 KB |
Output is correct |
14 |
Correct |
606 ms |
392 KB |
Output is correct |
15 |
Correct |
603 ms |
384 KB |
Output is correct |
16 |
Correct |
601 ms |
392 KB |
Output is correct |
17 |
Correct |
24 ms |
340 KB |
Output is correct |
18 |
Execution timed out |
1069 ms |
15108 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 |
1 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 |
216 KB |
Output is correct |
5 |
Correct |
0 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
324 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
324 KB |
Output is correct |
11 |
Correct |
0 ms |
316 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
316 KB |
Output is correct |
17 |
Correct |
0 ms |
272 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
49 ms |
340 KB |
Output is correct |
20 |
Correct |
15 ms |
364 KB |
Output is correct |
21 |
Correct |
31 ms |
340 KB |
Output is correct |
22 |
Correct |
64 ms |
376 KB |
Output is correct |
23 |
Correct |
31 ms |
352 KB |
Output is correct |
24 |
Correct |
57 ms |
340 KB |
Output is correct |
25 |
Correct |
21 ms |
340 KB |
Output is correct |
26 |
Correct |
52 ms |
364 KB |
Output is correct |
27 |
Correct |
22 ms |
340 KB |
Output is correct |
28 |
Correct |
32 ms |
340 KB |
Output is correct |
29 |
Correct |
61 ms |
368 KB |
Output is correct |
30 |
Correct |
34 ms |
352 KB |
Output is correct |
31 |
Correct |
615 ms |
388 KB |
Output is correct |
32 |
Correct |
606 ms |
392 KB |
Output is correct |
33 |
Correct |
603 ms |
384 KB |
Output is correct |
34 |
Correct |
601 ms |
392 KB |
Output is correct |
35 |
Correct |
24 ms |
340 KB |
Output is correct |
36 |
Execution timed out |
1069 ms |
15108 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |