#include<cstdio>
#include<algorithm>
#include<vector>
#include<functional>
using namespace std;
static const int MAX_N = 10000;

int n;
vector<pair<int, int> > G[MAX_N];
vector<pair<int, int> > va[MAX_N];
int memo[MAX_N], memo2[MAX_N];

void dfs(int pv, int v){
	va[v].push_back(make_pair(0, v));
	for(int i = 0; i < G[v].size(); i++){
		if(G[v][i].first == pv) continue;
		dfs(v, G[v][i].first);
		memo[v] = max(memo[v], memo[G[v][i].first] + G[v][i].second);
		va[v].push_back(make_pair(memo[G[v][i].first] + G[v][i].second, G[v][i].first));
	}
}
void dfs2(int pv, int v){
	sort(va[v].begin(), va[v].end(), greater<pair<int, int> >());
	memo2[v] = max(memo2[v], va[v][0].first);
	for(int i = 0; i < G[v].size(); i++){
		if(G[v][i].first == pv) continue;
		if(va[v][0].second == G[v][i].first) va[G[v][i].first].push_back(make_pair(va[v][1].first + G[v][i].second, v));
		else va[G[v][i].first].push_back(make_pair(va[v][0].first + G[v][i].second, v));
		dfs2(v, G[v][i].first);
	}
}

int main(){
	scanf("%d", &n);
	for(int i = 0; i < n - 1; i++){
		int s, t, w;
		scanf("%d %d %d", &s, &t, &w);
		G[s].push_back(make_pair(t, w));
		G[t].push_back(make_pair(s, w));
	}
	dfs(-1, 0);
	dfs2(-1, 0);
	for(int i = 0; i < n; i++) printf("%d\n", memo2[i]);
	return 0;
}



### Prompt

Develop a solution in Cpp to the problem described below:

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<cstdio>
#include<algorithm>
#include<vector>
#include<functional>
using namespace std;
static const int MAX_N = 10000;

int n;
vector<pair<int, int> > G[MAX_N];
vector<pair<int, int> > va[MAX_N];
int memo[MAX_N], memo2[MAX_N];

void dfs(int pv, int v){
	va[v].push_back(make_pair(0, v));
	for(int i = 0; i < G[v].size(); i++){
		if(G[v][i].first == pv) continue;
		dfs(v, G[v][i].first);
		memo[v] = max(memo[v], memo[G[v][i].first] + G[v][i].second);
		va[v].push_back(make_pair(memo[G[v][i].first] + G[v][i].second, G[v][i].first));
	}
}
void dfs2(int pv, int v){
	sort(va[v].begin(), va[v].end(), greater<pair<int, int> >());
	memo2[v] = max(memo2[v], va[v][0].first);
	for(int i = 0; i < G[v].size(); i++){
		if(G[v][i].first == pv) continue;
		if(va[v][0].second == G[v][i].first) va[G[v][i].first].push_back(make_pair(va[v][1].first + G[v][i].second, v));
		else va[G[v][i].first].push_back(make_pair(va[v][0].first + G[v][i].second, v));
		dfs2(v, G[v][i].first);
	}
}

int main(){
	scanf("%d", &n);
	for(int i = 0; i < n - 1; i++){
		int s, t, w;
		scanf("%d %d %d", &s, &t, &w);
		G[s].push_back(make_pair(t, w));
		G[t].push_back(make_pair(s, w));
	}
	dfs(-1, 0);
	dfs2(-1, 0);
	for(int i = 0; i < n; i++) printf("%d\n", memo2[i]);
	return 0;
}



```