#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
template <class S, class T>
pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) {
  return pair<S, T>(s.first + t.first, s.second + t.second);
}
template <class S, class T>
pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) {
  return pair<S, T>(s.first - t.first, s.second - t.second);
}
template <class S, class T>
ostream &operator<<(ostream &os, pair<S, T> p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}
void YES(bool t = true) { cout << (t ? "YES" : "NO") << endl; }
void Yes(bool t = true) { cout << (t ? "Yes" : "No") << endl; }
void yes(bool t = true) { cout << (t ? "yes" : "no") << endl; }
void NO(bool t = true) { cout << (t ? "NO" : "YES") << endl; }
void No(bool t = true) { cout << (t ? "No" : "Yes") << endl; }
void no(bool t = true) { cout << (t ? "no" : "yes") << endl; }
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long inf = 1000000001;
const long long INF = (long long)1e18 + 1;
const long double pi = 3.1415926535897932384626433832795028841971L;
int popcount(long long t) { return __builtin_popcountll(t); }
vector<int> dx = {0, 1, 0, -1}, dy = {-1, 0, 1, 0};
struct Setup_io {
  Setup_io() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(25);
  }
} setup_io;
const long long MOD = 1000000007;
const int N = 511111;
vector<vector<int>> G1(N);
vector<vector<pair<long long, long long>>> G2(N);
using P = array<long long, 3>;
vector<long long> ans(N);
vector<vector<P>> res(N);
int dfs1(int now, int par, int dep) {
  int idx = now;
  for (int ch : G1[now]) {
    if (ch == par) continue;
    int idx2 = dfs1(ch, now, dep + 1);
    if (res[idx].size() < res[idx2].size()) swap(idx, idx2);
    for (int i = 0; i < (int)(res[idx2].size()); i++) {
      int u = res[idx][res[idx].size() - 1 - i][1],
          v = res[idx2][res[idx2].size() - 1 - i][1];
      long long cntu = res[idx][res[idx].size() - 1 - i][0],
                cntv = res[idx2][res[idx2].size() - 1 - i][0];
      ans[v] += cntu * (dep + 1);
      ans[u] += cntv * (dep + 1);
      res[idx][res[idx].size() - 1 - i][0] +=
          res[idx2][res[idx2].size() - 1 - i][0];
      res[idx][res[idx].size() - 1 - i][1] = u;
      res[idx][res[idx].size() - 1 - i][2] = ans[u];
      G2[u].push_back(pair<long long, long long>(v, ans[v] - ans[u]));
    }
  }
  res[idx].push_back({1, now, dep});
  ans[now] = dep;
  return idx;
}
vector<bool> visited(N);
void dfs2(int now) {
  visited[now] = true;
  for (pair<long long, long long> temp : G2[now]) {
    auto [u, dif] = temp;
    ans[u] = ans[now] + dif;
    dfs2(u);
  }
}
void dfs3(int now, int par) {
  for (int ch : G1[now]) {
    if (ch == par) continue;
    ans[ch] += ans[now];
    dfs3(ch, now);
  }
}
signed main() {
  int n;
  cin >> n;
  int root1;
  for (int i = 0; i < (int)(n); i++) {
    int p;
    cin >> p;
    if (p == 0)
      root1 = i;
    else {
      p--;
      G1[p].push_back(i);
      G1[i].push_back(p);
    }
  }
  dfs1(root1, -1, 0);
  for (int i = 0; i < (int)(n); i++) {
    if (not visited[i]) {
      dfs2(i);
    }
  }
  dfs3(root1, -1);
  for (int i = 0; i < (int)(n); i++) {
    cout << ans[i] << " ";
  }
  cout << endl;
}
