#include <iostream>
#include <set>
#include <numeric>
#include <vector>
#define int long long
using namespace std;
int get_half(vector<int> vc)
{
int N = (int)vc.size();
if (N == 1) return 0;
set<int> s1;
for (int j = 0; j < (1 << (N / 2)); j++) {
int tmp = 0;
for (int k = 0; k < N / 2; k++) {
if (j >> k & 1) tmp += vc[k];
}
s1.insert(tmp);
}
int tot = accumulate(vc.begin(), vc.end(), 0LL);
int ret = 1e18;
int rst = N - N / 2;
for (int j = 0; j < (1 << rst); j++) {
int tmp = 0;
for (int k = 0; k < rst; k++) {
if (j >> k & 1) tmp += vc[k+N/2];
}
auto it = s1.lower_bound((tot+1)/2 - tmp);
if (it != s1.end()) {
int pv = *it + tmp;
if (abs(2 * ret - tot) > abs(2 * pv - tot)) {
ret = pv;
}
}
if (it != s1.begin()) {
--it;
int pv = *it + tmp;
if (abs(2 * ret - tot) > abs(2 * pv - tot)) {
ret = pv;
}
}
}
return ret;
}
int sz[200005];
vector<int> g[200005];
int a[200005], p[200005];
int N;
void getSize(int v, int p)
{
sz[v] = a[v];
for (int i:g[v]) if (i != p) {
getSize(i, v);
sz[v] += sz[i];
}
}
int getCent(int v, int p, int tot)
{
for (int i:g[v]) if (i != p) {
if (sz[i] > tot/2) return getCent(i, v, tot);
}
return v;
}
signed main()
{
cin >> N;
for (int i = 1; i <= N; i++) cin >> a[i];
for (int i = 2; i <= N; i++) {
int q; cin >> q;
g[q].push_back(i); g[i].push_back(q);
}
getSize(1, 0);
int cent = getCent(1, 0, sz[1]);
vector<int> vc;
int tot = sz[1];
getSize(cent, 0);
for (int i:g[cent]) vc.push_back(sz[i]);
int x = get_half(vc);
int ans = x * (sz[cent] - a[cent] - x);
ans += a[cent] * (sz[cent] - a[cent]);
for (int i = 1; i <= N; i++) ans += a[i] * (a[i] - 1);
for (int i = 1; i <= N; i++) {
if (i != cent) ans += a[i] * (sz[i] - a[i]);
}
cout << ans << '\n';
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:79:9: warning: unused variable 'tot' [-Wunused-variable]
79 | int tot = sz[1];
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5012 KB |
Output is correct |
8 |
Correct |
4 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
5008 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
5016 KB |
Output is correct |
16 |
Correct |
2 ms |
5012 KB |
Output is correct |
17 |
Correct |
2 ms |
5008 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5016 KB |
Output is correct |
2 |
Correct |
3 ms |
5012 KB |
Output is correct |
3 |
Correct |
3 ms |
5016 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5016 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5076 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5016 KB |
Output is correct |
2 |
Correct |
3 ms |
5012 KB |
Output is correct |
3 |
Correct |
3 ms |
5016 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5016 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5076 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
109 ms |
16780 KB |
Output is correct |
19 |
Correct |
93 ms |
15684 KB |
Output is correct |
20 |
Correct |
108 ms |
16876 KB |
Output is correct |
21 |
Correct |
124 ms |
16924 KB |
Output is correct |
22 |
Correct |
108 ms |
16868 KB |
Output is correct |
23 |
Correct |
110 ms |
16912 KB |
Output is correct |
24 |
Correct |
96 ms |
15004 KB |
Output is correct |
25 |
Correct |
86 ms |
14892 KB |
Output is correct |
26 |
Correct |
93 ms |
15152 KB |
Output is correct |
27 |
Correct |
85 ms |
15028 KB |
Output is correct |
28 |
Correct |
109 ms |
16212 KB |
Output is correct |
29 |
Correct |
114 ms |
17456 KB |
Output is correct |
30 |
Correct |
90 ms |
19000 KB |
Output is correct |
31 |
Correct |
91 ms |
19068 KB |
Output is correct |
32 |
Correct |
87 ms |
19112 KB |
Output is correct |
33 |
Correct |
88 ms |
18980 KB |
Output is correct |
34 |
Correct |
95 ms |
19108 KB |
Output is correct |
35 |
Correct |
88 ms |
19020 KB |
Output is correct |
36 |
Correct |
91 ms |
19068 KB |
Output is correct |
37 |
Correct |
86 ms |
19064 KB |
Output is correct |
38 |
Correct |
107 ms |
29220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
100 ms |
7516 KB |
Output is correct |
2 |
Correct |
103 ms |
7648 KB |
Output is correct |
3 |
Correct |
101 ms |
7568 KB |
Output is correct |
4 |
Correct |
94 ms |
7544 KB |
Output is correct |
5 |
Correct |
98 ms |
7488 KB |
Output is correct |
6 |
Correct |
95 ms |
7332 KB |
Output is correct |
7 |
Correct |
87 ms |
6628 KB |
Output is correct |
8 |
Correct |
96 ms |
7404 KB |
Output is correct |
9 |
Correct |
97 ms |
7412 KB |
Output is correct |
10 |
Correct |
100 ms |
7516 KB |
Output is correct |
11 |
Correct |
101 ms |
7728 KB |
Output is correct |
12 |
Correct |
97 ms |
7520 KB |
Output is correct |
13 |
Correct |
97 ms |
7508 KB |
Output is correct |
14 |
Correct |
100 ms |
7644 KB |
Output is correct |
15 |
Correct |
104 ms |
7756 KB |
Output is correct |
16 |
Correct |
95 ms |
7208 KB |
Output is correct |
17 |
Correct |
103 ms |
7412 KB |
Output is correct |
18 |
Correct |
99 ms |
7520 KB |
Output is correct |
19 |
Correct |
102 ms |
7740 KB |
Output is correct |
20 |
Correct |
101 ms |
7760 KB |
Output is correct |
21 |
Correct |
99 ms |
7296 KB |
Output is correct |
22 |
Correct |
111 ms |
7972 KB |
Output is correct |
23 |
Correct |
94 ms |
7308 KB |
Output is correct |
24 |
Correct |
102 ms |
7760 KB |
Output is correct |
25 |
Correct |
98 ms |
7604 KB |
Output is correct |
26 |
Correct |
97 ms |
7484 KB |
Output is correct |
27 |
Correct |
104 ms |
7968 KB |
Output is correct |
28 |
Correct |
101 ms |
7688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5012 KB |
Output is correct |
8 |
Correct |
4 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
5008 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
5016 KB |
Output is correct |
16 |
Correct |
2 ms |
5012 KB |
Output is correct |
17 |
Correct |
2 ms |
5008 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
5016 KB |
Output is correct |
20 |
Correct |
3 ms |
5012 KB |
Output is correct |
21 |
Correct |
3 ms |
5016 KB |
Output is correct |
22 |
Correct |
3 ms |
4948 KB |
Output is correct |
23 |
Correct |
3 ms |
4948 KB |
Output is correct |
24 |
Correct |
3 ms |
4948 KB |
Output is correct |
25 |
Correct |
3 ms |
5016 KB |
Output is correct |
26 |
Correct |
3 ms |
4948 KB |
Output is correct |
27 |
Correct |
3 ms |
4948 KB |
Output is correct |
28 |
Correct |
3 ms |
4948 KB |
Output is correct |
29 |
Correct |
3 ms |
5076 KB |
Output is correct |
30 |
Correct |
3 ms |
4948 KB |
Output is correct |
31 |
Correct |
3 ms |
5076 KB |
Output is correct |
32 |
Correct |
3 ms |
5076 KB |
Output is correct |
33 |
Correct |
3 ms |
5076 KB |
Output is correct |
34 |
Correct |
3 ms |
5076 KB |
Output is correct |
35 |
Correct |
3 ms |
5076 KB |
Output is correct |
36 |
Correct |
109 ms |
16780 KB |
Output is correct |
37 |
Correct |
93 ms |
15684 KB |
Output is correct |
38 |
Correct |
108 ms |
16876 KB |
Output is correct |
39 |
Correct |
124 ms |
16924 KB |
Output is correct |
40 |
Correct |
108 ms |
16868 KB |
Output is correct |
41 |
Correct |
110 ms |
16912 KB |
Output is correct |
42 |
Correct |
96 ms |
15004 KB |
Output is correct |
43 |
Correct |
86 ms |
14892 KB |
Output is correct |
44 |
Correct |
93 ms |
15152 KB |
Output is correct |
45 |
Correct |
85 ms |
15028 KB |
Output is correct |
46 |
Correct |
109 ms |
16212 KB |
Output is correct |
47 |
Correct |
114 ms |
17456 KB |
Output is correct |
48 |
Correct |
90 ms |
19000 KB |
Output is correct |
49 |
Correct |
91 ms |
19068 KB |
Output is correct |
50 |
Correct |
87 ms |
19112 KB |
Output is correct |
51 |
Correct |
88 ms |
18980 KB |
Output is correct |
52 |
Correct |
95 ms |
19108 KB |
Output is correct |
53 |
Correct |
88 ms |
19020 KB |
Output is correct |
54 |
Correct |
91 ms |
19068 KB |
Output is correct |
55 |
Correct |
86 ms |
19064 KB |
Output is correct |
56 |
Correct |
107 ms |
29220 KB |
Output is correct |
57 |
Correct |
100 ms |
7516 KB |
Output is correct |
58 |
Correct |
103 ms |
7648 KB |
Output is correct |
59 |
Correct |
101 ms |
7568 KB |
Output is correct |
60 |
Correct |
94 ms |
7544 KB |
Output is correct |
61 |
Correct |
98 ms |
7488 KB |
Output is correct |
62 |
Correct |
95 ms |
7332 KB |
Output is correct |
63 |
Correct |
87 ms |
6628 KB |
Output is correct |
64 |
Correct |
96 ms |
7404 KB |
Output is correct |
65 |
Correct |
97 ms |
7412 KB |
Output is correct |
66 |
Correct |
100 ms |
7516 KB |
Output is correct |
67 |
Correct |
101 ms |
7728 KB |
Output is correct |
68 |
Correct |
97 ms |
7520 KB |
Output is correct |
69 |
Correct |
97 ms |
7508 KB |
Output is correct |
70 |
Correct |
100 ms |
7644 KB |
Output is correct |
71 |
Correct |
104 ms |
7756 KB |
Output is correct |
72 |
Correct |
95 ms |
7208 KB |
Output is correct |
73 |
Correct |
103 ms |
7412 KB |
Output is correct |
74 |
Correct |
99 ms |
7520 KB |
Output is correct |
75 |
Correct |
102 ms |
7740 KB |
Output is correct |
76 |
Correct |
101 ms |
7760 KB |
Output is correct |
77 |
Correct |
99 ms |
7296 KB |
Output is correct |
78 |
Correct |
111 ms |
7972 KB |
Output is correct |
79 |
Correct |
94 ms |
7308 KB |
Output is correct |
80 |
Correct |
102 ms |
7760 KB |
Output is correct |
81 |
Correct |
98 ms |
7604 KB |
Output is correct |
82 |
Correct |
97 ms |
7484 KB |
Output is correct |
83 |
Correct |
104 ms |
7968 KB |
Output is correct |
84 |
Correct |
101 ms |
7688 KB |
Output is correct |
85 |
Correct |
110 ms |
16760 KB |
Output is correct |
86 |
Correct |
108 ms |
15692 KB |
Output is correct |
87 |
Correct |
123 ms |
16848 KB |
Output is correct |
88 |
Correct |
134 ms |
17000 KB |
Output is correct |
89 |
Correct |
108 ms |
16948 KB |
Output is correct |
90 |
Correct |
109 ms |
16904 KB |
Output is correct |
91 |
Correct |
91 ms |
14988 KB |
Output is correct |
92 |
Correct |
86 ms |
14948 KB |
Output is correct |
93 |
Correct |
88 ms |
14920 KB |
Output is correct |
94 |
Correct |
93 ms |
15052 KB |
Output is correct |
95 |
Correct |
101 ms |
16204 KB |
Output is correct |
96 |
Correct |
112 ms |
17496 KB |
Output is correct |
97 |
Correct |
191 ms |
31356 KB |
Output is correct |
98 |
Correct |
206 ms |
31348 KB |
Output is correct |
99 |
Correct |
176 ms |
31260 KB |
Output is correct |
100 |
Correct |
181 ms |
31356 KB |
Output is correct |
101 |
Correct |
174 ms |
31260 KB |
Output is correct |
102 |
Correct |
191 ms |
31332 KB |
Output is correct |
103 |
Correct |
197 ms |
31296 KB |
Output is correct |
104 |
Correct |
181 ms |
31404 KB |
Output is correct |
105 |
Correct |
186 ms |
31316 KB |
Output is correct |
106 |
Correct |
180 ms |
31340 KB |
Output is correct |
107 |
Correct |
179 ms |
31288 KB |
Output is correct |
108 |
Correct |
176 ms |
31140 KB |
Output is correct |
109 |
Correct |
184 ms |
31308 KB |
Output is correct |
110 |
Correct |
189 ms |
31292 KB |
Output is correct |
111 |
Correct |
197 ms |
31292 KB |
Output is correct |
112 |
Correct |
197 ms |
31328 KB |
Output is correct |
113 |
Correct |
107 ms |
29264 KB |
Output is correct |