#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 10, LG = 22;
int st[maxn], bg[maxn], sp[LG][2 * maxn], h[maxn], SZ[maxn], C;
vector<int> v[maxn], g[maxn], lc;
vector<pair<int, int> > vh[maxn];
long long ans[maxn];
void dfs1(int u, int H = 1, int par = 0) {
  sp[0][u] = par;
  st[u] = ++C;
  bg[u] = int((lc).size());
  lc.push_back(u);
  h[u] = H;
  vh[H].push_back({st[u], u});
  for (int y : v[u]) dfs1(y, H + 1, u), lc.push_back(u);
}
void dfs2(int u) {
  for (int y : g[u]) dfs2(y), SZ[u] += SZ[y];
}
void dfs3(int u, long long num) {
  if (SZ[u] == 1) ans[u] += num + h[u];
  for (int y : g[u]) dfs3(y, num + 1ll * h[u] * (SZ[u] - SZ[y]));
}
void dfs4(int u, int par = 0) {
  ans[u] += ans[par];
  for (int y : v[u]) dfs4(y, u);
}
inline int LCA(int a, int b) {
  if (st[a] > st[b]) swap(a, b);
  int l = 31 - __builtin_clz(bg[b] - bg[a] + 1);
  int A = sp[l][bg[a]], B = sp[l][bg[b] - (1 << l) + 1];
  if (h[A] < h[B]) return A;
  return B;
}
int main() {
  int n, r = 1;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    if (x == 0)
      r = i;
    else
      v[x].push_back(i);
  }
  dfs1(r);
  for (int i = 0; i < int((lc).size()); i++) sp[0][i] = lc[i];
  for (int i = 1; i < LG; i++) {
    for (int j = 0; j < int((lc).size()); j++) {
      int A = sp[i - 1][j], B = (1 << (i - 1)) + j >= int((lc).size())
                                    ? 0
                                    : sp[i - 1][(1 << (i - 1)) + j];
      sp[i][j] = (h[A] < h[B] ? A : B);
    }
  }
  for (int i = 1; i < maxn; i++) {
    if (vh[i].empty()) continue;
    for (int j = int((vh[i]).size()) - 1; j > 0; j--) {
      int u = LCA(vh[i][j].second, vh[i][j - 1].second);
      vh[i].push_back({st[u], u});
    }
    sort(vh[i].begin(), vh[i].end());
    vh[i].resize(unique(vh[i].begin(), vh[i].end()) - vh[i].begin());
    for (int j = 0; j < int((vh[i]).size()); j++)
      g[vh[i][j].second].clear(),
          SZ[vh[i][j].second] = (h[vh[i][j].second] == i);
    for (int j = 1; j < int((vh[i]).size()); j++)
      g[LCA(vh[i][j].second, vh[i][j - 1].second)].push_back(vh[i][j].second);
    dfs2(vh[i][0].second);
    dfs3(vh[i][0].second, 0);
  }
  dfs4(r);
  for (int i = 1; i <= n; i++) printf("%lld ", ans[i] - h[i]);
  printf("\n");
  return 0;
}
