#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long f = 1, x = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
const int MAXN = 5e5 + 5;
struct EDGE {
  int next, to;
} edge[MAXN];
int n, head[MAXN], tot;
inline void add(int u, int v) {
  edge[++tot].next = head[u];
  edge[tot].to = v;
  head[u] = tot;
}
int dep[MAXN], an[MAXN][22], dfn[MAXN], cnt, D;
vector<int> nd[MAXN];
void dfs(int u) {
  dep[u] = dep[an[u][0]] + 1;
  D = max(D, dep[u]);
  nd[dep[u]].push_back(u);
  dfn[u] = ++cnt;
  for (int i = 1; i <= 20; ++i) an[u][i] = an[an[u][i - 1]][i - 1];
  for (int i = head[u]; i; i = edge[i].next) {
    int v = edge[i].to;
    an[v][0] = u;
    dfs(v);
  }
}
inline int get_lca(int u, int v) {
  if (dep[u] < dep[v]) swap(u, v);
  for (int i = 20; i >= 0; --i) {
    if (dep[an[u][i]] >= dep[v]) {
      u = an[u][i];
    }
  }
  if (u == v) return u;
  for (int i = 20; i >= 0; --i) {
    if (an[u][i] != an[v][i]) {
      u = an[u][i];
      v = an[v][i];
    }
  }
  return an[u][0];
}
bool cmp(int x, int y) { return dfn[x] < dfn[y]; }
long long ans[MAXN];
struct node {
  int id, dep, pos;
  node() {}
  node(int x, int y, int z) {
    id = x;
    dep = y;
    pos = z;
  }
};
stack<node> sta;
inline long long gx() {
  node t = sta.top();
  sta.pop();
  long long x = (long long)t.dep * (t.pos - sta.top().pos);
  sta.push(t);
  return x;
}
void calc(int d) {
  while (!sta.empty()) sta.pop();
  long long res = 0;
  for (int i = 0; i < (int)nd[d].size(); ++i) {
    int x = nd[d][i];
    if (!i)
      sta.push(node(x, 0, i));
    else
      while (true) {
        int v = get_lca(x, sta.top().id);
        if (dep[v] <= sta.top().dep) {
          res -= gx();
          sta.pop();
          continue;
        } else {
          sta.push(node(x, dep[v], i));
          res += gx();
          break;
        }
      }
    ans[x] += res;
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  n = read();
  int rt;
  for (int i = 1; i <= n; ++i) {
    int p = read();
    if (!p)
      rt = i;
    else
      add(p, i);
  }
  dfs(rt);
  for (int i = 1; i <= D; ++i) {
    sort(nd[i].begin(), nd[i].end(), cmp);
    for (int j = 0; j < (int)nd[i].size(); ++j)
      ans[nd[i][j]] = ans[an[nd[i][j]][0]] + i - 1;
    calc(i);
    reverse(nd[i].begin(), nd[i].end());
    calc(i);
  }
  for (int i = 1; i <= n; ++i) cout << ans[i] << " ";
  cout << endl;
  return 0;
}
