# BZOJ 3786: 星系探索 [伪ETT]

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define lc t[x].ch[0]
#define rc t[x].ch[1]
#define pa t[x].fa
#define pii pair<int, int>
#define MP make_pair
#define fir first
#define sec second
typedef long long ll;
const int N=2e5+5;
char c=getchar();int x=0,f=1;
while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
return x*f;
}

int n, a[N], x, y, Q, fa[N]; char s[5];
struct edge{int v, ne;} e[N];
int cnt, h[N];
inline void ins(int u, int v) {
e[++cnt]=(edge){v, h[u]}; h[u]=cnt;
}
pii dfn[N]; int dfc, eul[N];
void dfs(int u) {
dfn[u].fir = ++dfc; eul[dfc] = u;
for(int i=h[u]; i; i=e[i].ne) dfs(e[i].v);
dfn[u].sec = ++dfc; eul[dfc] = -u;
}

struct meow{int ch[2], fa, v, sl, sr, type; ll sum, tag;} t[N];
int root, sz;
inline int wh(int x) {return t[pa].ch[1] == x;}
inline void update(int x) {
t[x].sum = t[lc].sum + t[rc].sum + t[x].v;
t[x].sl = t[lc].sl + t[rc].sl + (t[x].type==1);
t[x].sr = t[lc].sr + t[rc].sr + (t[x].type==-1);
}

inline void paint(int x, int d) {
t[x].sum += (ll)d*(t[x].sl - t[x].sr);
t[x].v += d*t[x].type;
t[x].tag += d;
}
inline void pushDown(int x) { //printf("pushDown %d\n",x);
if(t[x].tag) { //puts("yes");
if(lc) paint(lc, t[x].tag);
if(rc) paint(rc, t[x].tag);
t[x].tag = 0;
}
}
void pd(int x) { if(pa) pd(pa); pushDown(x); }

inline void rotate(int x) {
int f=t[x].fa, g=t[f].fa, c=wh(x);
if(g) t[g].ch[wh(f)] = x; t[x].fa=g;
t[f].ch[c] = t[x].ch[c^1]; t[t[f].ch[c]].fa=f;
t[x].ch[c^1] = f; t[f].fa=x;
update(f); update(x);
}
inline void splay(int x, int tar) {
pd(x);
for(; pa!=tar; rotate(x))
if(t[pa].fa != tar) rotate(wh(x)==wh(pa) ? pa : x);
if(tar==0) root=x;
}

void build(int &x, int l, int r, int f) {
int mid = (l+r)>>1; x=mid;
t[x].fa=f;
if(eul[x]>0) t[x].type = 1, t[x].v = a[eul[x]];
else t[x].type = -1, t[x].v = -a[-eul[x]];
if(l<mid) build(lc, l, mid-1, x);
if(mid<r) build(rc, mid+1, r, x);
update(x);
}

inline int pre(int x) {
x = lc; while(rc) x = rc; return x;
}
inline int nex(int x) {
x = rc; while(lc) x = lc; return x;
}
inline void Split(int &p, int &x) {
splay(p, 0); p = pre(p);
splay(x, 0); x = nex(x);
splay(p, 0); splay(x, p);
}

ll Que(int u) {
int p = dfn[1].fir, x = dfn[u].fir;
Split(p, x);
return t[lc].sum;
}

void Cha(int u, int far) {
int p = dfn[u].fir, x = dfn[u].sec;
Split(p, x);
int q = lc;
lc = t[q].fa = 0;
update(x); update(p);

p = dfn[far].fir; splay(p, 0);
x = nex(p); splay(x, p);
lc = q; t[q].fa = x;
update(x); update(p);
}

void AddVal(int u, int d) {
int p = dfn[u].fir, x = dfn[u].sec;
Split(p, x);
paint(lc, d);
}

int main() {
//freopen("in","r",stdin);
freopen("galaxy.in", "r", stdin);
freopen("galaxy.out", "w", stdout);
for(int i=2; i<=n; i++) ins(read(), i);
dfc=1; dfs(1); dfc++;
build(root, 1, dfc, 0);
}