A - Elevator

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 99

注意

この問題に対する言及は、2020年5月2日 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。

試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

下から順に B9, B8, ..., B1, 1F, 2F, ..., 9F と呼ばれる 1818 のフロアを持つ建物があります。

この建物のエレベーターは、隣接する 22 つのフロア間の移動に常に 11 秒を要します。例えば、B9 から 9F への移動には 1717 秒を要し、反対方向も同様です。

2 つのフロア SS, TT が与えられます。エレベーターが SS から TT へ移動するには最短で何秒を要するでしょうか。

制約

  • SS, TT はいずれも B1,B2,B3,B4,B5,B6,B7,B8,B9,1F,2F,3F,4F,5F,6F,7F,8F,9F のいずれかである
  • SSTT は等しくない

入力

入力は以下の形式で標準入力から与えられる。

SS TT

出力

SS から TT への移動に要する最短秒数を表す整数を出力せよ。


入力例 1

1F 5F

出力例 1

4

1F から 5F への移動には 44 秒を要します。


入力例 2

B1 B7

出力例 2

6

B1 から B7 への移動には 66 秒を要します。


入力例 3

1F B1

出力例 3

1

1F と B1 は隣接しているため、移動に 11 秒を要します。


入力例 4

B9 9F

出力例 4

17

B9 から 9F への移動には 1717 秒を要します。

Score : 99 points

Warning

Do not make any mention of this problem until May 2, 2020, 6:00 p.m. JST. In case of violation, compensation may be demanded.

After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

There is a building with 1818 floors called B9, B8, ..., B1, 1F, 2F, ..., 9F.

The elevator in this building always takes one second to travel between two adjacent floors. For example, it takes 1717 seconds to travel from B9 to 9F or vice versa.

Given two floors SS and TT, find the shortest time required for the elevator to travel from SS and TT.

Constraints

  • Each of SS and TT is B1, B2, B3, B4, B5, B6, B7, B8, B9, 1F, 2F, 3F, 4F, 5F, 6F, 7F, 8F, or 9F.
  • SS and TT are not equal.

Input

Input is given from Standard Input in the following format:

SS TT

Output

Print an integer representing the shortest time required to travel from SS to TT.


Sample Input 1

1F 5F

Sample Output 1

4

It takes 44 seconds to travel from 1F to 5F.


Sample Input 2

B1 B7

Sample Output 2

6

It takes 66 seconds to travel from B1 to B7.


Sample Input 3

1F B1

Sample Output 3

1

1F and B1 are adjacent, and it takes 11 second to travel between them.


Sample Input 4

B9 9F

Sample Output 4

17

It takes 1717 second to travel from B9 to 9F.

B - Plurality Voting

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 88

注意

この問題に対する言及は、2020年5月2日 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。

試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

ある国で選挙が行われました。候補者は a, b, c の 33 名です。

投票されたすべての票を表す文字列 SS が与えられます。SS は英小文字 a, b, c からなり、SSii 文字目は ii 番目の投票者が投票した候補者を表します。最多得票を得た候補者は誰か求めてください。

なお、最多得票を得た人物はちょうど 1 名であることが保証されています。

制約

  • 1S10001 \leq |S| \leq 1000
  • SSa, b, c からなる文字列である
  • 最多得票を得た人物はちょうど 1 名

入力

入力は以下の形式で標準入力から与えられる。

SS

出力

最多得票を得た候補者を表す英小文字を出力せよ。


入力例 1

abbc

出力例 1

b

候補者 a が 1 票、b が 2 票、c が 1 票を得ました。最多得票者は b です。


入力例 2

cacca

出力例 2

c

候補者 b は 1 票も得られませんでした。


入力例 3

b

出力例 3

b

投票を行ったのは 1 人だけで、その人は b に投票したため、最多得票者は b です。


入力例 4

babababacaca

出力例 4

a

Score : 88 points

Warning

Do not make any mention of this problem until May 2, 2020, 6:00 p.m. JST. In case of violation, compensation may be demanded.

After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

A country held an election. There were three candidates: a, b, and c.

Given is a string SS representing all the votes cast. SS consists of the lowercase English letters a, b, and c, and the ii-th character of SS represents the candidate the ii-th voter voted for. Find the candidate who received the most votes. It is guaranteed that there was exactly one candidate who received the most votes.

Constraints

  • 1S10001 \leq |S| \leq 1000
  • SS consists of a, b, and c.
  • There was exactly one candidate who received the most votes.

Input

Input is given from Standard Input in the following format:

SS

Output

Print a lowercase English letter representing the candidate with the most votes.


Sample Input 1

abbc

Sample Output 1

b

The candidates a, b, and c received 11, 22, and 11 vote(s), respectively. Thus, b received the most votes.


Sample Input 2

cacca

Sample Output 2

c

The candidate b received no votes.


Sample Input 3

b

Sample Output 3

b

There was only one voter, and that person voted for b. Thus, b received the most votes.


Sample Input 4

babababacaca

Sample Output 4

a
C - Landslide

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 88

注意

この問題に対する言及は、2020年5月2日 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。

試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

整数 2N502 \leq N \leq 50 に対して、縦 NN マス、横 2N12N - 1 マスのマス目があります。

上から ii 行目、左から jj 列目のマスをマス (i,j)(i, j) と表します。

このマス目は白黒の 22 色で 山型 に塗られています。具体的には、jN<i|j - N| < i を満たす 1iN,1j(2N1)1 \leq i \leq N, 1 \leq j \leq (2N - 1) についてはマス (i,j)(i, j) は黒く塗られており、他のマスは白く塗られています。

下は N=5N = 5 の場合の例です。(# は黒く塗られたマス、. は白く塗られたマスを表します)

....#....
...###...
..#####..
.#######.
#########

ここで、黒く塗られたマスのうちいくつかに X を書きます。

書き込んだ後の状態は文字からなるサイズ N×(2N1)N \times (2N - 1)22 次元配列 SS として与えられ、Si,jS_{i, j} がマス (i,j)(i, j) の状態を表します。Si,jS_{i, j}X のとき、マス (i,j)(i, j)X の書かれた黒く塗られたマスであり、Si,jS_{i, j}# のとき、マス (i,j)(i, j)X の書かれていない黒く塗られたマスであり、Si,jS_{i, j}. のとき、マス (i,j)(i, j) は白く塗られたマスです。

それから、i=N1,N2,,1i = N - 1, N - 2, \cdots, 1 の順番で次の操作を行います。

  • 2j2N22 \leq j \leq 2N - 2 に対して、マス (i,j)(i, j) が黒く塗られているが X は書かれておらず、マス (i+1,j1),(i+1,j),(i+1,j+1)(i+1, j-1), (i+1, j), (i+1, j+1) のうち 11 つ以上に X が書かれているとき、マス (i,j)(i, j)X を書く。

全ての操作を終えた後のマス目の状態を計算してください。

制約

  • 2N502 \leq N \leq 50
  • Si,jS_{i, j}. または # または X
  • SS に対応するマス目の状態は、山型に塗られたマス目の黒く塗られたマスのうち 11 つ以上に X を書くことで得られる

入力

入力は以下の形式で標準入力から与えられる。

NN
S1,1Si,2S1,2N1S_{1,1}S_{i,2}\cdots S_{1,2N-1}
S2,1S2,2S2,2N1S_{2,1}S_{2,2}\cdots S_{2,2N-1}
::
SN,1SN,2SN,2N1S_{N,1}S_{N,2}\cdots S_{N,2N-1}

出力

全ての操作を終えた後のマス (i,j)(i, j) の状態を Ti,jT_{i,j} (黒かつ X が書かれているとき X、黒かつ X が書かれていないとき #、白のとき .) としたとき、以下の形式で出力せよ。

T1,1Ti,2T1,2N1T_{1,1}T_{i,2}\cdots T_{1,2N-1}
T2,1T2,2T2,2N1T_{2,1}T_{2,2}\cdots T_{2,2N-1}
::
TN,1TN,2TN,2N1T_{N,1}T_{N,2}\cdots T_{N,2N-1}

入力例 1

5
....#....
...##X...
..#####..
.#X#####.
#########

出力例 1

....X....
...XXX...
..XX###..
.#X#####.
#########

入力例 2

2
.#.
#X#

出力例 2

.X.
#X#

入力例 3

10
.........#.........
........###........
.......#####.......
......#######......
.....#########.....
....###########....
...#############...
..###############..
.#################.
X#X########X#X####X

出力例 3

.........X.........
........XXX........
.......XXXXX.......
......XXXXXXX......
.....XXXXXXXXX.....
....XXXXXXXXXXX....
...XXX##XXXXXXXX...
..XXX####XXXXXXXX..
.XXX######XXXXX##X.
X#X########X#X####X

Score : 88 points

Warning

Do not make any mention of this problem until May 2, 2020, 6:00 p.m. JST. In case of violation, compensation may be demanded.

After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

We have an N×(2N1)N \times (2N - 1) grid (NN vertical, 2N12N - 1 horizontal) for an integer 1N501 \leq N \leq 50.

Let (i,j)(i, j) denote the square at the ii-th row from the top and jj-th column from the left.

The squares are painted black and white so that the black part looks like a mountain. More specifically, for 1iN1 \leq i \leq N and 1j2N11 \leq j \leq 2N - 1 such that jN<i|j - N| < i, (i,j)(i, j) is painted black, and the other squares are painted white.

Below is the grid for the case N=5N = 5. (# and . stand for a black square and a white square, respectively.)

....#....
...###...
..#####..
.#######.
#########

Now, we will write an X in some of the black squares.

The grid after this operation is given as a two-dimensional array of characters SS of size N×(2N1)N \times (2N - 1), where Si,jS_{i, j} corresponds to the square (i,j)(i, j). If Si,jS_{i, j} is X, (i,j)(i, j) is a black square with an X written on it; if Si,jS_{i, j} is #, (i,j)(i, j) is a black square without an X written on it; if Si,jS_{i, j} is ., (i,j)(i, j) is a white square.

Then, for each i=N1,N2,,1i = N - 1, N - 2, \cdots, 1 in this order, we will do the following operation:

  • For each 2j2N22 \leq j \leq 2N - 2, if (i,j)(i, j) is painted black, has no X written on it, and at least one of the squares (i+1,j1)(i+1, j-1), (i+1,j)(i+1, j), and (i+1,j+1)(i+1, j+1) has X written in it, write X in (i,j)(i, j)

Compute the final state of the grid after all the operations.

Constraints

  • 2N502 \leq N \leq 50
  • Si,jS_{i, j} is ., #, or X.
  • The state of the grid corresponding to SS can be obtained by writing an X in one or more black squares in a grid painted black and white as described in Problem Statement.

Input

Input is given from Standard Input in the following format:

NN
S1,1Si,2S1,2N1S_{1,1}S_{i,2}\cdots S_{1,2N-1}
S2,1S2,2S2,2N1S_{2,1}S_{2,2}\cdots S_{2,2N-1}
::
SN,1SN,2SN,2N1S_{N,1}S_{N,2}\cdots S_{N,2N-1}

Output

Output in the following format, where Ti,jT_{i,j} is the character representing the state of the square (i,j)(i, j) after all the operations. (Ti,jT_{i,j} should be X if (i,j)(i, j) is black and has an X written on it, # if (i,j)(i, j) is black and has no X written on it, and . if (i,j)(i, j) is white.)

T1,1Ti,2T1,2N1T_{1,1}T_{i,2}\cdots T_{1,2N-1}
T2,1T2,2T2,2N1T_{2,1}T_{2,2}\cdots T_{2,2N-1}
::
TN,1TN,2TN,2N1T_{N,1}T_{N,2}\cdots T_{N,2N-1}

Sample Input 1

5
....#....
...##X...
..#####..
.#X#####.
#########

Sample Output 1

....X....
...XXX...
..XX###..
.#X#####.
#########

Sample Input 2

2
.#.
#X#

Sample Output 2

.X.
#X#

Sample Input 3

10
.........#.........
........###........
.......#####.......
......#######......
.....#########.....
....###########....
...#############...
..###############..
.#################.
X#X########X#X####X

Sample Output 3

.........X.........
........XXX........
.......XXXXX.......
......XXXXXXX......
.....XXXXXXXXX.....
....XXXXXXXXXXX....
...XXX##XXXXXXXX...
..XXX####XXXXXXXX..
.XXX######XXXXX##X.
X#X########X#X####X
D - String Match

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 77

注意

この問題に対する言及は、2020年5月2日 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。

試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

英小文字から成る文字列 SS が与えられます。

英小文字または . から成る長さ 11 以上 33 以下の文字列 TTSS にマッチするとは、次の条件を満たすことを表します。

  • TT の長さを KK とする。 SS の連続する KK 文字であって、TT. を自由にそれぞれ英小文字 11 文字に置き換えることで一致するような部分が存在する。

英小文字または . から成る長さ 11 以上 33 以下の文字列全てのうち、SS にマッチするものの個数を求めてください。

制約

  • 1S1001 \leq |S| \leq 100
  • SS は英小文字から成る

入力

入力は以下の形式で標準入力から与えられる。

SS

出力

英小文字または . から成る長さ 11 以上 33 以下の文字列全てのうち、SS にマッチするものの個数を出力せよ。


入力例 1

ab

出力例 1

7

SS にマッチするような文字列としては、以下に挙げるように a, b, ., .., .b, a., ab77 通りが考えられます。

  • aSS11 文字目と一致する。
  • bSS22 文字目と一致する。
  • . は、例えば .a に置き換えることでSS11 文字目と一致する。
  • .. は、ab に置き換えることで SS1,21, 2 文字目と一致する。
  • .b は、ab に置き換えることで SS1,21, 2 文字目と一致する。
  • a. は、ab に置き換えることで SS1,21, 2 文字目と一致する。
  • ab は、SS1,21, 2 文字目と一致する。

入力例 2

aa

出力例 2

6

SS にマッチするような文字列としては、a, ., .., .a, a., aa66 通りが考えられます。


入力例 3

aabbaabb

出力例 3

33

Score : 77 points

Warning

Do not make any mention of this problem until May 2, 2020, 6:00 p.m. JST. In case of violation, compensation may be demanded.

After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

Given is a string SS consisting of lowercase English letters.

A non-empty string TT of length at most 33 consisting of lowercase English letters and periods (.) is said to match SS if and only if the following holds:

  • Let KK be the length of TT. SS has a contiguous substring of length KK that can be equal to TT after replacing each period in TT with one lowercase English letter.

Among all non-empty strings of length at most 33 consisting of lowercase English letters and periods, how many match SS?

Constraints

  • 1S1001 \leq |S| \leq 100
  • SS consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

SS

Output

Print the number of non-empty strings that match SS among all strings of length at most 33 consisting of lowercase English letters and periods.


Sample Input 1

ab

Sample Output 1

7

Seven strings match SS: a, b, ., .., .b, a., and ab, as follows:

  • a: coincides with the 11-st character of SS.
  • b: coincides with the 22-nd character of SS.
  • .: coincides with the 11-st character of SS after replacing the period with a, for example.
  • ..: coincides with the 11-st and 22-nd characters of SS after changing it to ab.
  • .b: coincides with the 11-st and 22-nd characters of SS after changing it to ab.
  • a.: coincides with the 11-st and 22-nd characters of SS after changing it to ab.
  • ab: coincides with the 11-st and 22-nd characters of SS.

Sample Input 2

aa

Sample Output 2

6

Six strings match SS: a, ., .., .a, a., and aa.


Sample Input 3

aabbaabb

Sample Output 3

33
E - Permutation

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 77

注意

この問題に対する言及は、2020年5月2日 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。

試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

1,2,,N1, 2, \ldots, N の順列 A1,A2,,ANA_1, A_2, \ldots, A_N が与えられます。

各整数 1iN1 \leq i \leq N に対して、次の条件を満たす 11 以上の整数 jj として考えられる最小の値を求めてください。

  • x=ix = i とする。xxAxA_x で置き換えるという操作をちょうど jj 回行った後、x=ix = i となる。

制約

  • 1N1001 \leq N \leq 100
  • 1AiN1 \leq A_i \leq N
  • AiAj(ij)A_i \neq A_j (i \neq j)
  • 入力は全て整数

入力

入力は以下の形式で標準入力から与えられる。

NN
A1A_1 A2A_2 \ldots ANA_N

出力

1,2,,N1, 2, \ldots, N のそれぞれに対して、この順に、問題文中の条件を満たす 11 以上の整数 jj として考えられる最小の値を出力せよ。


入力例 1

6
1 3 2 5 6 4

出力例 1

1 2 2 3 3 3
  • i=1i = 1 のとき A1=1A_1 = 1 であるので、j=1j = 1 が条件を満たす最小の値です。
  • i=2i = 2 のとき A2=3,A3=2A_2 = 3, A_3 = 2 であるので、j=2j = 2 が条件を満たす最小の値です。
  • i=3i = 3 のとき A3=2,A2=3A_3 = 2, A_2 = 3 であるので、j=2j = 2 が条件を満たす最小の値です。
  • i=4i = 4 のとき A4=5,A5=6,A6=4A_4 = 5, A_5 = 6, A_6 = 4 であるので、j=3j = 3 が条件を満たす最小の値です。
  • i=5i = 5 のとき A5=6,A6=4,A4=5A_5 = 6, A_6 = 4, A_4 = 5 であるので、j=3j = 3 が条件を満たす最小の値です。
  • i=6i = 6 のとき A6=4,A4=5,A5=6A_6 = 4, A_4 = 5, A_5 = 6 であるので、j=3j = 3 が条件を満たす最小の値です。

入力例 2

3
3 2 1

出力例 2

2 1 2

入力例 3

2
1 2

出力例 3

1 1

Score : 77 points

Warning

Do not make any mention of this problem until May 2, 2020, 6:00 p.m. JST. In case of violation, compensation may be demanded.

After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

Given is a permutation A1,A2,,ANA_1, A_2, \ldots, A_N of 1,2,,N1, 2, \ldots, N.

For each integer 1iN1 \leq i \leq N, find the minimum integer jj not less than 11 that satisfies the following condition:

  • If we let x=ix = i and then replace xx with AxA_x exactly jj times, we have x=ix = i.

Constraints

  • 1N1001 \leq N \leq 100
  • 1AiN1 \leq A_i \leq N
  • AiAj(ij)A_i \neq A_j (i \neq j)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN
A1A_1 A2A_2 \ldots ANA_N

Output

For each of the integers 1,2,,N1, 2, \ldots, N, in this order, print the minimum integer jj not less than 11 that satisfies the condition in Problem Statement.


Sample Input 1

6
1 3 2 5 6 4

Sample Output 1

1 2 2 3 3 3
  • For i=1i = 1: Since A1=1A_1 = 1, j=1j = 1 is the minimum value of jj satisfying the condition.
  • For i=2i = 2: Since A2=3,A3=2A_2 = 3, A_3 = 2, j=2j = 2 is the minimum value of jj satisfying the condition.
  • For i=3i = 3: Since A3=2,A2=3A_3 = 2, A_2 = 3, j=2j = 2 is the minimum value of jj satisfying the condition.
  • For i=4i = 4: Since A4=5,A5=6,A6=4A_4 = 5, A_5 = 6, A_6 = 4, j=3j = 3 is the minimum value of jj satisfying the condition.
  • For i=5i = 5: Since A5=6,A6=4,A4=5A_5 = 6, A_6 = 4, A_4 = 5, j=3j = 3 is the minimum value of jj satisfying the condition.
  • For i=6i = 6: Since A6=4,A4=5,A5=6A_6 = 4, A_4 = 5, A_5 = 6, j=3j = 3 is the minimum value of jj satisfying the condition.

Sample Input 2

3
3 2 1

Sample Output 2

2 1 2

Sample Input 3

2
1 2

Sample Output 3

1 1
F - Tasking

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 77

注意

この問題に対する言及は、2020年5月2日 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。

試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

NN 個のタスクがあります。ii 個目のタスクは AiA_i 日目 (今日を 11 日目とします) またはそれ以降に実行することができ、消化することで BiB_i ポイントが得られます。

あなたは、これから 11 日ごとに、「タスクを一つ選んでそれを消化する」という行為を繰り返します。 11 以上 NN 以下のすべての整数 kk に対して、これから kk 日間で得られるポイントの合計の最大値を求めてください。

ただし、11 以上 NN 以下のすべての整数 kk に対して、kk 日目までに実行することのできるタスクが kk 個以上存在することが保証されています。

制約

  • 1N2×1051 \leq N \leq 2\times10^5
  • 1AiN1 \leq A_i \leq N
  • 1Bi1001 \leq B_i \leq 100
  • 入力中のすべての値は整数である。

入力

入力は以下の形式で標準入力から与えられる。

NN
A1A_1 B1B_1
A2A_2 B2B_2
:
ANA_N BNB_N

出力

以下の形式で NN 行出力せよ。ここで、anskans_k はこれから kk 日間で得られるポイントの合計の最大値である。

ans1ans_1
ans2ans_2
:
ansNans_N

入力例 1

3
1 3
2 2
2 4

出力例 1

3
7
9

11 日目に消化できるタスクは 11 番目のタスクだけなので、そのタスクを消化し、33 ポイントを得ます。k=1k=1 の場合、これが答えです。 22 日目に消化できるタスクは 11 番目から 33 番目までのすべてです。 k=2k=2 の場合、11 日目に 11 番目のタスクを消化し、22 日目に 33 番目のタスクを消化することで得られる 3+4=73+4=7 ポイントが得られる最大のポイントです。 k=3k=3 の場合、33 日目までにはすべてのタスクを消化することができるので、答えは 3+2+4=93+2+4=9 です。


入力例 2

5
5 3
4 1
3 4
2 1
1 5

出力例 2

5
6
10
11
14

入力例 3

6
1 8
1 6
2 9
3 1
3 2
4 1

出力例 3

8
17
23
25
26
27

Score : 77 points

Warning

Do not make any mention of this problem until May 2, 2020, 6:00 p.m. JST. In case of violation, compensation may be demanded.

After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

We have NN tasks. Let today be Day 11. You can do the ii-th task on or after Day AiA_i, and finishing the task gives you BiB_i points.

Each day, starting with today, you choose one task and finish it. For each integer kk from 11 through NN, find the maximum total number of points that can be earned in the kk days starting with today.

It is guaranteed that there are at least kk tasks that you can do in the first kk days for each integer kk from 11 through NN.

Constraints

  • 1N2×1051 \leq N \leq 2\times10^5
  • 1AiN1 \leq A_i \leq N
  • 1Bi1001 \leq B_i \leq 100
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN
A1A_1 B1B_1
A2A_2 B2B_2
::
ANA_N BNB_N

Output

Print NN lines in the format below, where anskans_k is the maximum total number of points earned on the kk days starting with today.

ans1ans_1
ans2ans_2
::
ansNans_N

Sample Input 1

3
1 3
2 2
2 4

Sample Output 1

3
7
9

On Day 11, only the first task is available. You do that task and earn 33 points, which is the answer for k=1k=1. On Day 22, all three tasks are available. For k=2k=2, do the first task on Day 11 and the third task on Day 22 to earn the maximum number of points: 3+4=73+4=7. For k=3k=3, you can finish all the tasks in three days, so the answer is 3+2+4=93+2+4=9.


Sample Input 2

5
5 3
4 1
3 4
2 1
1 5

Sample Output 2

5
6
10
11
14

Sample Input 3

6
1 8
1 6
2 9
3 1
3 2
4 1

Sample Output 3

8
17
23
25
26
27
G - String Query

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 66

注意

この問題に対する言及は、2020年5月2日 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。

試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

文字列 SS に対して QQ 回の操作を行います。初め文字列 SS は空文字列です。

ii 回目の操作の種類は整数 Ti(1iQ)T_i (1 \leq i \leq Q) で表され、その内容は以下の通りです。

  • Ti=1T_i = 1 のとき
    • 英小文字 CiC_i と整数 XiX_i が与えられる。SS の末尾に CiC_iXiX_i 文字追加する。
  • Ti=2T_i = 2 のとき
    • 整数 DiD_i が与えられる。SS の先頭から DiD_i 文字を削除する。SS の長さが DiD_i 文字以下である場合は、SS は空文字列となる。 そして、この操作でa, b, c, \cdots z の各文字が何文字削除されたかを求める。それぞれ dela,delb,,delzdel_a, del_b, \cdots, del_z 文字削除された場合、dela2+delb2++delz2{del_a}^2 + {del_b}^2 + \cdots + {del_z}^2 を出力する。

制約

  • 1Q1051 \leq Q \leq 10^5
  • Ti=1T_i = 1 または 22
  • CiC_i は英小文字
  • 1Xi1051 \leq X_i \leq 10^5
  • 1Di1051 \leq D_i \leq 10^5
  • Q,Ti,Xi,DiQ, T_i, X_i, D_i は整数
  • Ti=2T_i = 2 の操作が 11 つ以上存在する

入力

入力は以下の形式で標準入力から与えられる。

QQ
Query1Query_1
::
QueryQQuery_Q

22 行目から Q+1Q+1 行目の QueryiQuery_i は以下の 22 つのいずれかである。

11 CiC_i XiX_i

Ti=1T_i = 1 として操作を行うことを表す。

22 DiD_i

Ti=2T_i = 2 として操作を行うことを表す。

出力

Ti=2T_i = 2 である操作それぞれに対して順番に、各文字種の削除された文字数の 22 乗の和を出力せよ。


入力例 1

6
1 a 5
2 3
1 t 8
1 c 10
2 21
2 4

出力例 1

9
168
0

初め SS は空文字列です。

  • 11 回目の操作の後、SSaaaaa となります。
  • 22 回目の操作の後、SSaa となります。a33 文字削除され、他の文字は削除されていません。
  • 33 回目の操作の後、SSaatttttttt となります。
  • 44 回目の操作の後、SSaattttttttcccccccccc となります。
  • 55 回目の操作の後、操作前の SS の長さは 2020 であるので、SS は空文字列となります。 a22 文字、c1010 文字、t88 文字削除されています。
  • 66 回目の操作の後、SS は空文字列のままです。どの文字も削除されていません。

よって、22 回目の操作では 32+02++02=93^2 + 0^2 + \cdots + 0^2 = 9 を、55 回目の操作では 22+102+82=1682^2 + 10^2 + 8^2 = 168 を出力してください。 66 回目の操作ではどの文字も削除されていないので、00 を出力してください。


入力例 2

4
1 x 5
1 y 8
2 7
1 z 8

出力例 2

29

初め SS は空文字列です。

  • 11 回目の操作の後、SSxxxxx となります。
  • 22 回目の操作の後、SSxxxxxyyyyyyyy となります。
  • 33 回目の操作の後、SSyyyyyy となります。x55 文字、y22 文字削除されています。52+22=295^2 + 2^2 = 29 です。
  • 44 回目の操作の後、SSyyyyyyzzzzzzzz となります。

入力例 3

3
1 p 3
1 q 100000
2 100000

出力例 3

9999400018

Score : 66 points

Warning

Do not make any mention of this problem until May 2, 2020, 6:00 p.m. JST. In case of violation, compensation may be demanded.

After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

Let us perform QQ operations on a string SS, which is initially empty.

The type of the ii-th operation is represented by an integer Ti(1iQ)T_i (1 \leq i \leq Q), as follows:

  • If Ti=1T_i = 1:
    • Given a lowercase English letter CiC_i and an integer XiX_i, append XiX_i copies of CiC_i to the end of SS.
  • If Ti=2T_i = 2:
    • Given an integer DiD_i, erase the first DiD_i characters in SS. If the length of SS is DiD_i or less, SS becomes empty. Then, find the number of as deleted in this operation, bs deleted in this operation, \cdots, and zs deleted in this operation. Let dela,delb,,delzdel_a, del_b, \cdots, del_z be the numbers of characters deleted. We ask you to print the value dela2+delb2++delz2{del_a}^2 + {del_b}^2 + \cdots + {del_z}^2.

Constraints

  • 1Q1051 \leq Q \leq 10^5
  • Ti=1T_i = 1 or 22.
  • CiC_i is a lowercase English letters.
  • 1Xi1051 \leq X_i \leq 10^5
  • 1Di1051 \leq D_i \leq 10^5
  • Q,Ti,Xi,Q, T_i, X_i, and DiD_i are integers.
  • There is at least one operation with Ti=2T_i = 2.

Input

Input is given from Standard Input in the following format:

QQ
Query1Query_1
::
QueryQQuery_Q

Here, on the 22-nd through the (Q+1)(Q+1)-th lines, QueryiQuery_i is one of the following:

11 CiC_i XiX_i

representing an operation with Ti=1T_i = 1, and:

22 DiD_i

representing an operation with Ti=2T_i = 2.

Output

For each operation with Ti=2T_i = 2 in order, print the value dela2+delb2++delz2{del_a}^2 + {del_b}^2 + \cdots + {del_z}^2 described in Problem Statement.


Sample Input 1

6
1 a 5
2 3
1 t 8
1 c 10
2 21
2 4

Sample Output 1

9
168
0

Initially, SS is empty.

  • After the 11-st operation, SS is aaaaa.
  • After the 22-nd operation, SS is aa. Here, just three as get deleted.
  • After the 33-rd operation, SS is aatttttttt.
  • After the 44-th operation, SS is aattttttttcccccccccc.
  • After the 55-th operation, SS is empty, since the length of SS is 2020 before this operation. Here, two as, ten cs, and eight ts get deleted.
  • After the 66-th operation, SS is still empty. No characters get deleted.

Therefore, for the second operation, print 32+02++02=93^2 + 0^2 + \cdots + 0^2 = 9; for the fifth operation, print 22+102+82=1682^2 + 10^2 + 8^2 = 168; for the sixth operation where no characters get deleted, print 00.


Sample Input 2

4
1 x 5
1 y 8
2 7
1 z 8

Sample Output 2

29

Initially, SS is empty.

  • After the 11-st operation, SS is xxxxx.
  • After the 22-nd operation, SS is xxxxxyyyyyyyy.
  • After the 33-rd operation, SS is yyyyyy. Here, five xs and two ys get deleted, and 52+22=295^2 + 2^2 = 29.
  • After the 44-th operation, SS is yyyyyyzzzzzzzz.

Sample Input 3

3
1 p 3
1 q 100000
2 100000

Sample Output 3

9999400018
H - 1-9 Grid

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 66

注意

この問題に対する言及は、2020年5月2日 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。

試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

NN マス、横 MM マスのマス目があり、各マスには S, G, 1 から 9 の数字のうちいずれかが書かれています。SG の書かれたマスはただ 11 つ存在します。

マス目の状態は文字からなるサイズ N×MN \times M の二次元配列 AA で表され、上から ii 行目、左から jj 列目に書かれた文字は Ai,jA_{i, j} です。

今、S のマスから出発して G のマスに移動しようとしています。各マスからは、44 方向に隣り合うマスに移動することができます。マス目の外に出るような移動はできません。

S のマスから G のマスへのそのような経路であって、1, 2, \cdots, 9 の書かれたマスをこの順に含むような経路のうち、隣のマスに移動する回数が最小である経路での移動回数を求めてください。また、そのような経路が存在しない場合は 1-1 を出力してください。

なお、経路が 1, 2, \cdots, 9 の書かれたマスをこの順に含むとは、kk 回の移動でマスに書かれていた文字を c0=c_0 = S, c1,c2,,ckc_1, c_2, \cdots, c_k = G としたとき、ci1=c_{i_1} = 1, ci2=c_{i_2} = 2, \cdots, ci9=c_{i_9} = 9 となるような 1i1i2i9<k1 \leq i_1 \leq i_2 \cdots \leq i_9 < k が存在することを表します。

ただし、同じマスは複数回通って良く、S, G の書かれたマスを途中で通過しても構いません。

制約

  • 1N,M501 \leq N, M \leq 50
  • AAS, G, および 1 から 9 の数字から成る
  • S, G の書かれたマスはちょうど 11 つずつ存在する

入力

入力は以下の形式で標準入力から与えられる。

NN MM
A1,1Ai,2A1,MA_{1,1}A_{i,2}\cdots A_{1,M}
A2,1A2,2A2,MA_{2,1}A_{2,2}\cdots A_{2,M}
::
AN,1AN,2AN,MA_{N,1}A_{N,2}\cdots A_{N,M}

出力

1, 2, \cdots, 9 の書かれたマスをこの順に部分列に含むような経路のうち、隣のマスに移動する回数が最小であるような経路での移動回数を出力せよ。


入力例 1

3 4
1S23
4567
89G1

出力例 1

17

1 の書かれたマスのみ 22 つあります。上から ii 行目、左から jj 列目のマスを (i,j)(i, j) で表すと、次の経路は移動回数が 1717 で、これが最小です。

  • (1, 2), (1, 1), (1, 2), (1, 3), (1, 4), (1, 3), (1, 2), (1, 1), (2, 1), (2, 2), (2, 3), (2, 4), (2, 3), (2, 2), (2, 1), (3, 1), (3, 2), (3, 3)

例えば太字で示したマスに 1, 2, \cdots, 9 がこの順で書かれています。


入力例 2

1 11
S134258976G

出力例 2

20

入力例 3

3 3
S12
4G7
593

出力例 3

-1

6 が含まれないので、条件を満たす経路は存在しません。1-1 を出力してください。

Score : 66 points

Warning

Do not make any mention of this problem until May 2, 2020, 6:00 p.m. JST. In case of violation, compensation may be demanded.

After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

We have an N×MN \times M grid (NN vertical, MM horizontal), where each square contains one of the following characters: S, G, and the digits from1 through 9. There is only one square with S and only one square with G.

This grid is described by a two-dimensional array of characters AA of size N×MN \times M, where Ai,jA_{i, j} is the character contained in the square at the ii-th row from the top and jj-th column from the left.

We now want to start at the square with S and reach the square with G. From each square, we can make a move to a horizontally or vertically (but not diagonally) adjacent square. It is not allowed to go out of the grid.

Find the minimum possible number of moves in such a path from the S square to the G square that contains the squares with 1, 2, \cdots, 9 in this order. If no such path exists, print 1-1.

Here, a path is said to contain the squares with 1, 2, \cdots, 9 in this order when the following holds: Assume that the path has kk moves, and it visited squares with the characters c0=c_0 = S, c1,c2,,ckc_1, c_2, \cdots, c_k = G. There exists integers 1i1i2i9<k1 \leq i_1 \leq i_2 \cdots \leq i_9 < k such that ci1=c_{i_1} = 1, ci2=c_{i_2} = 2, \cdots, ci9=c_{i_9} = 9.

A path may visit the same square multiple times, including the squares with S and G.

Constraints

  • 1N,M501 \leq N, M \leq 50
  • AA consists of S, G, and digits from1 through 9.
  • There is exactly one square with S and exactly one square with E.

Input

Input is given from Standard Input in the following format:

NN MM
A1,1Ai,2A1,MA_{1,1}A_{i,2}\cdots A_{1,M}
A2,1A2,2A2,MA_{2,1}A_{2,2}\cdots A_{2,M}
::
AN,1AN,2AN,MA_{N,1}A_{N,2}\cdots A_{N,M}

Output

Print the minimum possible number of moves in a path from the S square to the G square that contains the squares with 1, 2, \cdots, 9 in this order.


Sample Input 1

3 4
1S23
4567
89G1

Sample Output 1

17

In this grid, only 1 appears twice. Let (i,j)(i, j) denote the square at the ii-th row from the top and the jj-th square from the left. Then, the following path has the minimum possible number of moves, which is 1717:

  • (1, 2), (1, 1), (1, 2), (1, 3), (1, 4), (1, 3), (1, 2), (1, 1), (2, 1), (2, 2), (2, 3), (2, 4), (2, 3), (2, 2), (2, 1), (3, 1), (3, 2), (3, 3)

The squares shown by bold characters, for example, contain 1, 2, \cdots, 9, respectively.


Sample Input 2

1 11
S134258976G

Sample Output 2

20

Sample Input 3

3 3
S12
4G7
593

Sample Output 3

-1

The grid has no 6, so there is no path we are seeking, and we should print 1-1.

I - Elimination

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 66

注意

この問題に対する言及は、2020年5月2日 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。

試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

11、人 22、…、人 2N2^N2N2^N 人が一列に並んでトーナメントを行いました。このトーナメントは以下のようにして行われました。

  • 11 回戦: 1i2N11 \leq i \leq 2^{N-1} を満たすそれぞれの ii について、人 2i12i-1 と人 2i2i が戦う。どちらか一方が勝つので、勝った方が残る。
  • ii 回戦 (i2i \geq 2): i1i - 1 回戦で残った人が番号順に左から並び、左から 2 人ずつペアになり、各ペア内の 22 人が戦う。どちらか一方が勝つので、勝った方が残る。

各試合の記録は失われてしまいましたが、人 ii の強さが AiA_i であったことは記録に残っていました。ここで、22 人の人が戦ったとき、強さの値が大きい方が勝ちます。

それぞれの人について、その人が最後に戦ったのは何回戦か求めてください。

制約

  • 1N161 \leq N \leq 16
  • 1Ai2N1 \leq A_i \leq 2^N
  • AA の要素は相異なる

入力

入力は以下の形式で標準入力から与えられる。

NN
A1A_1 A2A_2 \cdots A2NA_{2^N}

出力

2N2^N 行出力せよ。ii 行目には、人 ii が最後に戦ったのは何回戦かを出力せよ。


入力例 1

2
2 4 3 1

出力例 1

1
2
2
1
  • 11 回戦: 人 11 (強さ 22) と人 22 (強さ 44) が戦い、人 22 が勝ち残った。また、人 33 (強さ 33) と人 44 (強さ 11) が戦い、人 33 が勝ち残った。
  • 22 回戦: 人 22 と人 33 が戦い、人 22 が勝った。

したがって、人 11 と 人 44 が最後に戦ったのは 11 回戦、人 22 と人 33 が最後に戦ったのは 22 回戦です。


入力例 2

1
2 1

出力例 2

1
1

参加者が 22 人しかいない場合は、どちらも 1 回戦が最後の戦いになります。


入力例 3

3
4 7 5 1 6 3 2 8

出力例 3

1
3
2
1
2
1
1
3

Score : 66 points

Warning

Do not make any mention of this problem until May 2, 2020, 6:00 p.m. JST. In case of violation, compensation may be demanded.

After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

2N2^N players with integer IDs - Player 11, Player 22, ..., Player 2N2^N - stood in a line and played an elimination tournament, as follows:

  • Round 11: For each ii such that 1i2N11 \leq i \leq 2^{N-1}, Player 2i12i-1 and Player 2i2i fight until one of them wins and survives.
  • Round ii (i2i \geq 2): The players survived Round i1i-1 stand in a line in ascending order of ID. Then, for each ii such that 1i2Ni1 \leq i \leq 2^{N-i}, the (2i1)(2i-1)-th and the $(2i)-th players from the left fight until one of them wins and survives.

While the record of each fight is now lost, our record says that the strength of Player ii was AiA_i. Here, when two players fight, the player with the greater strength wins.

For each of the players, find the last round in which that player fought.

Constraints

  • 1N161 \leq N \leq 16
  • 1Ai2N1 \leq A_i \leq 2^N
  • The elements of AA are distinct.

Input

Input is given from Standard Input in the following format:

NN
A1A_1 A2A_2 \cdots A2NA_{2^N}

Output

Print 2N2^N. The ii-th line should contain the integer representing the last round in which Player ii fought.


Sample Input 1

2
2 4 3 1

Sample Output 1

1
2
2
1
  • Round 11: Player 22 (strength: 44) fought against Player 11 (strength: 22) and won. Also, Player 33 (strength: 33) fought against Player 44 (strength: 11) and won.
  • Round 22: Player 22 fought against Player 33 and won.

Thus, the last rounds in which Player 1,2,3,41,2,3,4 fought are Round 1,2,2,11,2,2,1, respectively.


Sample Input 2

1
2 1

Sample Output 2

1
1

There were just two players, in which case Round 11 was the last round for both of them.


Sample Input 3

3
4 7 5 1 6 3 2 8

Sample Output 3

1
3
2
1
2
1
1
3
J - Parse

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 66

注意

この問題に対する言及は、2020年5月2日 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。

試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

英小文字 a ~ z() からなる文字列 SS が与えられます。 ある文字列 aabb に対して a+ba+baabb をこの順に結合した文字列とします。 また、ある文字列 aa に対して rev(a)rev(a)aa を反転させた文字列とします。 以下の操作をこれ以上操作が行えなくなるまで繰り返し行ったときの、最終的な文字列 SS を出力してください。

  • () を含まない文字列 aa (空文字列でもよい) に対して ( +a++a+ ) のような部分文字列が SS の中にあったときに、 その部分文字列を a+rev(a)a+rev(a) に置き換える

ただし、文字列 SS で正しい括弧の対応が取れていること、すなわち最終的な文字列 SS() は含まれないことが保証されています。また、この条件下で最終的な文字列は一意に定まることが示せます。

制約

  • SS は長さ 11 以上 10001000 以下の、英小文字 a ~ z() からなる文字列である。
  • SS11 文字以上の英小文字を含む。
  • 操作をこれ以上操作が行えなくなるまで繰り返し行ったときの、最終的な文字列 SS の長さは 10001000 以下であり、これに() は含まれない。

入力

入力は以下の形式で標準入力から与えられる。

SS

出力

操作をこれ以上操作が行えなくなるまで繰り返し行ったときの最終的な文字列 SS を出力せよ。


入力例 1

(ab)c

出力例 1

abbac

(ab)ab +rev(+rev( ab )) である abba に置き換えると、SSabbac になります。 これ以上操作を行うことはできないので、これを出力します。


入力例 2

past

出力例 2

past

括弧が存在しない可能性もあります。


入力例 3

(d(abc)e)()

出力例 3

dabccbaeeabccbad

括弧の中が空文字列である可能性もあります。

Score : 66 points

Warning

Do not make any mention of this problem until May 2, 2020, 6:00 p.m. JST. In case of violation, compensation may be demanded.

After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

Given is a string SS consisting of lowercase English letters a through z, (, and ). For strings aa and bb, let a+ba+b be the concatenation of aa and bb in this order. Also, for a string aa, let rev(a)rev(a) be the reversal of aa. Print the string SS after repeating the following operation until it cannot be applied.

  • If SS has a substring of the form ( + aa + ) where aa is a string that does not contain (s or )s, replace that substring with a+rev(a)a+rev(a).

Here, it is guaranteed that parentheses in SS are balanced, that is, the final string will not contain (s or )s. Also, under this condition, it can be shown that the final string is uniquely determined.

Constraints

  • SS is a string of length between 11 and 10001000 (inclusive) consisting of lowercase English letters a through z, (, and ).
  • SS contains one or more lowercase English letters.
  • The string SS after repeating the operation until it cannot be applied has a length of at most 10001000 and does not contain (s or )s.

Input

Input is given from Standard Input in the following format:

SS

Output

Print the string SS after repeating the following operation until it cannot be applied.


Sample Input 1

(ab)c

Sample Output 1

abbac

After replacing (ab) with ab +rev(+rev( ab )), that is, abba, SS is abbac. We can do no more operations, so we should print this string.


Sample Input 2

past

Sample Output 2

past

The input may contain no parentheses.


Sample Input 3

(d(abc)e)()

Sample Output 3

dabccbaeeabccbad

There can be an empty string between parentheses.

K - Parentheses

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 66

注意

この問題に対する言及は、2020年5月2日 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。

試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

() からなる長さ NN の文字列 SS が与えられます。SSii 番目の文字を SiS_i で表します。

以下の手順で SS括弧の対応が取れている文字列 (後述) にすることを考えます。

まず、次の操作を 00 回以上好きなだけ行います。

  • 1iN1 \leq i \leq N を選ぶ。SiS_i( ならば ) に、) ならば ( に変更する。この操作のコストは CiC_i である。

その後、次の操作を行います。

  • SS から 00 文字以上選んで削除し (全ての文字を削除しても良い)、削除しなかった文字を元の順序で繋げる。SSii 文字目を削除するコストは DiD_i である。

SS を括弧の対応が取れている文字列にするための合計コストの最小値を求めてください。

なお、括弧の対応が取れている文字列とは、次のうちいずれかの条件を満たす文字列です。

  • 空文字列
  • ある括弧の対応が取れている空でない文字列 A,BA, B が存在し、 AA , BB をこの順に連結した文字列
  • ある括弧の対応が取れている文字列 AA が存在し、 (, AA, ) をこの順に連結した文字列

制約

  • 1N30001 \leq N \leq 3000
  • SS の長さは NN
  • SS の各文字は ( または )
  • 1Ci1091 \leq C_i \leq 10^9
  • 1Di1091 \leq D_i \leq 10^9
  • N,Ci,DiN, C_i, D_i は整数

入力

入力は以下の形式で標準入力から与えられる。

NN
SS
C1C_1 C2C_2 \cdots CNC_N
D1D_1 D2D_2 \cdots DND_N

出力

SS を括弧の対応が取れている文字列にするための合計コストの最小値を出力せよ。


入力例 1

3
))(
3 5 7
2 6 5

出力例 1

8

はじめ SS))( です。例えば次の手順で括弧の対応が取れている文字列にすることができます。

  • 文字を変更する段階で、S1S_1 を変更する。33 のコストがかかり、SS()( となる。
  • 文字を削除する段階で、S3S_3 を削除する。55 のコストがかかり、SS() となる。

この場合の合計コストは 3+5=83+5=8 であり、これが最小です。


入力例 2

1
(
10
20

出力例 2

20

はじめ SS( です。これを空文字列に変えるしかありません。


入力例 3

10
))())((()(
13 18 17 3 20 20 6 14 14 2
20 1 19 5 2 19 2 19 9 4

出力例 3

18

入力例 4

4
()()
17 8 3 19
5 3 16 3

出力例 4

0

SS は既に括弧の対応が取れている文字列です。

Score : 66 points

Warning

Do not make any mention of this problem until May 2, 2020, 6:00 p.m. JST. In case of violation, compensation may be demanded.

After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

Given is a string SS of length NN consisting of (s and )s. Let SiS_i denote the ii-th character of SS.

We want to make SS a balanced string (defined below) in the following procedure.

First, we do the operation below zero or more times:

  • Choose an integer 1iN1 \leq i \leq N. If SiS_i is (, replace it with ); if SiS_i is ), replace it with (. The cost of this operation is CiC_i.

Then, we do the operation below:

  • Choose zero or more (possibly all) characters in SS and delete them. Then, concatenate the remaining characters in SS without changing the order.

Find the minimum total cost required to make SS a balanced string.

A balanced string is a string that is one of the following:

  • An empty string;
  • The concatenation of AA and BB in this order, for some non-empty balanced strings AA and BB;
  • The concatenation of (, AA, and ) in this order, for some balanced string AA/

Constraints

  • 1N30001 \leq N \leq 3000
  • The length of SS is NN.
  • Each character of SS is ( or ).
  • 1Ci1091 \leq C_i \leq 10^9
  • 1Di1091 \leq D_i \leq 10^9
  • NN, CiC_i, and DiD_i are integers.

Input

Input is given from Standard Input in the following format:

NN
SS
C1C_1 C2C_2 \cdots CNC_N
D1D_1 D2D_2 \cdots DND_N

Output

Print the minimum total cost required to make SS a balanced string.


Sample Input 1

3
))(
3 5 7
2 6 5

Sample Output 1

8

SS is initially ))(. One way to make it a balanced string is:

  • In the "modifying" step, change S1S_1 at the cost of 33. SS becomes ()(.
  • In the "deleting" step, delete S3S_3 at the cost of 55. SS becomes ().

The total cost here is 3+5=83+5=8, and this is the minimum possible value.


Sample Input 2

1
(
10
20

Sample Output 2

20

SS is initially (, and the only choice is to make it an empty string.


Sample Input 3

10
))())((()(
13 18 17 3 20 20 6 14 14 2
20 1 19 5 2 19 2 19 9 4

Sample Output 3

18

Sample Input 4

4
()()
17 8 3 19
5 3 16 3

Sample Output 4

0

SS is already a balanced string.

L - Lexicographically Minimum

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 66

注意

この問題に対する言及は、2020年5月2日 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。

試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

長さ NN の整数列 A1,A2,,ANA_1, A_2, \cdots, A_N があります。ここから KK 個の要素を選び、それらを相対的な順序を保ったまま並べて新しい数列を作るという操作を考えます。ただし、AiA_iAjA_j (ij)(i \neq j) をともに選べるのは ijD|i - j| \geq D であるときに限ります。

この操作で作ることのできる数列のうち、辞書順で最小である数列を求めてください。そのような操作が不可能である場合は 1-1 を出力してください。

なお、長さ KK の数列 x1,x2,,xKx_1,x_2,\cdots,x_Ky1,y2,,yKy_1,y_2,\cdots,y_K より辞書順で小さいとは、二つの数列が異なり、かつ iixiyix_i \neq y_i であるような最小の整数としたとき xi<yix_i < y_i であることをいいます。

制約

  • 2N2×1052 \leq N \leq 2 \times 10^5
  • 1DN1 \leq D \leq N
  • 1KN1 \leq K \leq N
  • 0Ai1090 \leq A_i \leq 10^9
  • 入力は全て整数

入力

入力は以下の形式で標準入力から与えられる。

NN KK DD
A1A_1 A2A_2 \cdots ANA_N

出力

操作が可能であれば得られる辞書順最小の数列を、不可能であれば 1-1 を出力せよ。


入力例 1

3 2 2
3 1 4

出力例 1

3 4

数列 AA{3,1,4}\{ 3, 1, 4\} です。D=2D = 2 の下で、K=2K = 2 個の要素を選んで数列を作る方法は次の 11 通りです。

  • AA1,31, 3 番目の要素を選び、{3,4}\{ 3, 4\} とする。

よって、{3,4}\{ 3, 4\} を出力してください。


入力例 2

3 3 2
3 1 4

出力例 2

-1

数列 AA{3,1,4}\{ 3, 1, 4\} です。D=2D = 2 の下で、K=3K = 3 個の要素を選んで数列を作ることはできません。

よって、1-1 を出力してください。


入力例 3

3 2 1
3 1 4

出力例 3

1 4

数列 AA{3,1,4}\{ 3, 1, 4\} です。D=1D = 1 の下で、K=2K = 2 個の要素を選んで数列を作る方法は次の 33 通りです。

  • AA1,21, 2 番目の要素を選び、{3,1}\{ 3, 1\} とする。
  • AA1,31, 3 番目の要素を選び、{3,4}\{ 3, 4\} とする。
  • AA2,32, 3 番目の要素を選び、{1,4}\{ 1, 4\} とする。

よって、{1,4}\{ 1, 4\} を出力してください。


入力例 4

4 2 2
3 6 5 5

出力例 4

3 5

数列 AA{3,6,5,5}\{ 3, 6, 5, 5\} です。D=2D = 2 の下で、K=2K = 2 個の要素を選んで数列を作る方法は次の 33 通りです。

  • AA1,31, 3 番目の要素を選び、{3,5}\{ 3, 5\} とする。
  • AA1,41, 4 番目の要素を選び、{3,5}\{ 3, 5\} とする。
  • AA2,42, 4 番目の要素を選び、{6,5}\{ 6, 5\} とする。

よって、{3,5}\{ 3, 5\} を出力してください。

Score : 66 points

Warning

Do not make any mention of this problem until May 2, 2020, 6:00 p.m. JST. In case of violation, compensation may be demanded.

After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

We have an integer sequence of length NN: A1,A2,,ANA_1, A_2, \cdots, A_N. Consider choosing KK of its elements and arrange them in a row to form a new sequence without changing the relative order of the elements. Here, choosing AiA_i and AjA_j (ij)(i \neq j) at the same time is only allowed when ijD|i - j| \geq D.

Find the lexicographically smallest sequence obtained in this way. If it is impossible to obtain a sequence in this way, print 1-1.

Here, a sequence of length KK: x1,x2,,xKx_1,x_2,\cdots,x_K is said to be lexicographically smaller than another sequence y1,y2,,yKy_1,y_2,\cdots,y_K if and only if the sequences are different and xi<yix_i < y_i holds for the minimum integer ii such that xiyix_i \neq y_i.

Constraints

  • 2N2×1052 \leq N \leq 2 \times 10^5
  • 1DN1 \leq D \leq N
  • 1KN1 \leq K \leq N
  • 0Ai1090 \leq A_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN KK DD
A1A_1 A2A_2 \cdots ANA_N

Output

If it is possible to obtain a sequence in the way described in Problem Statement, print the lexicographically smallest such sequence; otherwise, print 1-1.


Sample Input 1

3 2 2
3 1 4

Sample Output 1

3 4

A={3,1,4}A = \{ 3, 1, 4\}. Under D=2D = 2, there is one way to choose K=2K = 2 elements to form a sequence, as follows:

  • Choose the 11-st and 33-rd elements of AA to form {3,4}\{ 3, 4\}.

Thus, we should print {3,4}\{ 3, 4\}.


Sample Input 2

3 3 2
3 1 4

Sample Output 2

-1

A={3,1,4}A = \{ 3, 1, 4\}. Under D=2D = 2, there is no way to choose K=3K = 3 elements to form a sequence.

Thus, we should print 1-1.


Sample Input 3

3 2 1
3 1 4

Sample Output 3

1 4

A={3,1,4}A = \{ 3, 1, 4\}. Under D=1D = 1, there are three ways to choose K=2K = 2 elements to form a sequence, as follows:

  • Choose the 11-st and 22-nd elements of AA to form {3,1}\{ 3, 1\}.
  • Choose the 11-st and 33-rd elements of AA to form {3,4}\{ 3, 4\}.
  • Choose the 22-nd and 33-rd elements of AA to form {1,4}\{ 1, 4\}.

Thus, we should print 1-1.


Sample Input 4

4 2 2
3 6 5 5

Sample Output 4

3 5

A={3,6,5,5}A = \{ 3, 6, 5, 5\}. Under D=2D = 2, there are three ways to choose K=2K = 2 elements to form a sequence, as follows:

  • Choose the 11-st and 33-rd elements of AA to form {3,5}\{ 3, 5\}.
  • Choose the 11-st and 44-th elements of AA to form {3,5}\{ 3, 5\}.
  • Choose the 22-nd and 44-th elements of AA to form {6,5}\{ 6, 5\}.

Thus, we should print {3,5}\{ 3, 5\}.

M - Cafeteria

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 66

注意

この問題に対する言及は、2020年5月2日 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。

試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

ある社員食堂では DD 日周期でメニューが組まれています。この食堂の料理は整数で表され、今日を 11 日目として ii 日目、i+Di + D 日目、i+2Di + 2D 日目、…、には料理 CiC_i が提供されます。

NN 人の社員はそれぞれ食事にこだわりがあり、社員 jj は料理 KjK_j を好んでいます。各社員は、好みの料理が提供される日には必ず食堂を利用してその料理を食べます。それ以外の料理が提供される日には、前回の利用が LL 日前である場合のみ食堂を利用します。ただし、初回の利用についてはこの限りではありません (後述)。

各整数 1jN1 \leq j \leq N について、以下の問題に答えてください。

  • 社員 jjFjF_j 日目に初めて食堂を利用するとする (この日に限って料理が好みでなくても利用するものとし、またこれより前に好みの料理が提供されていても利用できないものとする)。社員 jj が合計で TjT_j 回食堂を利用するまでに好みの料理を食べる回数を求めよ。

制約

  • 1N1051 \leq N \leq 10^5
  • 1D1051 \leq D \leq 10^5
  • 1L1091 \leq L \leq 10^9
  • 1Ci,Kj1051 \leq C_i, K_j \leq 10^5
  • 1FjD1 \leq F_j \leq D
  • 1Tj1091 \leq T_j \leq 10^9
  • 入力は全て整数

入力

入力は以下の形式で標準入力から与えられる。

DD LL NN
C1C_1 C2C_2 \cdots CDC_D
K1K_1 F1F_1 T1T_1
K2K_2 F2F_2 T2T_2
::
KNK_N FNF_N TNT_N

出力

NN 行出力せよ。jj 行目には、社員 jj が好みの料理を食べる回数を出力せよ。


入力例 1

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

出力例 1

1
0
3

食堂のメニューは、44 日周期で {2,3,1,3}\{ 2, 3, 1, 3 \} が繰り返されています。

  • 社員 11 は、料理 11 が好みです。はじめに 22 日目に料理 33 を食べ、次に 33 日目は料理 11 を食べることができます。よって 11 回好みの料理を食べることができるので、11 を出力してください。
  • 社員 22 は、料理 33 が好みです。33 日目に料理 11 を食べます。好みの料理を一度も食べることができないので、00 を出力してください。
  • 社員 33 は、料理 33 が好みです。はじめに 44 日目に料理 33 を、66 日目に料理 33 を、88 日目に料理 33 を食べます。よって 33 を出力してください。

入力例 2

3 1 3
1 1 1
2 1 3
1 2 3
1 3 3

出力例 2

0
3
3

食堂のメニューは毎日 11 です。

  • 社員 11 は、料理 22 が好みです。はじめに 11 日目に料理 11 を食べた後、L=1L = 1 であるので 2,32, 3 日目も料理 11 を食べます。好みの料理はメニューに含まれないので 00 を出力してください。
  • 社員 2,32, 3 は料理 11 が好きであり、毎日好みの料理を食べます。33 を出力してください。

入力例 3

10 4 4
4 4 4 3 1 1 5 2 2 1
2 5 2
2 9 10
2 3 3
2 7 13

出力例 3

1
5
1
6

Score : 66 points

Warning

Do not make any mention of this problem until May 2, 2020, 6:00 p.m. JST. In case of violation, compensation may be demanded.

After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

The menu in some company cafeteria is based on a DD-day cycle. Its dishes are represented by integers, and it serves Dish CiC_i on Day ii, Day i+Di + D, Day i+2Di + 2D, and so on, where today is Day 11.

The NN employees who use this cafeteria are particular about food, and Employee jj loves Dish KjK_j. Each employee always uses the cafeteria on a day when the favorite dish is served and gets it. On the other days, an employee uses the cafeteria only if the last day when he/she used it is LL days ago. However, the above does not apply when it is the first time for him/her to use it (described below).

For each integer 1jN1 \leq j \leq N, answer the question below:

  • Assume that Employee jj uses the cafeteria on Day FjF_j for the first time. (For this day only, he/she uses it even if the served dish is not his/her favorite. Also, assume that he/she cannot use the cafeteria before this day even when his/her favorite dish is served.) Find the number of times Employee jj gets the favorite dish until he/she uses the cafeteria TjT_j times in total.

Constraints

  • 1N1051 \leq N \leq 10^5
  • 1D1051 \leq D \leq 10^5
  • 1L1091 \leq L \leq 10^9
  • 1Ci,Kj1051 \leq C_i, K_j \leq 10^5
  • 1FjD1 \leq F_j \leq D
  • 1Tj1091 \leq T_j \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

DD LL NN
C1C_1 C2C_2 \cdots CDC_D
K1K_1 F1F_1 T1T_1
K2K_2 F2F_2 T2T_2
::
KNK_N FNF_N TNT_N

Output

Print NN lines. The jj-th line should contain the number of times Employee jj gets the favorite dish.


Sample Input 1

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

Sample Output 1

1
0
3

The cafeteria serves the dishes with a 44-day cycle: {2,3,1,3}\{ 2, 3, 1, 3 \}.

  • Employee 11 loves Dish 11. He first eats Dish 33 on Day 22, then eats Dish 11 on Day 33. Thus, he gets his favorite dish once, and we should print 11.
  • Employee 22 loves Dish 33. He eats Dish 11 on Day 33, and that is it. Thus, he gets his favorite dish zero times, and we should print 00.
  • Employee 33 loves Dish 33. He first eats Dish 33 on Day 44, then eats Dish 33 on Day 66, then eats Dish 33 on Day 88. Thus, we should print 33.

Sample Input 2

3 1 3
1 1 1
2 1 3
1 2 3
1 3 3

Sample Output 2

0
3
3

The cafeteria serves Dish 11 every day.

  • Employee 11 loves Dish 22. He first eats Dish 11 on Day 11, then also eats Dish 11 on Day 22 and 33 since L=1L = 1. The cafeteria never offers his favorite, so we should print 00.
  • Employee 22 and 33 love Dish 11, and they get their favorite every day, so we should print 33 for each of them.

Sample Input 3

10 4 4
4 4 4 3 1 1 5 2 2 1
2 5 2
2 9 10
2 3 3
2 7 13

Sample Output 3

1
5
1
6
N - Building Construction

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 66

注意

この問題に対する言及は、2020年5月2日 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。

試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

22 次元平面上に、各辺が x, y 軸に並行な正方形の敷地が NN 個あり、ii 個目の敷地の範囲は領域 xminixxmini+Di,yminiyymini+Dixmin_i \leq x \leq xmin_i + D_i, ymin_i \leq y \leq ymin_i + D_i です。各敷地にはコストが定まっており、ii 番目の敷地のコストは CiC_i です。

今、QQ 件のビル建設計画があり、jj 件目の計画での建設予定地点の座標は (Aj,Bj)(A_j, B_j) です。

各計画のコストは、その計画の建設座標を含む (または境界線上に持つ) すべての敷地のコストの和です。

各計画のコストを求めてください。

制約

  • 1N500001 \leq N \leq 50000
  • 1Q1000001 \leq Q \leq 100000
  • 109xmini,ymini109-10^9 \leq xmin_i, ymin_i \leq 10^9
  • 1Di1091 \leq D_i \leq 10^9
  • 0Ci1090 \leq C_i \leq 10^9
  • 109Ai,Bi109-10^9 \leq A_i, B_i \leq 10^9
  • 入力は全て整数

入力

入力は以下の形式で標準入力から与えられる。

NN QQ
xmin1xmin_1 ymin1ymin_1 D1D_1 C1C_1
xmin2xmin_2 ymin2ymin_2 D2D_2 C2C_2
::
xminNxmin_N yminNymin_N DND_N CNC_N
A1A_1 B1B_1
::
AQA_Q BQB_Q

出力

QQ 行出力せよ。jj 行目に jj 件目の計画のコストを出力すること。


入力例 1

2 4
1 3 6 10
3 6 6 20
4 7
-1 -1
1 4
7 13

出力例 1

30
0
10
0
  • 11 件目の計画の座標は、1,21, 2 個目の敷地に含まれるので、10+20=3010 + 20 = 30 を出力してください。
  • 22 件目の計画の座標は、どちらの敷地にも含まれないので、00 を出力してください。
  • 33 件目の計画の座標は、11 個目の敷地に含まれるので、1010 を出力してください。
  • 44 件目の計画の座標は、どちらの敷地にも含まれないので、00 を出力してください。

入力例 2

2 3
-3 5 4 100
1 9 7 30
1 9
1 8
8 10

出力例 2

130
100
30
  • 11 件目の計画の座標は、1,21, 2 個目の敷地に含まれるので、100+30=130100 + 30 = 130 を出力してください。
  • 22 件目の計画の座標は、11 個目の敷地に含まれるので、100100 を出力してください。
  • 33 件目の計画の座標は、22 個目の敷地に含まれるので、3030 を出力してください。

入力例 3

10 10
17 2 17 1000000000
7 12 12 1000000000
2 12 8 1000000000
2 12 2 1000000000
3 9 16 1000000000
8 13 15 1000000000
8 1 3 1000000000
15 9 17 1000000000
16 5 5 1000000000
13 12 9 1000000000
17 3
4 10
1 9
5 3
17 12
14 19
19 17
17 11
16 17
12 16

出力例 3

1000000000
1000000000
0
0
5000000000
4000000000
6000000000
3000000000
5000000000
3000000000

Score : 66 points

Warning

Do not make any mention of this problem until May 2, 2020, 6:00 p.m. JST. In case of violation, compensation may be demanded.

After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

In a two-dimensional plane, there are NN square plots of land whose sides are parallel to the x- or y-axis. The ii-th plot can be represented as the region xminixxmini+Di,yminiyymini+Dixmin_i \leq x \leq xmin_i + D_i, ymin_i \leq y \leq ymin_i + D_i. Each of these plots has a fixed cost, and the cost of the ii-th plot is CiC_i.

Here, QQ construction plans are considered. The jj-th plan aims to construct a building at coordinates (Aj,Bj)(A_j, B_j).

The cost of each plan is the sum of the costs of all the plots covering (or touching) the construction point.

Find the cost of each plan.

Constraints

  • 1N500001 \leq N \leq 50000
  • 1Q1000001 \leq Q \leq 100000
  • 109xmini,ymini109-10^9 \leq xmin_i, ymin_i \leq 10^9
  • 1Di1091 \leq D_i \leq 10^9
  • 0Ci1090 \leq C_i \leq 10^9
  • 109Ai,Bi109-10^9 \leq A_i, B_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN QQ
xmin1xmin_1 ymin1ymin_1 D1D_1 C1C_1
xmin2xmin_2 ymin2ymin_2 D2D_2 C2C_2
::
xminNxmin_N yminNymin_N DND_N CNC_N
A1A_1 B1B_1
::
AQA_Q BQB_Q

Output

Print QQ lines. The jj-th line should contain the cost of the jj-th plan.


Sample Input 1

2 4
1 3 6 10
3 6 6 20
4 7
-1 -1
1 4
7 13

Sample Output 1

30
0
10
0
  • For the 11-st plan, both plots cover the construction point, so we should print 10+20=3010 + 20 = 30.
  • For the 22-nd plan, neither plot covers the construction point, so we should print 00.
  • For the 33-rd plan, the 11-st plot covers the construction point, so we should print 1010.
  • For the 44-th plan, neither plot covers the construction point, so we should print 00.

Sample Input 2

2 3
-3 5 4 100
1 9 7 30
1 9
1 8
8 10

Sample Output 2

130
100
30
  • For the 11-st plan, both plots cover the construction point, so we should print 100+30=130100 + 30 = 130.
  • For the 22-nd plan, the 11-st plot covers the construction point, so we should print 100100.
  • For the 33-rd plan, the 22-nd plot covers the construction point, so we should print 3030.

Sample Input 3

10 10
17 2 17 1000000000
7 12 12 1000000000
2 12 8 1000000000
2 12 2 1000000000
3 9 16 1000000000
8 13 15 1000000000
8 1 3 1000000000
15 9 17 1000000000
16 5 5 1000000000
13 12 9 1000000000
17 3
4 10
1 9
5 3
17 12
14 19
19 17
17 11
16 17
12 16

Sample Output 3

1000000000
1000000000
0
0
5000000000
4000000000
6000000000
3000000000
5000000000
3000000000
O - Variable Spanning Trees

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 66

注意

この問題に対する言及は、2020年5月2日 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。

試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

NN 頂点 MM 辺の重みつき単純連結無向グラフが与えられます。頂点には 11 から NN、辺には 11 から MM までの番号が振られています。

ii は頂点 AiA_iBiB_i を結び、その重みは CiC_i です。

11 から辺 MM までのそれぞれの辺について、その辺を含む最小の重みの全域木を求め、その重みを出力してください。

注釈

  • 連結無向グラフ (V,E)(V, E) における全域木とは、ある辺の部分集合 TET \subseteq E について (V,T)(V, T) と書ける木のことである。
  • 全域木 (V,T)(V, T) の重みは、TT に含まれる全ての辺の重みの和である。

制約

  • 2N1052 \leq N \leq 10^5
  • N1Mmin(105,N(N1)/2)N - 1 \leq M \leq \mathrm{min}(10^5, N(N - 1)/2)
  • 1Ai,BiN1 \leq A_i, B_i \leq N (1iM1 \leq i \leq M)
  • AiBiA_i \neq B_i (1iM1 \leq i \leq M)
  • 1Ci1091 \leq C_i \leq 10^9 (1iM1 \leq i \leq M)
  • 与えられる無向グラフは単純かつ連結である

入力

入力は以下の形式で標準入力から与えられる。

NN MM
A1A_1 B1B_1 C1C_1
::
AMA_M BMB_M CMC_M

出力

MM 行出力せよ。ii 行目には、辺 ii を含む全域木の重みとしてありえる最小値を出力せよ。


入力例 1

3 3
1 2 2
2 3 5
3 1 3

出力例 1

5
7
5

入力例 2

5 7
1 2 8
2 3 9
2 4 7
3 1 5
3 4 6
4 1 8
3 5 1

出力例 2

20
21
19
19
19
21
19

入力例 3

8 10
1 2 314159265
2 3 358979323
3 4 846264338
1 3 327950288
1 4 419716939
2 4 937510582
1 5 97494459
1 6 230781640
1 7 628620899
1 8 862803482

出力例 3

2881526972
2912556007
3308074371
2881526972
2881526972
3399320615
2881526972
2881526972
2881526972
2881526972

答えが 32 ビット整数の範囲内でないこともあるため、注意してください。

Score : 66 points

Warning

Do not make any mention of this problem until May 2, 2020, 6:00 p.m. JST. In case of violation, compensation may be demanded.

After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

Given is a simple connected undirected graph with NN vertices and MM edges. The vertices are numbered 11 to NN, and the edges are numbered 11 to MM.

Edge ii connects Vertex AiA_i and BiB_i, and the weight of this edge is CiC_i.

For each edge from 11 to MM, find a minimum-weight spanning tree containing that edge, and print the weight of that tree.

Notes

  • A spanning tree in a connected undirected graph (V,E)(V, E) is a tree that can be written as (V,T)(V, T) for some subset of edges TET \subseteq E.
  • The weight of a spanning tree (V,T)(V, T) is the sum of the weights of all edges in TT.

Constraints

  • 2N1052 \leq N \leq 10^5
  • N1Mmin(105,N(N1)/2)N - 1 \leq M \leq \mathrm{min}(10^5, N(N - 1)/2)
  • 1Ai,BiN1 \leq A_i, B_i \leq N (1iM1 \leq i \leq M)
  • AiBiA_i \neq B_i (1iM1 \leq i \leq M)
  • 1Ci1091 \leq C_i \leq 10^9 (1iM1 \leq i \leq M)
  • The given graph is simple and connected.

Input

Input is given from Standard Input in the following format:

NN MM
A1A_1 B1B_1 C1C_1
::
AMA_M BMB_M CMC_M

Output

Print MM lines. The ii-th line should contain the minimum possible weight of a spanning tree containing Edge ii.


Sample Input 1

3 3
1 2 2
2 3 5
3 1 3

Sample Output 1

5
7
5

Sample Input 2

5 7
1 2 8
2 3 9
2 4 7
3 1 5
3 4 6
4 1 8
3 5 1

Sample Output 2

20
21
19
19
19
21
19

Sample Input 3

8 10
1 2 314159265
2 3 358979323
3 4 846264338
1 3 327950288
1 4 419716939
2 4 937510582
1 5 97494459
1 6 230781640
1 7 628620899
1 8 862803482

Sample Output 3

2881526972
2912556007
3308074371
2881526972
2881526972
3399320615
2881526972
2881526972
2881526972
2881526972

Note that the answer may not fit into a 32-bit integer type.