Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
整数 A,B が与えられます。
以下の条件を満たす整数 x が何通りあるか求めてください。
- 条件:3 つの整数 A,B,x をうまく並べることで、等差数列を作ることができる。
なお、3 つの整数 p,q,r をこの順に並べた列が等差数列であるとは、q-p が r-q と一致することをいいます。
制約
- 1\leq A,B \leq 100
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
A B
出力
問題文中の条件を満たす整数 x が何通りあるかを出力せよ。 なお、答えは有限であることが証明できる。
入力例 1
5 7
出力例 1
3
以下のように、x=3,6,9 はいずれも条件を満たします。
- x=3 のとき、例えば x,A,B と並べることで等差数列 3,5,7 を作ることができます。
- x=6 のとき、例えば B,x,A と並べることで等差数列 7,6,5 を作ることができます。
- x=9 のとき、例えば A,B,x と並べることで等差数列 5,7,9 を作ることができます。
逆に、これ以外に条件を満たす x は存在しません。 よって答えは 3 です。
入力例 2
6 1
出力例 2
2
x=-4,11 のみが条件を満たします。
入力例 3
3 3
出力例 3
1
x=3 のみが条件を満たします。
Score : 100 points
Problem Statement
You are given two integers A and B.
How many integers x satisfy the following condition?
- Condition: It is possible to arrange the three integers A, B, and x in some order to form an arithmetic sequence.
A sequence of three integers p, q, and r in this order is an arithmetic sequence if and only if q-p is equal to r-q.
Constraints
- 1 \leq A,B \leq 100
- All input values are integers.
Input
The input is given from Standard Input in the following format:
A B
Output
Print the number of integers x that satisfy the condition in the problem statement. It can be proved that the answer is finite.
Sample Input 1
5 7
Sample Output 1
3
The integers x=3,6,9 all satisfy the condition as follows:
- When x=3, for example, arranging x,A,B forms the arithmetic sequence 3,5,7.
- When x=6, for example, arranging B,x,A forms the arithmetic sequence 7,6,5.
- When x=9, for example, arranging A,B,x forms the arithmetic sequence 5,7,9.
Conversely, there are no other values of x that satisfy the condition. Therefore, the answer is 3.
Sample Input 2
6 1
Sample Output 2
2
Only x=-4 and 11 satisfy the condition.
Sample Input 3
3 3
Sample Output 3
1
Only x=3 satisfies the condition.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
高橋君は、横一列に並んだ 100 個の鍵盤からなるピアノを持っています。 このピアノの左から i 個目の鍵盤のことを鍵盤 i と呼びます。
高橋君は今から N 回にわたってこのピアノの鍵盤を一つずつ押すことでとある曲を演奏します。
i 回目に押す鍵盤は鍵盤 A_i であり、それを押す手は S_i= L のとき左手、S_i= R のとき右手です。
演奏を始める前、高橋君は両手をそれぞれ好きな鍵盤の上に置くことができ、この時点での疲労度は 0 です。 演奏中、片方の手を鍵盤 x の上から鍵盤 y の上へと動かすと疲労度が |y-x| 増加します(逆に、手の移動以外で疲労度が増加することはありません)。 なお、ある手である鍵盤を押すためには、その手がその鍵盤の上に置かれている必要があります。
演奏が終了した時点での疲労度は最小でいくつになるか求めてください。
制約
- 1\leq N \leq 100
- 1\leq A_i \leq 100
- N,A_i は整数
- S_i は
LまたはR
入力
入力は以下の形式で標準入力から与えられる。
N A_1 S_1 A_2 S_2 \vdots A_N S_N
出力
演奏が終了した時点での疲労度の最小値を出力せよ。
入力例 1
4 3 L 6 R 9 L 1 R
出力例 1
11
例えば以下のように演奏することができます。
- 最初、左手を鍵盤 3 の上に、右手を鍵盤 6 の上に置いておく。
- 左手で鍵盤 3 を押す。
- 右手で鍵盤 6 を押す。
- 左手を鍵盤 3 の上から鍵盤 9 の上へと動かす。疲労度が |9-3|=6 増加する。
- 右手を鍵盤 6 の上から鍵盤 1 の上へと動かす。疲労度が |1-6|=5 増加する。
- 左手で鍵盤 9 を押す。
- 右手で鍵盤 1 を押す。
このとき、演奏が終了した時点での疲労度は 6+5=11 であり、これが最小です。
入力例 2
3 2 L 2 L 100 L
出力例 2
98
入力例 3
8 22 L 75 L 26 R 45 R 72 R 81 R 47 L 29 R
出力例 3
188
Score : 200 points
Problem Statement
Takahashi has a piano with 100 keys arranged in a row. The i-th key from the left is called key i.
He will play music by pressing N keys one by one.
For the i-th press, he will press key A_i, using his left hand if S_i= L, and his right hand if S_i= R.
Before starting to play, he can place both of his hands on any keys he likes, and his fatigue level at this point is 0. During the performance, if he moves one hand from key x to key y, the fatigue level increases by |y-x| (conversely, the fatigue level does not increase for any reason other than moving hands). To press a certain key with a hand, that hand must be placed on that key.
Find the minimum possible fatigue level at the end of the performance.
Constraints
- 1 \leq N \leq 100
- 1 \leq A_i \leq 100
- N and A_i are integers.
- S_i is
LorR.
Input
The input is given from Standard Input in the following format:
N A_1 S_1 A_2 S_2 \vdots A_N S_N
Output
Print the minimum fatigue level at the end of the performance.
Sample Input 1
4 3 L 6 R 9 L 1 R
Sample Output 1
11
For example, the performance can be done as follows:
- Initially, place the left hand on key 3 and the right hand on key 6.
- Press key 3 with the left hand.
- Press key 6 with the right hand.
- Move the left hand from key 3 to key 9. The fatigue level increases by |9-3| = 6.
- Move the right hand from key 6 to key 1. The fatigue level increases by |1-6| = 5.
- Press key 9 with the left hand.
- Press key 1 with the right hand.
In this case, the fatigue level at the end of the performance is 6+5 = 11, which is the minimum possible.
Sample Input 2
3 2 L 2 L 100 L
Sample Output 2
98
Sample Input 3
8 22 L 75 L 26 R 45 R 72 R 81 R 47 L 29 R
Sample Output 3
188
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
長さ N の正整数列 A=(A_1,A_2,\dots,A_N) が与えられます。
1\leq l\leq r\leq N を満たす整数の組 (l,r) であって、数列 (A_l,A_{l+1},\dots,A_r) が等差数列であるようなものが何通りあるか求めてください。
なお、数列 (x_1,x_2,\dots,x_{|x|}) が等差数列であるとは、ある d が存在して x_{i+1}-x_i=d\ (1\leq i < |x|) であることをいいます。 特に、長さ 1 の数列は常に等差数列です。
制約
- 1\leq N \leq 2\times 10^5
- 1\leq A_i \leq 10^9
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N
出力
答えを出力せよ。
入力例 1
4 3 6 9 3
出力例 1
8
条件を満たす整数の組 (l,r) は (1,1),(2,2),(3,3),(4,4),(1,2),(2,3),(3,4),(1,3) の 8 通りです。
実際、(l,r)=(1,3) のとき (A_l,\dots,A_r)=(3,6,9) は等差数列なので条件を満たしますが、 (l,r)=(2,4) のとき (A_l,\dots,A_r)=(6,9,3) は等差数列ではないので条件を満たしません。
入力例 2
5 1 1 1 1 1
出力例 2
15
すべての整数の組 (l,r)\ (1\leq l\leq r\leq 5) が条件を満たします。
入力例 3
8 87 42 64 86 72 58 44 30
出力例 3
22
Score : 300 points
Problem Statement
You are given a sequence of N positive integers A=(A_1,A_2,\dots,A_N).
Find the number of pairs of integers (l,r) satisfying 1\leq l\leq r\leq N such that the subsequence (A_l,A_{l+1},\dots,A_r) forms an arithmetic progression.
A sequence (x_1,x_2,\dots,x_{|x|}) is an arithmetic progression if and only if there exists a d such that x_{i+1}-x_i=d\ (1\leq i < |x|). In particular, a sequence of length 1 is always an arithmetic progression.
Constraints
- 1\leq N \leq 2\times 10^5
- 1\leq A_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \dots A_N
Output
Print the answer.
Sample Input 1
4 3 6 9 3
Sample Output 1
8
There are eight pairs of integers (l,r) satisfying the condition: (1,1),(2,2),(3,3),(4,4),(1,2),(2,3),(3,4),(1,3).
Indeed, when (l,r)=(1,3), (A_l,\dots,A_r)=(3,6,9) is an arithmetic progression, so it satisfies the condition. However, when (l,r)=(2,4), (A_l,\dots,A_r)=(6,9,3) is not an arithmetic progression, so it does not satisfy the condition.
Sample Input 2
5 1 1 1 1 1
Sample Output 2
15
All pairs of integers (l,r)\ (1\leq l\leq r\leq 5) satisfy the condition.
Sample Input 3
8 87 42 64 86 72 58 44 30
Sample Output 3
22
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
高橋君は N 匹のモンスターに順に出会います。i 匹目 (1\leq i\leq N) のモンスターの強さは A_i です。
高橋君はそれぞれのモンスターについて逃がすか倒すか選ぶことができます。
高橋君はそれぞれの行動によって次のように経験値を得ます。
- モンスターを逃がした場合、得られる経験値は 0 である。
- 強さが X のモンスターを倒したとき、経験値を X 得る。
ただし、モンスターを倒すのが偶数回目(2, 4, \ldots 回目)であるとき、さらに追加で経験値を X 得る。
N 匹から高橋君が得た経験値の合計としてあり得る最大の値を求めてください。
制約
- 1\leq N\leq 2\times 10^5
- 1\leq A_i\leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N
出力
N 匹から高橋君が得た経験値の合計としてあり得る最大の値を整数で出力せよ。
入力例 1
5 1 5 3 2 7
出力例 1
28
1,2,3,5 匹目のモンスターを倒して、4 匹目のモンスターを逃したとき、高橋君は次のように経験値を得ます。
- 強さが A_1=1 のモンスターを倒す。高橋君は 1 の経験値を得る。
- 強さが A_2=5 のモンスターを倒す。高橋君は 5 の経験値を得る。モンスターを倒すのは 2 回目であるため、追加で経験値 5 を得る。
- 強さが A_3=3 のモンスターを倒す。高橋君は 3 の経験値を得る。
- 4 匹目のモンスターを逃がす。高橋君は経験値を得ない。
- 強さが A_5=7 のモンスターを倒す。高橋君は 7 の経験値を得る。モンスターを倒すのは 4 回目であるため、追加で経験値 7 を得る。
よって、このとき 1+(5+5)+3+0+(7+7)=28 の経験値を得ます。
出会ったモンスターであっても、逃がした場合は倒した回数には数えられないことに注意してください。
高橋君がどのように行動しても得られる経験値は 28 以下であるため、28 を出力します。
なお、この場合においてすべてのモンスターを倒した時に得られる経験値は 1+(5+5)+3+(2+2)+7=25 となります。
入力例 2
2 1000000000 1000000000
出力例 2
3000000000
答えが 32bit 整数型に収まらない可能性があることに注意してください。
Score : 400 points
Problem Statement
Takahashi will encounter N monsters in order. The i-th monster (1\leq i\leq N) has a strength of A_i.
For each monster, he can choose to either let it go or defeat it.
Each action awards him experience points as follows:
- If he lets a monster go, he gains 0 experience points.
- If he defeats a monster with strength X, he gains X experience points.
If it is an even-numbered defeated monster (2nd, 4th, ...), he gains an additional X experience points.
Find the maximum total experience points he can gain from the N monsters.
Constraints
- 1\leq N\leq 2\times 10^5
- 1\leq A_i\leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \ldots A_N
Output
Print the maximum total experience points he can gain from the N monsters as an integer.
Sample Input 1
5 1 5 3 2 7
Sample Output 1
28
If Takahashi defeats the 1st, 2nd, 3rd, and 5th monsters, and lets the 4th monster go, he gains experience points as follows:
- Defeats a monster with strength A_1=1. He gains 1 experience point.
- Defeats a monster with strength A_2=5. He gains 5 experience points. As it is the 2nd defeated monster, he gains an additional 5 points.
- Defeats a monster with strength A_3=3. He gains 3 experience points.
- Lets the 4th monster go. Takahashi gains no experience points.
- Defeats a monster with strength A_5=7. He gains 7 experience points. As it is the 4th defeated monster, he gains an additional 7 points.
Therefore, in this case, he gains 1+(5+5)+3+0+(7+7)=28 experience points.
Note that even if he encounters a monster, if he lets it go, it does not count as defeated.
He can gain at most 28 experience points no matter how he acts, so print 28.
As a side note, if he defeats all monsters in this case, he would gain 1+(5+5)+3+(2+2)+7=25 experience points.
Sample Input 2
2 1000000000 1000000000
Sample Output 2
3000000000
Beware that the answer may not fit in a 32-bit integer.
Time Limit: 4 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
N 個の島と、2 つの島の間を双方向に結ぶ M 本の橋があり、
それぞれ島 1, 島 2, \ldots, 島 N および 橋 1, 橋 2, \ldots, 橋 M と番号づけられています。
橋 i は島 U_i と島 V_i を相互に結んでおり、どちらの方向に移動するにも T_i だけ時間がかかります。
ここで、橋の両端が同一の島であるような橋は存在しませんが、ある 2 つの島の間が 2 本以上の橋で直接繋がれている可能性はあります。
また、どの 2 つの島の間もいくつかの橋をわたって移動することができます。
Q 個の問題が与えられるので、各問題に対する答えを求めてください。i 番目の問題は次のようなものです。
相異なる K_i 本の橋、橋 B_{i,1}, 橋 B_{i,2}, \ldots, 橋 B_{i,K_i} が与えられます。
これらの橋をすべて 1 回以上わたり、島 1 から島 N まで移動するために必要な時間の最小値を求めてください。
ただし、島 1 から島 N までの移動において、橋をわたって島の間を移動するのにかかる時間以外は無視できるものとします。
また、与えられた橋はどの順で、またどの向きにわたってもかまいません。
制約
- 2\leq N \leq 400
- N-1\leq M \leq 2\times 10^5
- 1\leq U_i<V_i\leq N
- 1\leq T_i\leq 10^9
- 1\leq Q\leq 3000
- 1\leq K_i\leq 5
- 1\leq B_{i,1}<B_{i,2}<\cdots<B_{i,K_i}\leq M
- 入力はすべて整数
- どの 2 つの島の間もいくつかの橋をわたって移動することができる。
入力
入力は以下の形式で標準入力から与えられる。
N M
U_1 V_1 T_1
U_2 V_2 T_2
\vdots
U_M V_M T_M
Q
K_1
B_{1,1} B_{1,2} \cdots B_{1,{K_1}}
K_2
B_{2,1} B_{2,2} \cdots B_{2,{K_2}}
\vdots
K_Q
B_{Q,1} B_{Q,2} \cdots B_{Q,{K_Q}}
出力
Q 行出力せよ。i 行目(1\leq i\leq Q)には i 番目の問題に対する答えを整数で出力せよ。
入力例 1
3 5 1 2 10 1 3 20 1 3 30 2 3 15 2 3 25 2 1 1 2 3 5
出力例 1
25 70
1 番目の問題では橋 1 をわたった上で島 1 から島 3 へ移動するために必要な時間の最小値を求める必要があります。
橋 1 を使って島 1 から島 2 に移動した後に橋 4 を使って島 2 から島 3 に移動したとき時間は 10+15=25 だけかかり、このときが最小です。
よって、1 行目には 25 を出力します。
2 番目の問題では橋 3 および橋 5 をわたった上で島 1 から島 3 へ移動するために必要な時間の最小値を求める必要があります。
橋 3 を通って島 1 から島 3 に移動した後に橋 5 を使って島 2 へ移動し、橋 4 を使用して島 3 に移動したとき時間は 30+25+15=70 だけかかり、このときが最小です。よって、2 行目には 70 を出力します。
入力例 2
6 6 1 5 1 2 5 1 2 4 1 3 4 1 3 6 1 1 6 1 2 5 1 2 3 4 5 1 5
出力例 2
5 3
各問題において指定された橋をわたる際、わたる方向はどちらでも問題ありません。
入力例 3
5 5 1 2 1000000000 2 3 1000000000 3 4 1000000000 4 5 1000000000 1 5 1000000000 1 1 3
出力例 3
4000000000
答えが 32bit 整数型に収まらない可能性があることに注意してください。
Score : 450 points
Problem Statement
There are N islands and M bidirectional bridges connecting two islands. The islands and bridges are numbered 1, 2, \ldots, N and 1, 2, \ldots, M, respectively.
Bridge i connects islands U_i and V_i, and the time it takes to cross it in either direction is T_i.
No bridge connects an island to itself, but it is possible for two islands to be directly connected by more than one bridge.
One can travel between any two islands using some bridges.
You are given Q queries, so answer each of them. The i-th query is as follows:
You are given K_i distinct bridges: bridges B_{i,1}, B_{i,2}, \ldots, B_{i,K_i}.
Find the minimum time required to travel from island 1 to island N using each of these bridges at least once.
Only consider the time spent crossing bridges.
You can cross the given bridges in any order and in any direction.
Constraints
- 2 \leq N \leq 400
- N-1 \leq M \leq 2 \times 10^5
- 1 \leq U_i < V_i \leq N
- 1 \leq T_i \leq 10^9
- 1 \leq Q \leq 3000
- 1 \leq K_i \leq 5
- 1 \leq B_{i,1} < B_{i,2} < \cdots < B_{i,K_i} \leq M
- All input values are integers.
- It is possible to travel between any two islands using some bridges.
Input
The input is given from Standard Input in the following format:
N M
U_1 V_1 T_1
U_2 V_2 T_2
\vdots
U_M V_M T_M
Q
K_1
B_{1,1} B_{1,2} \cdots B_{1,{K_1}}
K_2
B_{2,1} B_{2,2} \cdots B_{2,{K_2}}
\vdots
K_Q
B_{Q,1} B_{Q,2} \cdots B_{Q,{K_Q}}
Output
Print Q lines. The i-th line (1 \leq i \leq Q) should contain the answer to the i-th query as an integer.
Sample Input 1
3 5 1 2 10 1 3 20 1 3 30 2 3 15 2 3 25 2 1 1 2 3 5
Sample Output 1
25 70
For the first query, we need to find the minimum time to travel from island 1 to island 3 while using bridge 1. The minimum time is achieved by using bridge 1 to move from island 1 to island 2, then using bridge 4 to move from island 2 to island 3. The time taken is 10 + 15 = 25. Hence, print 25 on the first line.
For the second query, we need to find the minimum time to travel from island 1 to island 3 while using both bridges 3 and 5. The minimum time is achieved by using bridge 3 to move from island 1 to island 3, then using bridge 5 to move to island 2, and finally using bridge 4 to return to island 3. The time taken is 30 + 25 + 15 = 70. Hence, print 70 on the second line.
Sample Input 2
6 6 1 5 1 2 5 1 2 4 1 3 4 1 3 6 1 1 6 1 2 5 1 2 3 4 5 1 5
Sample Output 2
5 3
For each query, you can cross the specified bridges in either direction.
Sample Input 3
5 5 1 2 1000000000 2 3 1000000000 3 4 1000000000 4 5 1000000000 1 5 1000000000 1 1 3
Sample Output 3
4000000000
Beware that the answer may not fit in a 32-bit integer.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
縦 H 行、横 W 列のグリッドがあります。 このグリッドの上から i 行目、左から j 列目にあるマスのことを (i,j) と表記します。
このグリッド上には N 枚のコインが落ちており、i 個目のコインはマス (R_i,C_i) を通ることで拾うことができます。
あなたの目標は、マス (1,1) から始めて下か右に 1 マス移動することを繰り返し、できるだけ多くのコインを拾いながらマス (H,W) まで行くことです。
あなたが拾うことのできるコインの枚数の最大値、およびそれを達成するための移動経路を 1 つ求めてください。
制約
- 2\leq H,W \leq 2\times 10^5
- 1\leq N \leq \min(HW-2, 2\times 10^5)
- 1\leq R_i \leq H
- 1\leq C_i \leq W
- (R_i,C_i)\neq (1,1)
- (R_i,C_i)\neq (H,W)
- (R_i,C_i) は互いに相異なる
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
H W N R_1 C_1 R_2 C_2 \vdots R_N C_N
出力
2 行出力せよ。
1 行目には、あなたが拾うことのできるコインの枚数の最大値を出力せよ。
2 行目には、それを達成するための移動経路の 1 つを長さ H+W-2 の文字列として出力せよ。
ここで、出力する文字列の i 文字目は、i 回目の移動において下に移動するならば D、右に移動するならば R である。
拾うコインの枚数が最大となるような移動経路が複数存在する場合は、そのどれを出力しても良い。
入力例 1
3 4 4 3 3 2 1 2 3 1 4
出力例 1
3 DRRDR

上図のように (1,1)\rightarrow (2,1)\rightarrow (2,2)\rightarrow (2,3)\rightarrow (3,3)\rightarrow (3,4) と移動することで、(2,1),(2,3),(3,3) にある 3 枚のコインを拾うことができます。
入力例 2
2 2 2 2 1 1 2
出力例 2
1 DR
RD という移動経路も正解となります。
入力例 3
10 15 8 2 7 2 9 7 9 10 3 7 11 8 12 9 6 8 1
出力例 3
5 DRRRRRRRRDDDDDRRDRDDRRR
Score : 500 points
Problem Statement
There is a grid with H rows and W columns. Let (i,j) denote the cell at the i-th row from the top and j-th column from the left.
There are N coins on this grid, and the i-th coin can be picked up by passing through the cell (R_i,C_i).
Your goal is to start from cell (1,1), repeatedly move either down or right by one cell, and reach cell (H,W) while picking up as many coins as possible.
Find the maximum number of coins you can pick up and one of the paths that achieves this maximum.
Constraints
- 2\leq H,W \leq 2\times 10^5
- 1\leq N \leq \min(HW-2, 2\times 10^5)
- 1\leq R_i \leq H
- 1\leq C_i \leq W
- (R_i,C_i)\neq (1,1)
- (R_i,C_i)\neq (H,W)
- (R_i,C_i) are pairwise distinct.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
H W N R_1 C_1 R_2 C_2 \vdots R_N C_N
Output
Print two lines.
The first line should contain the maximum number of coins you can pick up.
The second line should contain one of the paths that achieves this maximum as a string of length H+W-2.
The i-th character of this string should be D if the i-th move is downward, and R if it is rightward.
If there are multiple paths that maximize the number of coins picked up, you may print any of them.
Sample Input 1
3 4 4 3 3 2 1 2 3 1 4
Sample Output 1
3 DRRDR

As shown in the figure above, by moving (1,1)\rightarrow (2,1)\rightarrow (2,2)\rightarrow (2,3)\rightarrow (3,3)\rightarrow (3,4), you can pick up three coins at (2,1),(2,3),(3,3).
Sample Input 2
2 2 2 2 1 1 2
Sample Output 2
1 DR
The path RD is also acceptable.
Sample Input 3
10 15 8 2 7 2 9 7 9 10 3 7 11 8 12 9 6 8 1
Sample Output 3
5 DRRRRRRRRDDDDDRRDRDDRRR
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 600 点
問題文
N 頂点からなる木が与えられます。
頂点は頂点 1, 頂点 2, \ldots, 頂点 N と番号づけられています。
また、i 番目( 1\leq i\leq N-1 )の辺は頂点 U_i と頂点 V_i を結んでおり、長さは L_i です。
K=1,2,\ldots, N について次の問題を解いてください。
高橋君と青木君がゲームをします。ゲームは次のように行われます。
- まず、青木君が木上の相異なる K 個の頂点を指定します。
- 次に、高橋君は始点と終点がともに頂点 1 であるような歩道であって、青木君が指定した頂点をすべて通るようなものを構成します。
高橋君が構成した歩道の長さをスコアと定義します。高橋君はスコアをなるべく小さく、青木君はスコアをなるべく大きくしたいです。 2 人が最善に行動したときのスコアを求めてください。
歩道とは
無向グラフ(木を含む)上の歩道とは、k 個 (k は正整数) の頂点と k-1 個の辺を交互に並べた列 v_1,e_1,v_2,\ldots,v_{k-1},e_{k-1},v_k であって、 辺 e_i が頂点 v_i と頂点 v_{i+1} を結んでいるようなものを指す。列の中に同じ頂点や同じ辺が何回登場しても良い。 歩道が頂点 x を通るとは、v_i=x となるような 1\leq i\leq k が 1 つ以上存在することをいう。(複数個存在しても良い。) また、歩道の始点、終点はそれぞれ v_1, v_k のことをさし、歩道の長さとは e_1, e_2, \ldots, e_{k-1} の長さの総和を表す。制約
- 2\leq N\leq 2\times 10^5
- 1\leq U_i<V_i\leq N
- 1\leq L_i\leq 10^9
- 入力はすべて整数
- 与えられるグラフは木である。
入力
入力は以下の形式で標準入力から与えられる。
N
U_1 V_1 L_1
U_2 V_2 L_2
\vdots
U_{N-1} V_{N-1} L_{N-1}
出力
N 行出力せよ。 i 行目 (1\leq i\leq N) には K=i のときの問題の答えを出力せよ。
入力例 1
5 1 2 3 2 3 5 2 4 2 1 5 3
出力例 1
16 22 26 26 26
K=1 のとき青木君は頂点 3 を指定するのが最善で、このとき高橋君は頂点 1 \to 頂点 2 \to 頂点 3 \to 頂点 2 \to 頂点 1 の歩道を構成しスコアを 16 とするのが最善となります。
K=2 のとき青木君は頂点 3,5 を指定するのが最善で、このとき高橋君は頂点 1 \to 頂点 5 \to 頂点 1 \to 頂点 2 \to 頂点 3 \to 頂点 2 \to 頂点 1 などの歩道を構成しスコアを 22 とするのが最善となります。
K\geq 3 のとき、両者が最善を尽くしたときのスコアは 26 となります。
入力例 2
3 1 2 1000000000 2 3 1000000000
出力例 2
4000000000 4000000000 4000000000
答えが 32bit 整数型に収まらない可能性があることに注意してください。
Score : 600 points
Problem Statement
You are given a tree with N vertices.
The vertices are numbered 1, 2, \ldots, N.
The i-th edge (1\leq i\leq N-1) connects vertices U_i and V_i, with a length of L_i.
For each K=1,2,\ldots, N, solve the following problem.
Takahashi and Aoki play a game. The game proceeds as follows.
- First, Aoki specifies K distinct vertices on the tree.
- Then, Takahashi constructs a walk that starts and ends at vertex 1, 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 k vertices and k-1 edges v_1,e_1,v_2,\ldots,v_{k-1},e_{k-1},v_k (where k is a positive integer) such that edge e_i connects vertices v_i and v_{i+1}. The same vertex or edge can appear multiple times in the sequence. A walk is said to pass through vertex x if there exists at least one i (1\leq i\leq k) such that v_i=x. (There can be multiple such i.) The walk is said to start and end at v_1 and v_k, respectively, and the length of the walk is the sum of the lengths of e_1, e_2, \ldots, e_{k-1}.Constraints
- 2\leq N\leq 2\times 10^5
- 1\leq U_i<V_i\leq N
- 1\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:
N
U_1 V_1 L_1
U_2 V_2 L_2
\vdots
U_{N-1} V_{N-1} L_{N-1}
Output
Print N lines. The i-th line (1\leq i\leq N) should contain the answer to the problem for K=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=1, Aoki's optimal move is to specify vertex 3, and Takahashi's optimal move is to construct a path vertex 1 \to vertex 2 \to vertex 3 \to vertex 2 \to vertex 1, resulting in a score of 16.
For K=2, Aoki's optimal move is to specify vertices 3 and 5, and Takahashi's optimal move is to construct a path such as vertex 1 \to vertex 5 \to vertex 1 \to vertex 2 \to vertex 3 \to vertex 2 \to vertex 1, resulting in a score of 22.
For K\geq 3, the score when both players play optimally is 26.
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 32-bit integer.