// ???????????????
#include<vector>
#include<tuple>
#include<algorithm>
// ???
typedef int Weight;
// ?????????
class Tree {
public:
  explicit Tree(int n);
  int size() const;
  void join(int u, int v, Weight w);
  Weight diameter() const;
  Weight height(int root) const;
  const std::vector<Weight> height() const;
  Weight height_dfs(int u, int v, std::vector<std::vector<Weight>>& cost) const;
private:
  struct Edge {int from, to; Weight weight;};
  struct Farthest {int vertex; Weight cost;};
  const Farthest farthest(int current, int previous) const;
  std::vector<std::vector<Edge>> edge_;
};
// ?????????????????????
Tree::Tree(int n) : edge_(n) {}
// ????????°
int Tree::size() const {return edge_.size();}
// ????????????
void Tree::join(int u, int v, Weight w) {
  edge_[u].push_back({u, v, w});
  edge_[v].push_back({v, u, w});
}
// ???????????¨?????¢
const Tree::Farthest Tree::farthest(int current, int previous) const {
  Farthest result({current, 0});
  for(const auto& e: edge_[current]) {
    if(e.to == previous) continue;
    auto f = farthest(e.to, e.from);
    f.cost += e.weight;
    if(result.cost < f.cost) result = f;
  }
  return result;
}
// ??´???
Weight Tree::diameter() const {
  int v = farthest(0, 0).vertex;
  return farthest(v, v).cost;
}
// ?????????????????????????????§????????¢
//   ?????????farthest????????¢???????????¨????????????
Weight Tree::height(int root) const {return farthest(root, root).cost;}
// ?????????????????????????????§????????¢
//   memo[v][e]: ??????v?????????e????????£?????¨????????????????????§????????¢
const std::vector<Weight> Tree::height() const {
  std::vector<std::vector<Weight>> memo(size());
  for(int v = 0; v < size(); ++v) for(int e = 0; e < edge_[v].size(); ++e) memo[v].push_back(-1);
  for(int v = 0; v < size(); ++v) for(int e = 0; e < edge_[v].size(); ++e) if(memo[v][e] == -1) height_dfs(v, e, memo);
  std::vector<Weight> result(size(), 0);
  for(int v = 0; v < size(); ++v) for(int e = 0; e < edge_[v].size(); ++e) result[v] = std::max(result[v], memo[v][e]);
  return result;
}
Weight Tree::height_dfs(int v, int e, std::vector<std::vector<Weight>>& memo) const {
  if(memo[v][e] != -1) return memo[v][e];
  memo[v][e] = edge_[v][e].weight;
  int next_v = edge_[v][e].to;
  for(int next_e = 0; next_e < edge_[next_v].size(); ++next_e)
    if(edge_[next_v][next_e].to != v)
      memo[v][e] = std::max(memo[v][e], edge_[v][e].weight + height_dfs(next_v, next_e, memo));
  return memo[v][e];
}

#include<bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;
  Tree t(n);
  for(int i = 0; i < n - 1; ++i) {
    int u, v, w;
    cin >> u >> v >> w;
    t.join(u, v, w);
  }
  for(const auto& i: t.height()) cout << i << endl;
}
### Prompt

Create a solution in Cpp for the following problem:

Given a tree T with non-negative weight, find the height of each node of the tree. For each node, the height is the distance to the most distant leaf from the node.

Constraints

* 1 ≤ n ≤ 10,000
* 0 ≤ wi ≤ 1,000

Input


n
s1 t1 w1
s2 t2 w2
:
sn-1 tn-1 wn-1


The first line consists of an integer n which represents the number of nodes in the tree. Every node has a unique ID from 0 to n-1 respectively.

In the following n-1 lines, edges of the tree are given. si and ti represent end-points of the i-th edge (undirected) and wi represents the weight (distance) of the i-th edge.

Output

The output consists of n lines. Print the height of each node 0, 1, 2, ..., n-1 in order.

Example

Input

4
0 1 2
1 2 1
1 3 3


Output

5
3
4
5

### Response

```cpp
// ???????????????
#include<vector>
#include<tuple>
#include<algorithm>
// ???
typedef int Weight;
// ?????????
class Tree {
public:
  explicit Tree(int n);
  int size() const;
  void join(int u, int v, Weight w);
  Weight diameter() const;
  Weight height(int root) const;
  const std::vector<Weight> height() const;
  Weight height_dfs(int u, int v, std::vector<std::vector<Weight>>& cost) const;
private:
  struct Edge {int from, to; Weight weight;};
  struct Farthest {int vertex; Weight cost;};
  const Farthest farthest(int current, int previous) const;
  std::vector<std::vector<Edge>> edge_;
};
// ?????????????????????
Tree::Tree(int n) : edge_(n) {}
// ????????°
int Tree::size() const {return edge_.size();}
// ????????????
void Tree::join(int u, int v, Weight w) {
  edge_[u].push_back({u, v, w});
  edge_[v].push_back({v, u, w});
}
// ???????????¨?????¢
const Tree::Farthest Tree::farthest(int current, int previous) const {
  Farthest result({current, 0});
  for(const auto& e: edge_[current]) {
    if(e.to == previous) continue;
    auto f = farthest(e.to, e.from);
    f.cost += e.weight;
    if(result.cost < f.cost) result = f;
  }
  return result;
}
// ??´???
Weight Tree::diameter() const {
  int v = farthest(0, 0).vertex;
  return farthest(v, v).cost;
}
// ?????????????????????????????§????????¢
//   ?????????farthest????????¢???????????¨????????????
Weight Tree::height(int root) const {return farthest(root, root).cost;}
// ?????????????????????????????§????????¢
//   memo[v][e]: ??????v?????????e????????£?????¨????????????????????§????????¢
const std::vector<Weight> Tree::height() const {
  std::vector<std::vector<Weight>> memo(size());
  for(int v = 0; v < size(); ++v) for(int e = 0; e < edge_[v].size(); ++e) memo[v].push_back(-1);
  for(int v = 0; v < size(); ++v) for(int e = 0; e < edge_[v].size(); ++e) if(memo[v][e] == -1) height_dfs(v, e, memo);
  std::vector<Weight> result(size(), 0);
  for(int v = 0; v < size(); ++v) for(int e = 0; e < edge_[v].size(); ++e) result[v] = std::max(result[v], memo[v][e]);
  return result;
}
Weight Tree::height_dfs(int v, int e, std::vector<std::vector<Weight>>& memo) const {
  if(memo[v][e] != -1) return memo[v][e];
  memo[v][e] = edge_[v][e].weight;
  int next_v = edge_[v][e].to;
  for(int next_e = 0; next_e < edge_[next_v].size(); ++next_e)
    if(edge_[next_v][next_e].to != v)
      memo[v][e] = std::max(memo[v][e], edge_[v][e].weight + height_dfs(next_v, next_e, memo));
  return memo[v][e];
}

#include<bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;
  Tree t(n);
  for(int i = 0; i < n - 1; ++i) {
    int u, v, w;
    cin >> u >> v >> w;
    t.join(u, v, w);
  }
  for(const auto& i: t.height()) cout << i << endl;
}
```