#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const long long int INF = 1e18;
vector<int> g[500005];
vector<pair<int, int>> dq[500005];
int re[500005], dep[500005], reinv[500005];
long long int ans[500005];
void getre(int root) {
  queue<int> q;
  int now = 1;
  q.push(root);
  while (!q.empty()) {
    int curr = q.front();
    re[curr] = now++;
    q.pop();
    for (auto it : g[curr]) q.push(it);
  }
}
void dfs(int node) {
  for (auto it : g[node]) {
    dep[it] = dep[node] + 1;
    dfs(it);
    ans[re[it]] += dep[node];
    ans[re[it] + 1] -= dep[node];
    if (dq[node].size() < dq[it].size()) swap(dq[node], dq[it]);
    int i = dq[node].size() - 1, j = dq[it].size() - 1;
    while (i >= 0 && j >= 0) {
      long long int cnt_now = dq[node][i].second - dq[node][i].first + 1;
      long long int cnt_curr = dq[it][j].second - dq[it][j].first + 1;
      ans[dq[node][i].first] += cnt_curr * dep[node];
      ans[dq[node][i].second + 1] -= cnt_curr * dep[node];
      ans[dq[it][j].first] += cnt_now * dep[node];
      ans[dq[it][j].second + 1] -= cnt_now * dep[node];
      dq[node][i].first = min(dq[node][i].first, dq[it][j].first);
      dq[node][i].second = max(dq[node][i].second, dq[it][j].second);
      i--;
      j--;
    }
  }
  dq[node].push_back({re[node], re[node]});
}
void getans(int node) {
  for (auto it : g[node]) {
    ans[re[it]] += ans[re[node]];
    getans(it);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, i, root, u, now = 1;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> u;
    if (u == 0)
      root = i;
    else
      g[u].push_back(i);
  }
  getre(root);
  for (i = 1; i <= n; i++) reinv[re[i]] = i;
  dep[root] = 1;
  dfs(root);
  for (i = 1; i <= n; i++) ans[i] += ans[i - 1];
  getans(root);
  for (i = 1; i <= n; i++) cout << ans[re[i]] << " \n"[i == n];
  return 0;
}
