#include <bits/stdc++.h>
using namespace std;
char *fs, *ft, buf[1 << 15];
inline char getc() {
  return (fs == ft &&
          (ft = (fs = buf) + fread(buf, 1, 1 << 15, stdin), fs == ft))
             ? 0
             : *fs++;
}
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
const int MAXN = 500010;
int n, rt, h;
int d[MAXN], q[MAXN], f[MAXN][20], Log[MAXN], b[MAXN], c[MAXN];
long long ans[MAXN];
vector<int> g[MAXN], v[MAXN];
inline void dfs(int x) {
  v[d[x]].push_back(x);
  for (unsigned int i = 0; i < g[x].size(); ++i) {
    int tn = g[x][i];
    d[tn] = d[x] + 1;
    f[tn][0] = x;
    for (int j = 1; j <= Log[d[tn]]; ++j) f[tn][j] = f[f[tn][j - 1]][j - 1];
    dfs(tn);
  }
}
inline int LCA(int x, int y) {
  if (d[x] > d[y]) swap(x, y);
  for (int i = Log[d[y]]; i >= 0; i--)
    if (d[f[y][i]] >= d[x]) y = f[y][i];
  if (x == y) return x;
  for (int i = Log[d[x]]; i >= 0; i--)
    if (f[y][i] != f[x][i]) y = f[y][i], x = f[x][i];
  return f[x][0];
}
inline void get_ans(int x) {
  long long cnt = 0;
  for (unsigned int i = 0; i < v[x].size(); ++i) {
    int tn = v[x][i];
    if (!i)
      q[h = 1] = tn, b[h] = c[h] = 0;
    else {
      while (1) {
        int lca = LCA(tn, q[h]);
        if (b[h] <= d[lca]) {
          q[++h] = tn, b[h] = d[lca] + 1;
          c[h] = i;
          break;
        }
        cnt -= 1ll * (c[h] - c[h - 1]) * b[h];
        --h;
      }
      cnt += 1ll * (c[h] - c[h - 1]) * b[h];
      ans[tn] += cnt;
    }
  }
}
int main() {
  n = read();
  for (int i = 1; i <= n; ++i) {
    int x = read();
    if (i != 1) Log[i] = Log[i >> 1] + 1;
    if (!x) rt = i;
    g[x].push_back(i);
  }
  dfs(rt);
  for (int i = 1; i <= n; ++i) {
    if (!v[i].size()) break;
    for (unsigned int j = 0; j < v[i].size(); ++j)
      ans[v[i][j]] += ans[f[v[i][j]][0]] + i;
    get_ans(i);
    reverse(v[i].begin(), v[i].end());
    get_ans(i);
  }
  for (int i = 1; i <= n; ++i) printf("%lld ", ans[i]);
  return 0;
}
