#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 500;
int dep[N], vis[N], L[N], R[N], R2[N], cur = 1, n;
vector<int> v[N], cnt[N], kad[N];
vector<pair<pair<int, int>, long long> > dod[N];
long long loga[N], sol[N];
void add(int i, long long x) {
  for (; i < N; i += i & -i) loga[i] += x;
}
void update(int l, int r, long long x) {
  add(l, x);
  add(r + 1, -x);
}
long long query(int i) {
  long long ret = 0;
  for (; i; i -= i & -i) ret += loga[i];
  return ret;
}
bool cmp(int x, int y) { return vis[x] > vis[y]; }
void calc(int x) {
  for (int y : v[x]) {
    dep[y] = dep[x] + 1;
    calc(y);
    vis[x] = max(vis[x], vis[y]);
  }
  vis[x]++;
  kad[dep[x]].push_back(x);
  sort(v[x].begin(), v[x].end(), cmp);
}
void dfs(int x) {
  L[x] = cur++;
  for (int y : v[x]) dfs(y);
  R[x] = cur - 1;
}
void rijesi(int x) {
  if (v[x].size()) {
    rijesi(v[x][0]);
    swap(cnt[x], cnt[v[x][0]]);
  }
  int siz_vel = (int)cnt[x].size();
  for (int i = 1; i < (int)v[x].size(); i++) {
    int sus = v[x][i];
    rijesi(sus);
    int siz = cnt[sus].size();
    for (int j = 1; j <= (int)cnt[sus].size(); j++) {
      dod[dep[x] + j].push_back({{L[x] + 1, L[sus] - 1},
                                 (long long)(dep[x] + 1) * cnt[sus][siz - j]});
      dod[dep[x] + j].push_back(
          {{L[sus], R[sus]}, (long long)(dep[x] + 1) * cnt[x][siz_vel - j]});
      cnt[x][siz_vel - j] += cnt[sus][siz - j];
    }
  }
  dod[dep[x]].push_back({{L[x], R[x]}, dep[x]});
  cnt[x].push_back(1);
}
void gotovo() {
  for (int i = 0; i < n; i++) {
    for (pair<pair<int, int>, long long> tmp : dod[i])
      update(tmp.first.first, tmp.first.second, tmp.second);
    for (int tmp : kad[i]) sol[tmp] = query(L[tmp]);
  }
}
int main() {
  scanf("%d", &n);
  int root = 0;
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    if (x == 0)
      root = i;
    else
      v[x].push_back(i);
  }
  calc(root);
  dfs(root);
  rijesi(root);
  gotovo();
  for (int i = 1; i <= n; i++) printf("%lld ", sol[i]);
  printf("\n");
}
