#include <bits/stdc++.h>
using namespace std;

void printmat(const vector<vector<long long>>& mat) {
    for (auto row : mat) {
        for (int elem : row)
            cout << elem << ", ";
        cout << endl;
    }
}
void printv(const vector<int>& v) {
    for (int elem : v)
        cout << elem << " ";
    cout << endl;
}
void printvp(const vector<pair<int, int>>& vp) {
    for (auto row : vp) 
        cout << row.first << ", " << row.second << " ; ";
    cout << endl;
}

int main() {
    int T=1, caseIdx=0;
    //cin >> T;  
    while (T--) {
        caseIdx++;
        int n;
        long long ans=0, second=0;
        cin >> n;
        vector<long long> a(n), s(n+1);
        for (int i=0; i<n; i++) {
            cin >> a[i];
            s[i+1] = s[i] + a[i];
            ans += (a[i]*a[i]);
        }
        ans *= (n-1LL);
        for (int i=0; i<n-1; i++) {
            second += (a[i] * (s[n]-s[i+1]));
            //cout << (s[n]-s[i+1]) << endl;
        }
        ans = ans - 2LL*second;
        
        cout << ans << endl;
        //cout << "Case #" << caseIdx << ": " << ans << endl;
    }
}
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define srep(i,s,t) for(int i = s; i < t; ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
#define yn {puts("Yes");}else{puts("No");}
#define MAX_N 200005

int n;
vector<int> G[MAX_N];
int par[MAX_N];
int visit_[MAX_N];

priority_queue<int> que1[MAX_N], que2[MAX_N];

int dfs(int x){
    int res = 0;
    if(visit_[x] == 0){
        res++;
        visit_[x] = 1;
        res -= dfs(x);
        return res;
    }
    if(visit_[x] == 1){
        rep(i,G[x].size()){
            int tmp = -dfs(G[x][i]);
            if(tmp % 2 == 0) que2[x].push(tmp);
            else que1[x].push(tmp);
        }
        visit_[x] = 2;
    }
    while(!que2[x].empty() && que2[x].top()>=0){
        res += que2[x].top();
        que2[x].pop();
    }
    if(!que1[x].empty()){
        res += que1[x].top();
        que1[x].pop();
        res -= dfs(x);
    }
    while(!que2[x].empty()){
        res += que2[x].top();
        que2[x].pop();
    }
    return res;
}

int main() {
    par[0] = -1;
    cin >> n;
    srep(i,1,n){
        cin >> par[i];
        par[i]--;
        G[par[i]].push_back(i);
    }

    int tmp = dfs(0);
    int ans = (n-tmp) / 2 + tmp;
    cout << ans << endl;
    return 0;
}


