#abc369g. G - As far as possible

G - As far as possible

Score : 600600 points

问题陈述

你得到了一个有 NN 个顶点的树。顶点编号为 11, 22, \ldots, NN。 第 ii 条边(1iN11\leq i\leq N-1)连接顶点 UiU_iViV_i,长度为 LiL_i

对于每个 K=1,2,,NK=1,2,\ldots, N,解决以下问题。

高桥和青木在玩一个游戏。游戏进行如下。

  • 首先,青木在树上指定 KK 个不同的顶点。
  • 然后,高桥构建一条从顶点 11 开始并在顶点 11 结束的路径,并且通过青木指定的所有顶点。

分数定义为高桥构建的路径的长度。高桥想要最小化分数,而青木想要最大化它。当两个玩家都以最优方式玩时,找出分数。

路径的定义 在无向图(可能是树)上的路径是一系列 kk 个顶点和 k1k-1 条边 v1,e1,v2,,vk1,ek1,vkv_1,e_1,v_2,\ldots,v_{k-1},e_{k-1},v_k(其中 kk 是一个正整数),使得边 eie_i 连接顶点 viv_ivi+1v_{i+1}。同一个顶点或边可以在序列中多次出现。如果存在至少一个 ii1ik1\leq i\leq k)使得 vi=xv_i=x,则路径通过顶点 xx。(可以有多个这样的 ii。)路径分别从 v1v_1 开始并在 vkv_k 结束,路径的长度是 e1e_1, e2e_2, \ldots, ek1e_{k-1} 的长度之和。

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

You are given a tree with NN vertices. The vertices are numbered 11, 22, \ldots, NN.
The ii-th edge (1iN11\leq i\leq N-1) connects vertices UiU_i and ViV_i, with a length of LiL_i.

For each K=1,2,,NK=1,2,\ldots, N, solve the following problem.

Takahashi and Aoki play a game. The game proceeds as follows.

  • First, Aoki specifies KK distinct vertices on the tree.
  • Then, Takahashi constructs a walk that starts and ends at vertex 11, and passes through all the vertices specified by Aoki.

The score is defined as the length of the walk constructed by Takahashi. Takahashi wants to minimize the score, while Aoki wants to maximize it. Find the score when both players play optimally.

Definition of a walk A walk on an undirected graph (possibly a tree) is a sequence of kk vertices and k1k-1 edges v1,e1,v2,,vk1,ek1,vkv_1,e_1,v_2,\ldots,v_{k-1},e_{k-1},v_k (where kk is a positive integer) such that edge eie_i connects vertices viv_i and vi+1v_{i+1}. The same vertex or edge can appear multiple times in the sequence. A walk is said to pass through vertex xx if there exists at least one ii (1ik1\leq i\leq k) such that vi=xv_i=x. (There can be multiple such ii.) The walk is said to start and end at v1v_1 and vkv_k, respectively, and the length of the walk is the sum of the lengths of e1e_1, e2e_2, \ldots, ek1e_{k-1}.

Constraints

  • 2N2×1052\leq N\leq 2\times 10^5
  • 1Ui<ViN1\leq U_i<V_i\leq N
  • 1Li1091\leq L_i\leq 10^9
  • All input values are integers.
  • The given graph is a tree.

Input

The input is given from Standard Input in the following format:

NN

U1U_1 V1V_1 L1L_1

U2U_2 V2V_2 L2L_2

\vdots

UN1U_{N-1} VN1V_{N-1} LN1L_{N-1}

Output

Print NN lines. The ii-th line (1iN)(1\leq i\leq N) should contain the answer to the problem for K=iK=i.

Sample Input 1

5
1 2 3
2 3 5
2 4 2
1 5 3

Sample Output 1

16
22
26
26
26

For K=1K=1, Aoki's optimal move is to specify vertex 33, and Takahashi's optimal move is to construct a path vertex 11 \to vertex 22 \to vertex 33 \to vertex 22 \to vertex 11, resulting in a score of 1616.

For K=2K=2, Aoki's optimal move is to specify vertices 33 and 55, and Takahashi's optimal move is to construct a path such as vertex 11 \to vertex 55 \to vertex 11 \to vertex 22 \to vertex 33 \to vertex 22 \to vertex 11, resulting in a score of 2222.

For K3K\geq 3, the score when both players play optimally is 2626.

Sample Input 2

3
1 2 1000000000
2 3 1000000000

Sample Output 2

4000000000
4000000000
4000000000

Beware that the answer may not fit in a 3232-bit integer.