#include <bits/stdc++.h>
using namespace std;
struct BIT {
  vector<long long> bit;
  int n;
  BIT() {}
  void init(int nn) {
    n = nn;
    bit.resize(n + 2, 0);
  }
  void update(int in, long long val) {
    while (in <= n) {
      bit[in] += val;
      in += in & (-in);
    }
  }
  long long query(int in) {
    long long ret = 0;
    while (in > 0) {
      ret += bit[in];
      in -= (in & (-in));
    }
    return ret;
  }
  long long query(int si, int ei) {
    return query(ei) - ((si == 1) ? 0 : query(si - 1));
  }
};
vector<int> ln[500009];
struct chain {
  int sz;
  vector<int> v;
  BIT bit, bit2;
  long long sm;
  void initchain() {
    sz = v.size();
    bit.init(sz);
    bit2.init(sz);
    sm = 0;
  }
  void clearchain() { v.clear(); }
  long long query(int i) {
    i++;
    return bit.query(i) + (sm - bit2.query(i)) * i;
  }
  void update(int i) {
    i++;
    bit.update(i, i);
    bit2.update(i, 1);
    sm++;
  }
};
int n;
int root = 1;
vector<int> al[500009];
int par[500009];
int dep[500009];
int spt[500009][20];
int cn[500009];
int cp[500009];
int cc;
chain ch[500009];
int vis[500009];
int sts[500009];
int sc[500009];
void dfs_hld(int v, int p) {
  par[v] = p;
  sts[v] = 1;
  sc[v] = -1;
  dep[v] = dep[p] + 1;
  for (int i = 0; i < (int)al[v].size(); i++) {
    int x = al[v][i];
    if (x == p) continue;
    dfs_hld(x, v);
    sts[v] += sts[x];
    if (sc[v] == -1 || sts[x] > sts[sc[v]]) sc[v] = x;
  }
}
void dfs_hld2(int v) {
  ch[cc].v.push_back(v);
  cn[v] = cc;
  cp[v] = ch[cc].v.size() - 1;
  if (al[v].size() + (par[v] == v) > 1) dfs_hld2(sc[v]);
  for (int i = 0; i < (int)al[v].size(); i++) {
    int x = al[v][i];
    if (x == sc[v] || x == par[v]) continue;
    cc++;
    dfs_hld2(x);
  }
}
int findlca(int u, int v) {
  if (u == v) return u;
  if (dep[u] < dep[v]) return findlca(v, u);
  for (int i = 20 - 1; i >= 0; i--) {
    if (dep[spt[u][i]] >= dep[v]) u = spt[u][i];
  }
  for (int i = 20 - 1; i >= 0; i--)
    if (spt[u][i] != spt[v][i]) {
      u = spt[u][i];
      v = spt[v][i];
    }
  return u == v ? u : spt[u][0];
}
void hld() {
  dep[root] = -1;
  dfs_hld(root, root);
  for (int i = 0; i < n + 3; i++) ch[i].clearchain();
  cc = 0;
  dfs_hld2(root);
  cc++;
  for (int i = 0; i < cc; i++) ch[i].initchain();
  for (int l = 0; l < 20; l++) {
    for (int i = 1; i <= n; i++) {
      if (l == 0) {
        spt[i][l] = par[i];
        continue;
      }
      spt[i][l] = spt[spt[i][l - 1]][l - 1];
    }
  }
}
long long hld_que(int u) {
  long long ret = 0;
  int v = u;
  while (true) {
    ret += ch[cn[u]].query(cp[u]);
    if (cn[u] == cn[root]) break;
    u = par[ch[cn[u]].v[0]];
  }
  return ret;
}
void hld_upd(int u) {
  while (1) {
    ch[cn[u]].update(cp[u]);
    if (cn[u] == cn[root]) {
      break;
    }
    u = par[ch[cn[u]].v[0]];
  }
}
long long ans[500009];
int main() {
  int t;
  t = 1;
  while (t--) {
    cin >> n;
    for (int i = 1; i < n + 1; i++) {
      int p;
      scanf("%d", &p);
      if (p != 0) {
        al[p].push_back(i);
        al[i].push_back(p);
      } else {
        root = i;
      }
    }
    hld();
    for (int i = 1; i < n + 1; i++) {
      ln[dep[i]].push_back(i);
    }
    for (int i = 0; i < n; i++) {
      if (ln[i].empty()) break;
      for (int j = 0; j < (int)ln[i].size(); j++) {
        hld_upd(ln[i][j]);
      }
      for (int j = 0; j < (int)ln[i].size(); j++) {
        int v = ln[i][j];
        ans[v] = hld_que(v) - (i + 1);
      }
    }
    for (int i = 1; i < n + 1; i++) {
      printf("%lld ", ans[i]);
    }
  }
}
