Arkady words in a large company. There are n employees working in a system of a strict hierarchy. Namely, each employee, with an exception of the CEO, has exactly one immediate manager. The CEO is a manager (through a chain of immediate managers) of all employees.

Each employee has an integer rank. The CEO has rank equal to 1, each other employee has rank equal to the rank of his immediate manager plus 1.

Arkady has a good post in the company, however, he feels that he is nobody in the company's structure, and there are a lot of people who can replace him. He introduced the value of replaceability. Consider an employee a and an employee b, the latter being manager of a (not necessarily immediate). Then the replaceability r(a, b) of a with respect to b is the number of subordinates (not necessarily immediate) of the manager b, whose rank is not greater than the rank of a. Apart from replaceability, Arkady introduced the value of negligibility. The negligibility za of employee a equals the sum of his replaceabilities with respect to all his managers, i.e. <image>, where the sum is taken over all his managers b.

Arkady is interested not only in negligibility of himself, but also in negligibility of all employees in the company. Find the negligibility of each employee for Arkady.

Input

The first line contains single integer n (1 ≤ n ≤ 5·105) — the number of employees in the company.

The second line contains n integers p1, p2, ..., pn (0 ≤ pi ≤ n), where pi = 0 if the i-th employee is the CEO, otherwise pi equals the id of the immediate manager of the employee with id i. The employees are numbered from 1 to n. It is guaranteed that there is exactly one 0 among these values, and also that the CEO is a manager (not necessarily immediate) for all the other employees.

Output

Print n integers — the negligibilities of all employees in the order of their ids: z1, z2, ..., zn.

Examples

Input

4
0 1 2 1


Output

0 2 4 2 


Input

5
2 3 4 5 0


Output

10 6 3 1 0 


Input

5
0 1 1 1 3


Output

0 3 3 3 5 

Note

Consider the first example: 

  * The CEO has no managers, thus z1 = 0. 
  * r(2, 1) = 2 (employees 2 and 4 suit the conditions, employee 3 has too large rank). Thus z2 = r(2, 1) = 2. 
  * Similarly, z4 = r(4, 1) = 2. 
  * r(3, 2) = 1 (employee 3 is a subordinate of 2 and has suitable rank). r(3, 1) = 3 (employees 2, 3, 4 suit the conditions). Thus z3 = r(3, 2) + r(3, 1) = 4. 
#include <bits/stdc++.h>
using namespace std;
struct edge {
  int to, next;
} e[1000005];
int head[1000005], dep[1000005], fa[1000005];
int top[1000005], sz[1000005], pos[1000005];
long long ans[1000005], s1[1000005], s2[1000005];
vector<int> v[1000005];
int tot, rt, n, T;
void adde(int x, int y) {
  e[++tot] = (edge){y, head[x]};
  head[x] = tot;
}
void dfs(int x) {
  v[dep[x]].push_back(x);
  sz[x] = 1;
  for (int i = head[x]; i; i = e[i].next) {
    fa[e[i].to] = x;
    dep[e[i].to] = dep[x] + 1;
    dfs(e[i].to);
    sz[x] += sz[e[i].to];
  }
}
void dfs2(int x, int tp) {
  pos[x] = ++T;
  top[x] = tp;
  int k = 0;
  for (int i = head[x]; i; i = e[i].next)
    if (sz[e[i].to] > sz[k]) k = e[i].to;
  if (k) dfs2(k, tp);
  for (int i = head[x]; i; i = e[i].next)
    if (e[i].to != k) dfs2(e[i].to, e[i].to);
}
void add(int x, int v) {
  for (int k = x; x <= n; x += x & (-x)) s1[x] += v, s2[x] += k * v;
}
long long sum(int x) {
  long long ans = 0;
  for (int k = x; k; k -= k & (-k)) ans += s1[k];
  ans *= (x + 1);
  for (int k = x; k; k -= k & (-k)) ans -= s2[k];
  return ans;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    if (x)
      adde(x, i);
    else
      rt = i;
  }
  dep[rt] = 1;
  dfs(rt);
  dfs2(rt, rt);
  for (int i = 2; i <= n; i++) {
    vector<int>::iterator it;
    for (it = v[i].begin(); it != v[i].end(); it++)
      for (int k = fa[*it]; k; k = fa[top[k]])
        add(pos[top[k]], 1), add(pos[k] + 1, -1);
    for (it = v[i].begin(); it != v[i].end(); it++)
      for (int k = fa[*it]; k; k = fa[top[k]])
        ans[*it] += sum(pos[k]) - sum(pos[top[k]] - 1);
  }
  for (int i = 1; i <= n; i++) printf("%I64d ", ans[i]);
}
