#include <bits/stdc++.h>
using namespace std;
const int c=1000005;
long long ans;
int n, koz, si[c], db[c];
vector<int> sz[c];
bitset<c> dp;
void dfs(int a) {
si[a]=1;
int maxi=0;
for (auto x:sz[a]) {
dfs(x);
maxi=max(maxi, si[x]);
si[a]+=si[x];
}
if (!koz && 2*si[a]>=n) {
for (auto x:sz[a]) {
db[si[x]]++;
}
if (a!=1) {
db[n-si[a]]++;
}
ans+=n;
koz=a;
} else {
ans+=min(si[a], n-maxi);
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin >> n;
for (int i=2; i<=n; i++) {
int x;
cin >> x;
sz[x].push_back(i);
}
dfs(1);
dp[0]=1;
for (int i=1; i<=n; i++) {
while (db[i]>=3) {
db[2*i]++;
db[i]-=2;
}
for (int j=1; j<=db[i]; j++) {
//cout << "fontos " << i << "\n";
dp|=(dp<<i);
}
}
for (long long ert=n/2; ert<=n; ert++) {
if (dp[ert]) {
ans+=ert*(n-1-ert);
break;
}
}
cout << ans << "\n";
return 0;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Runtime error | 3ms | 1260 KiB | ||||
2 | Runtime error | 2ms | 1432 KiB | ||||
subtask2 | 0/20 | ||||||
3 | Runtime error | 2ms | 1604 KiB | ||||
4 | Runtime error | 2ms | 1728 KiB | ||||
5 | Runtime error | 2ms | 1728 KiB | ||||
6 | Runtime error | 2ms | 1976 KiB | ||||
7 | Runtime error | 2ms | 2256 KiB | ||||
8 | Runtime error | 2ms | 2332 KiB | ||||
subtask3 | 0/20 | ||||||
9 | Runtime error | 2ms | 2236 KiB | ||||
10 | Runtime error | 2ms | 2488 KiB | ||||
11 | Runtime error | 2ms | 2732 KiB | ||||
12 | Runtime error | 2ms | 2788 KiB | ||||
13 | Runtime error | 2ms | 2852 KiB | ||||
14 | Runtime error | 2ms | 3040 KiB | ||||
subtask4 | 0/20 | ||||||
15 | Runtime error | 2ms | 2840 KiB | ||||
16 | Runtime error | 2ms | 2980 KiB | ||||
17 | Runtime error | 2ms | 2844 KiB | ||||
18 | Runtime error | 2ms | 2912 KiB | ||||
19 | Runtime error | 2ms | 3036 KiB | ||||
20 | Runtime error | 2ms | 3168 KiB | ||||
subtask5 | 0/40 | ||||||
21 | Runtime error | 2ms | 3148 KiB | ||||
22 | Runtime error | 2ms | 3216 KiB | ||||
23 | Runtime error | 2ms | 3128 KiB | ||||
24 | Runtime error | 2ms | 3152 KiB | ||||
25 | Runtime error | 2ms | 3284 KiB | ||||
26 | Runtime error | 2ms | 3384 KiB |