A - Spoon Taking Problem

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

N 人が円卓に座っており,各人は反時計回りに順に 1, \ldots, N と番号付けられています.各人はそれぞれ左右どちらか一方の利き手を持っています.

円卓上には 1, \ldots, N と番号付けられた計 N 本のスプーンが,隣り合う二人の間に 1 本ずつ置いてあります.各 1 \leq i \leq N について,人 i の左側,右側にはそれぞれスプーン i,スプーン (i+1) があります.ここで,スプーン (N+1) はスプーン 1 のことを指します.

N = 4 での模式図を以下に示します.

(1, \dots, N) の順列 (P_1, \dots, P_N) が与えられます.i=1,\dots,N の順に,人 P_i が以下のように行動します.

  • 自分の右側または左側にスプーンが残っているならば,そのうち 1 つを取る.
    • このとき自分の両側にスプーンが残っているならば,自分の利き手の側のスプーンを取る.
  • そうでないならば何もしない.

L, R, ? からなる長さ N の文字列 S が与えられます.N 人の利き手の組み合わせは 2^N 通りありますが,そのうち以下の条件を全て満たすような組み合わせの数を 998244353 で割った余りを求めてください.

  • Si 番目の文字が L ならば,人 i は左利きである.
  • Si 番目の文字が R ならば,人 i は右利きである.
  • 全員の行動が終了したとき,全員がスプーンを取っている.

制約

  • 入力される数値は全て整数
  • 2 \leq N \leq 2 \times 10^5
  • (P_1, \dots, P_N)(1, \dots, N) の順列
  • SL, R, ? からなる長さ N の文字列

入力

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

N
P_1 \dots P_N
S

出力

答えを 1 行に出力せよ.


入力例 1

3
1 2 3
L??

出力例 1

2

1,2,3 がそれぞれ左利き,左利き,右利きのとき,以下のように行動が行われます.

  • 1 が行動を開始する.人 1 の両側にスプーンが残っているので,人 1 の利き手と同じ左側のスプーン 1 を取る.
  • 2 が行動を開始する.人 2 の両側にスプーンが残っているので,人 2 の利き手と同じ左側のスプーン 2 を取る.
  • 3 が行動を開始する.人 3 の右側にはスプーンが残っておらず,左側にはスプーン 3 が残っているので,スプーン 3 を取る.全員の行動が終了し,このとき全員がスプーンを取っている.

この利き手の組み合わせは条件を満たします.他には人 1,2,3 がそれぞれ左利き,左利き,左利きの場合も条件を満たします.


入力例 2

3
1 3 2
R?L

出力例 2

0

条件を満たす利き手の組み合わせが存在しません.


入力例 3

12
6 2 9 3 1 4 11 5 12 10 7 8
????????????

出力例 3

160

Score: 400 points

Problem Statement

N people are sitting around a round table, and numbered 1 to N in a counterclockwise order. Each person has a dominant hand: left or right.

There are N spoons numbered 1 to N on the round table, with one spoon placed between each pair of adjacent people. For each 1 \leq i \leq N, to the left and right of person i, there are spoons i and (i+1), respectively. Here, spoon (N+1) refers to spoon 1.

Below is a diagram for N = 4.

You are given a permutation (P_1, \dots, P_N) of (1, \dots, N). In the order i=1,\dots,N, person P_i will act as follows:

  • If there is a spoon remaining on left or right side, they will take one of them.
    • If there are spoons remaining on both sides, they will take the spoon on the side of their dominant hand.
  • Otherwise, they do nothing.

You are also given a string S of length N consisting of L, R, and ?. Among the 2^N possible combinations of dominant hands, find how many satisfy all of the following conditions, modulo 998244353:

  • If the i-th character of S is L, person i is left-handed.
  • If the i-th character of S is R, person i is right-handed.
  • When everyone has finished acting, everyone has taken a spoon.

Constraints

  • All input values are integers.
  • 2 \leq N \leq 2 \times 10^5
  • (P_1, \dots, P_N) is a permutation of (1, \dots, N).
  • S is a string of length N consisting of L, R, and ?.

Input

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

N
P_1 \dots P_N
S

Output

Print the answer in a single line.


Sample Input 1

3
1 2 3
L??

Sample Output 1

2

When persons 1, 2, and 3 are left-handed, left-handed, and right-handed, respectively, the actions are performed as follows:

  • Person 1 starts acting. There are spoons on both sides, so person 1 takes spoon 1 on the left side, which is the same as their dominant hand.
  • Person 2 starts acting. There are spoons on both sides, so person 2 takes spoon 2 on the left side, which is the same as their dominant hand.
  • Person 3 starts acting. There is no spoon on the right side, but spoon 3 is remaining on the left side, so they take spoon 3. Everyone has finished acting and taken a spoon.

This combination of dominant hands satisfies the conditions. Besides, the conditions are also satisfied when persons 1, 2, 3 are all left-handed.


Sample Input 2

3
1 3 2
R?L

Sample Output 2

0

No combinations of dominant hands satisfy the conditions.


Sample Input 3

12
6 2 9 3 1 4 11 5 12 10 7 8
????????????

Sample Output 3

160
B - Parenthesis Arrangement

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

長さ 2N(, ) からなる文字列 S が与えられます.S の左から i 番目の文字を S_i と表します.

あなたは以下の 2 種類の操作を好きな順番で 0 回以上好きな回数行うことができます.

  • 1\leq i < j \leq 2N を満たす整数組 (i,j) を選ぶ.S_iS_j を入れ替える.この操作にはコストが A かかる.

  • 1\leq i \leq 2N を満たす整数 i を選ぶ.S_i( または ) で置き換える.この操作にはコストが B かかる.

あなたの目標は S を正しい括弧列にすることです.目標を達成するために必要なコストの総和の最小値を求めてください.なお,有限回の操作で必ず目標を達成できることが証明できます.

正しい括弧列とは 正しい括弧列とは,以下のいずれかの条件を満たす文字列です.
  • 空文字列
  • ある正しい括弧列 A が存在して,(, A, ) をこの順に結合した文字列
  • ある空でない正しい括弧列 S,T が存在して,S,T をこの順に結合した文字列

制約

  • 入力される数値は全て整数
  • 1 \leq N \leq 5\times 10^5
  • 1\leq A,B\leq 10^9
  • S は長さ 2N(, ) からなる文字列

入力

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

N A B
S

出力

答えを 1 行に出力せよ.


入力例 1

3 3 2
)))(()

出力例 1

5

操作の一例を示します.

  • S_3S_4 を入れ替える.S))()() となる.コストが 3 かかる.
  • S_1( で置き換える.S()()() となり,これは正しい括弧列である.コストが 2 かかる.

この例では,S を正しい括弧列にするのにかかったコストの総和が 5 です.コストの総和が 5 未満で S を正しい括弧列にする操作方法は存在しません.


入力例 2

1 175 1000000000
()

出力例 2

0

入力の S は既に正しい括弧列なので,操作を行う必要はありません.


入力例 3

7 2622 26092458
))()((((()()((

出力例 3

52187538

Score: 400 points

Problem Statement

You are given a string S of length 2N consisting of the characters ( and ). Let S_i denote the i-th character from the left of S.

You can perform the following two types of operations zero or more times in any order:

  • Choose a pair of integers (i,j) such that 1\leq i < j \leq 2N. Swap S_i and S_j. The cost of this operation is A.

  • Choose an integer i such that 1\leq i \leq 2N. Replace S_i with ( or ). The cost of this operation is B.

Your goal is to make S a correct parenthesis sequence. Find the minimum total cost required to achieve this goal. It can be proved that the goal can always be achieved with a finite number of operations.

What is a correct parenthesis sequence? A correct parenthesis sequence is a string that satisfies any of the following conditions:
  • It is an empty string.
  • It is formed by concatenating (, A, ) in this order where A is a correct parenthesis sequence.
  • It is formed by concatenating A and B in this order where A and B are correct parenthesis sequences.

Constraints

  • All input values are integers.
  • 1 \leq N \leq 5\times 10^5
  • 1\leq A,B\leq 10^9
  • S is a string of length 2N consisting of the characters ( and ).

Input

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

N A B
S

Output

Print the answer in a single line.


Sample Input 1

3 3 2
)))(()

Sample Output 1

5

Here is one way to operate:

  • Swap S_3 and S_4. S becomes ))()(). The cost is 3.
  • Replace S_1 with (. S becomes ()()(), which is a correct parentheses sequence. The cost is 2.

In this case, we have made S a correct bracket sequence for a total cost of 5. There is no way to make S a correct bracket sequence for less than 5.


Sample Input 2

1 175 1000000000
()

Sample Output 2

0

The given S is already a correct bracket sequence, so no operation is needed.


Sample Input 3

7 2622 26092458
))()((((()()((

Sample Output 3

52187538
C - Jumping Through Intervals

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

N 個の整数組 (L_1, R_1), (L_2, R_2), \dots, (L_N, R_N) が与えられます.ここで,全ての 1\leq i\leq N に対して L_i \leq R_i が満たされています.

N 個の整数からなる列 A = (A_1, A_2, \ldots, A_N) であって,以下の条件を満たすものを良い整数列と呼びます.

  • 全ての 1\leq i\leq N に対して,L_i \leq A_i \leq R_i である.

\displaystyle \sum_{i = 1}^{N-1} |A_{i+1} - A_{i}| を最小にするような良い整数列 A のうち,辞書順で最小のものを求めてください.

数列の辞書順とは

数列 S = (S_1,S_2,\ldots,S_{|S|}) が数列 T = (T_1,T_2,\ldots,T_{|T|}) より辞書順で小さいとは,下記の 1. と 2. のどちらかが成り立つことを言います. ここで,|S|, |T| はそれぞれ S, T の長さを表します.

  1. |S| \lt |T| かつ (S_1,S_2,\ldots,S_{|S|}) = (T_1,T_2,\ldots,T_{|S|})
  2. ある整数 1 \leq i \leq \min\lbrace |S|, |T| \rbrace が存在して,下記の 2 つがともに成り立つ.
    • (S_1,S_2,\ldots,S_{i-1}) = (T_1,T_2,\ldots,T_{i-1})
    • S_iT_i より(数として)小さい.

制約

  • 入力される数値は全て整数
  • 2 \leq N \leq 5 \times 10^5
  • 0 \leq L_i \leq R_i \leq 10^9

入力

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

N
L_1 R_1
L_2 R_2
\vdots
L_N R_N

出力

答えを以下の形式で 1 行に出力せよ.

A_1 A_2 \ldots A_N

入力例 1

4
1 10
8 13
3 4
5 20

出力例 1

8 8 4 5

(A_1, A_2, A_3, A_4) = (8, 8, 4, 5) は良い整数列です.このとき \sum_{i = 1}^{N-1} |A_{i+1} - A_{i}| = |8 - 8| + |4 - 8| + |5 - 4| = 5 となり,これが \sum_{i = 1}^{N-1} |A_{i+1} - A_{i}| の最小の値です.


入力例 2

3
20 24
3 24
1 75

出力例 2

20 20 20

\sum_{i = 1}^{N-1} |A_{i+1} - A_{i}| が最小となる良い数列 A が複数あるときは,そのうち辞書順で最小のものを出力することに注意してください.


入力例 3

15
335279264 849598327
446755913 822889311
526239859 548830120
181424399 715477619
342858071 625711486
448565595 480845266
467825612 647639160
160714711 449656269
336869678 545923679
61020590 573085537
626006012 816372580
135599877 389312924
511429216 547865075
561330066 605997004
539239436 921749002

出力例 3

526239859 526239859 526239859 467825612 467825612 467825612 467825612 449656269 449656269 449656269 626006012 389312924 511429216 561330066 561330066

Score: 600 points

Problem Statement

You are given N pairs of integers (L_1, R_1), (L_2, R_2), \dots, (L_N, R_N). Here, L_i \leq R_i for all 1 \leq i \leq N.

A sequence of N integers A = (A_1, A_2, \ldots, A_N) is called a good integer sequence if it satisfies the following condition:

  • L_i \leq A_i \leq R_i for all 1 \leq i \leq N.

Find the lexicographically smallest good integer sequence A that minimizes \displaystyle \sum_{i = 1}^{N-1} |A_{i+1} - A_{i}|.

What is lexicographical order for sequences?

A sequence S = (S_1,S_2,\ldots,S_{|S|}) is said to be lexicographically smaller than a sequence T = (T_1,T_2,\ldots,T_{|T|}) if 1. or 2. below holds. Here, |S|, |T| denote the lengths of S and T, respectively.

  1. |S| \lt |T| and (S_1,S_2,\ldots,S_{|S|}) = (T_1,T_2,\ldots,T_{|S|}).
  2. There is an integer 1 \leq i \leq \min\lbrace |S|, |T| \rbrace such that both of the following hold:
    • (S_1,S_2,\ldots,S_{i-1}) = (T_1,T_2,\ldots,T_{i-1}).
    • S_i is smaller than T_i (as a number).

Constraints

  • All input values are integers.
  • 2 \leq N \leq 5 \times 10^5
  • 0 \leq L_i \leq R_i \leq 10^9

Input

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

N
L_1 R_1
L_2 R_2
\vdots
L_N R_N

Output

Print the answer in a single line in the following format:

A_1 A_2 \ldots A_N

Sample Input 1

4
1 10
8 13
3 4
5 20

Sample Output 1

8 8 4 5

(A_1, A_2, A_3, A_4) = (8, 8, 4, 5) is a good integer sequence. In this case, \sum_{i = 1}^{N-1} |A_{i+1} - A_{i}| = |8 - 8| + |4 - 8| + |5 - 4| = 5, which is the minimum value of \sum_{i = 1}^{N-1} |A_{i+1} - A_{i}|.


Sample Input 2

3
20 24
3 24
1 75

Sample Output 2

20 20 20

Note that when multiple good integer sequences A minimize \sum_{i = 1}^{N-1} |A_{i+1} - A_{i}|, you should print the lexicographically smallest of them.


Sample Input 3

15
335279264 849598327
446755913 822889311
526239859 548830120
181424399 715477619
342858071 625711486
448565595 480845266
467825612 647639160
160714711 449656269
336869678 545923679
61020590 573085537
626006012 816372580
135599877 389312924
511429216 547865075
561330066 605997004
539239436 921749002

Sample Output 3

526239859 526239859 526239859 467825612 467825612 467825612 467825612 449656269 449656269 449656269 626006012 389312924 511429216 561330066 561330066
D - LIS on Tree 2

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 700

問題文

頂点に 1 から N の番号がついた N 頂点の木があります.木の i 番目の辺は頂点 u_i と頂点 v_i を双方向に結んでいます.

(1,\ldots,N) の順列 P=(P_1,\ldots,P_N) に対し f(P) を以下で定めます.

  • 頂点 i\ (1\leq i\leq N) に対し,頂点 1 から頂点 i への単純パスを (v_1=1,v_2,\ldots,v_k=i) として,(P_{v_1},P_{v_2},\ldots,P_{v_k}) の最長増加部分列の長さを L_i とする.f(P) = \sum_{i=1}^N L_i と定める.

整数 K が与えられます.f(P)=K を満たす (1,\ldots,N) の順列 P が存在するか判定し,存在する場合は一つ示してください.

最長増加部分列とは 数列の部分列とは,数列から 0 個以上の要素を取り除いた後,残りの要素を元の順序で連結して得られる数列のことをいいます. 例えば,(10,30)(10,20,30) の部分列ですが,(20,10)(10,20,30) の部分列ではありません.
数列の最長増加部分列とは,数列の狭義単調増加な部分列の中で列の長さが最大のものを指します.
単純パスとは グラフ G 上の頂点 X,Y に対して,頂点列 (v_1,v_2, \ldots, v_k) であって, v_1=X, v_k=Y かつ,1\leq i\leq k-1 に対して v_iv_{i+1} が辺で結ばれているようなものを頂点 X から頂点 Y への ウォーク と呼びます. さらに,v_1,v_2, \ldots, v_k がすべて異なるようなものを頂点 X から頂点 Y への 単純パス (あるいは単に パス) と呼びます.

制約

  • 入力される数値は全て整数
  • 2 \leq N \leq 2\times 10^5
  • 1\leq K \leq 10^{11}
  • 1\leq u_i,v_i\leq N
  • 与えられるグラフは木

入力

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

N K
u_1 v_1
\vdots
u_{N-1} v_{N-1}

出力

f(P)=K を満たす順列 P が存在しない場合, No を出力せよ.

f(P)=K を満たす順列 P が存在する場合,以下の形式で出力せよ.

Yes
P_1 \ldots P_N

条件を満たす順列 P が複数存在する場合,どれを出力しても正解とみなされる.


入力例 1

5 8
1 2
2 3
2 4
4 5

出力例 1

Yes
3 2 1 4 5

P=(3,2,1,4,5) のとき,f(P) は以下のように定まります.

  • 頂点 1 から頂点 1 への単純パスは (1) であり,(P_1)=(3) の最長増加部分列の長さは 1 である.よって L_1 = 1 である.

  • 頂点 1 から頂点 2 への単純パスは (1,2) であり,(P_1,P_2)=(3,2) の最長増加部分列の長さは 1 である.よって L_2 = 1 である.

  • 頂点 1 から頂点 3 への単純パスは (1,2,3) であり,(P_1,P_2,P_3)=(3,2,1) の最長増加部分列の長さは 1 である.よって L_3 = 1 である.

  • 頂点 1 から頂点 4 への単純パスは (1,2,4) であり,(P_1,P_2,P_4)=(3,2,4) の最長増加部分列の長さは 2 である.よって L_4 = 2 である.

  • 頂点 1 から頂点 5 への単純パスは (1,2,4,5) であり,(P_1,P_2,P_4,P_5)=(3,2,4,5) の最長増加部分列の長さは 3 である.よって L_5 = 3 である.

  • 以上より,f(P)=1+1+1+2+3= 8 である.

このことから,出力例の Pf(P)=8 という条件を満たすことが分かります.この他にも,例えば P=(3,2,4,5,1) も条件を満たします.


入力例 2

7 21
2 1
7 2
5 1
3 7
2 6
3 4

出力例 2

No

f(P) = 21 を満たす順列 P は存在しないことが証明できます.


入力例 3

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

出力例 3

Yes
2 1 3 5 6 8 4 7

Score: 700 points

Problem Statement

There is a tree with N vertices numbered 1 to N. The i-th edge of the tree connects vertices u_i and v_i bidirectionally.

For a permutation P=(P_1,\ldots,P_N) of (1,\ldots,N), we define f(P) as follows:

  • For each vertex i\ (1\leq i\leq N), let (v_1=1,v_2,\ldots,v_k=i) be the simple path from vertex 1 to vertex i, and let L_i be the length of a longest increasing subsequence of (P_{v_1},P_{v_2},\ldots,P_{v_k}). We define f(P) = \sum_{i=1}^N L_i.

You are given an integer K. Determine whether there is a permutation P of (1,\ldots,N) such that f(P)=K. If it exists, present one such permutation.

What is a longest increasing subsequence? A subsequence of a sequence is a sequence obtained by removing zero or more elements from the original sequence and concatenating the remaining elements without changing the order. For example, (10,30) is a subsequence of (10,20,30), but (20,10) is not.
The longest increasing subsequence of a sequence is the longest strictly increasing subsequence of that sequence.
What is a simple path? For vertices X and Y in a graph G, a walk from vertex X to vertex Y is a sequence of vertices (v_1,v_2, \ldots, v_k) such that v_1=X, v_k=Y, and v_i and v_{i+1} are connected by an edge for all 1\leq i\leq k-1. Furthermore, if v_1,v_2, \ldots, v_k are all distinct, it is called a simple path (or simply a path) from vertex X to vertex Y.

Constraints

  • All input values are integers.
  • 2 \leq N \leq 2\times 10^5
  • 1\leq K \leq 10^{11}
  • 1\leq u_i,v_i\leq N
  • The given graph is a tree.

Input

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

N K
u_1 v_1
\vdots
u_{N-1} v_{N-1}

Output

If there is no permutation P such that f(P)=K, print No.

If there is a permutation P such that f(P)=K, print it in the following format:

Yes
P_1 \ldots P_N

If multiple permutations P satisfy the condition, any of them will be accepted.


Sample Input 1

5 8
1 2
2 3
2 4
4 5

Sample Output 1

Yes
3 2 1 4 5

If P=(3,2,1,4,5), then f(P) is determined as follows:

  • The simple path from vertex 1 to vertex 1 is (1), and the length of the longest increasing subsequence of (P_1)=(3) is 1. Thus, L_1 = 1.

  • The simple path from vertex 1 to vertex 2 is (1,2), and the length of the longest increasing subsequence of (P_1,P_2)=(3,2) is 1. Thus, L_2 = 1.

  • The simple path from vertex 1 to vertex 3 is (1,2,3), and the length of the longest increasing subsequence of (P_1,P_2,P_3)=(3,2,1) is 1. Thus, L_3 = 1.

  • The simple path from vertex 1 to vertex 4 is (1,2,4), and the length of the longest increasing subsequence of (P_1,P_2,P_4)=(3,2,4) is 2. Thus, L_4 = 2.

  • The simple path from vertex 1 to vertex 5 is (1,2,4,5), and the length of the longest increasing subsequence of (P_1,P_2,P_4,P_5)=(3,2,4,5) is 3. Thus, L_5 = 3.

  • From the above, f(P)=1+1+1+2+3= 8.

Hence, the permutation P in the sample output satisfies the condition f(P)=8. Besides, P=(3,2,4,5,1) also satisfies the condition, for example.


Sample Input 2

7 21
2 1
7 2
5 1
3 7
2 6
3 4

Sample Output 2

No

It can be proved that no permutation P satisfies f(P) = 21.


Sample Input 3

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

Sample Output 3

Yes
2 1 3 5 6 8 4 7
E - Three View Drawing

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 800

問題文

1 辺の長さが N の立方体を,1 辺の長さが 1 の立方体 N^3 個に分割し,そこから K 個選びます. 立方体の面に垂直な 3 方向のうちどの方向から見ても,選んだ K 個の立方体がすべて見え,なおかつ同じ形で見えるような選び方を 1 つ構成してください.

問題を厳密に定式化するために,分割後の各立方体を整数の 3 つ組 (x_i, y_i, z_i) に対応させます.

以下の条件を満たす K 個の整数の 3 つ組 (x_i, y_i, z_i)1 つ構成し,出力してください.

  • 0 \leq x_i, y_i, z_i < N
  • \left\lbrace (x_i, y_i) \, \middle| \, 1 \le i \le K \right\rbrace = \left\lbrace (y_i, z_i) \, \middle| \, 1 \le i \le K \right\rbrace = \left\lbrace (z_i, x_i) \, \middle| \, 1 \le i \le K \right\rbrace
  • 前項の集合は K 個の要素を持つ.つまり,i \neq j に対し (x_i, y_i) \neq (x_j, y_j) となる.

なお,制約を満たす任意の入力に対して,条件を満たす答えが存在することが示せます.

制約

  • 入力される数値は全て整数
  • 1 \leq N \leq 500
  • 1 \leq K \leq N^2

入力

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

N K

出力

答えを以下の形式で出力せよ.

x_1 y_1 z_1
x_2 y_2 z_2
\vdots
x_K y_K z_K

解が複数存在する場合,どれを出力しても正解とみなされる.


入力例 1

3 3

出力例 1

0 0 0
1 1 1
2 2 2

入力例 2

2 4

出力例 2

0 0 1
0 1 0
1 0 0
1 1 1

入力例 3

1 1

出力例 3

0 0 0

Score: 800 points

Problem Statement

Divide a cube with a side length of N into N^3 smaller cubes, each with a side length of 1, and select K of these smaller cubes. Construct one way to select them so that, when viewed from any of the three directions perpendicular to the faces of the cubes, all K selected cubes are visible and appear in the same shape.

To formulate the problem precisely, we associate each smaller cube after division with a triple of integers (x_i, y_i, z_i).

Construct and print one set of K triples of integers (x_i, y_i, z_i) that satisfy the following conditions.

  • 0 \leq x_i, y_i, z_i < N
  • \left\lbrace (x_i, y_i) \, \middle| \, 1 \le i \le K \right\rbrace = \left\lbrace (y_i, z_i) \, \middle| \, 1 \le i \le K \right\rbrace = \left\lbrace (z_i, x_i) \, \middle| \, 1 \le i \le K \right\rbrace
  • The set mentioned in the previous item has K elements. That is, (x_i, y_i) \neq (x_j, y_j) for i \neq j.

It can be shown that a solution exists for any input satisfying the constraints.

Constraints

  • All input values are integers.
  • 1 \leq N \leq 500
  • 1 \leq K \leq N^2

Input

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

N K

Output

Print your answer in the following format:

x_1 y_1 z_1
x_2 y_2 z_2
\vdots
x_K y_K z_K

If multiple solutions exist, any of them will be accepted.


Sample Input 1

3 3

Sample Output 1

0 0 0
1 1 1
2 2 2

Sample Input 2

2 4

Sample Output 2

0 0 1
0 1 0
1 0 0
1 1 1

Sample Input 3

1 1

Sample Output 3

0 0 0
F - Append Same Characters

Time Limit: 4 sec / Memory Limit: 1024 MB

配点 : 1000

問題文

英小文字からなる N 個の文字列 S_1, \dots, S_N が与えられます.以下の 2 種類の操作を好きな順番で 0 回以上好きな回数行うことを考えます.

  • 英小文字 c1 つ選ぶ.全ての 1 \leq i\leq N について,S_i の末尾に c を追加する.
  • 1 \leq i \leq N-1 を満たす整数 i1 つ選ぶ.S_iS_{i+1} を入れ替える.

全ての操作の終了後に,辞書順で S_i \leq S_{i+1} が各 1 \leq i \leq N-1 について成り立つようにするとき,操作の合計回数の最小値を求めてください.

辞書順とは

文字列 S = S_1S_2\ldots S_{|S|} が文字列 T = T_1T_2\ldots T_{|T|} より辞書順で小さいとは,下記の 1. と 2. のどちらかが成り立つことを言います. ここで,|S|, |T| はそれぞれ S, T の長さを表します.

  1. |S| \lt |T| かつ S_1S_2\ldots S_{|S|} = T_1T_2\ldots T_{|S|}
  2. ある整数 1 \leq i \leq \min\lbrace |S|, |T| \rbrace が存在して,下記の 2 つがともに成り立つ.
    • S_1S_2\ldots S_{i-1} = T_1T_2\ldots T_{i-1}
    • S_iT_i よりアルファベット順で小さい文字である.

制約

  • 入力される数値は全て整数
  • 2 \le N \le 3 \times 10^5
  • S_i は英小文字からなる文字列
  • 1 \le |S_i|
  • |S_1| + |S_2| + \dots + |S_N| \le 3 \times 10^5

入力

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

N
S_1
S_2
\vdots
S_N

出力

答えを 1 行に出力せよ.


入力例 1

5
ab
rac
a
dab
ra

出力例 1

3

操作の一例を示します.

  • S_2S_3 を入れ替える.(S_1, \ldots, S_5) = (ab, a, rac, dab, ra) となる.
  • 各文字列の末尾に z を追加する.(S_1, \ldots, S_5) = (abz, az, racz, dabz, raz) となる.
  • S_3S_4 を入れ替える.(S_1, \ldots, S_5) = (abz, az, dabz, racz, raz) となる.このとき全ての i = 1, \ldots, N-1 について S_i \leq S_{i+1} が満たされている.

3 回未満の操作により,全ての i = 1, \ldots, N-1 について S_i \leq S_{i+1} が満たされている状態にすることはできないので,3 を出力します.


入力例 2

3
kitekuma
nok
zkou

出力例 2

0

操作を行う前の時点で,全ての i = 1, \ldots, N-1 について S_i \leq S_{i+1} が満たされています.


入力例 3

31
arc
arrc
rc
rac
a
rc
aara
ra
caac
cr
carr
rrra
ac
r
ccr
a
c
aa
acc
rar
r
c
r
a
r
rc
a
r
rc
cr
c

出力例 3

175

i \neq j に対して S_i = S_j となりうることに注意してください.

Score: 1000 points

Problem Statement

You are given N strings S_1, \dots, S_N consisting of lowercase English letters. Consider performing the following two types of operations zero or more times in any order:

  • Choose one lowercase letter c. Append c to the end of S_i for all 1 \leq i \leq N.
  • Choose an integer i such that 1 \leq i \leq N-1. Swap S_i and S_{i+1}.

Find the minimum total number of operations required to make S_i \leq S_{i+1} in lexicographical order for all 1 \leq i \leq N-1.

What is lexicographical order?

A string S = S_1S_2\ldots S_{|S|} is said to be lexicographically smaller than a string T = T_1T_2\ldots T_{|T|} if 1. or 2. below holds. Here, |S| and |T| denote the lengths of S and T, respectively.

  1. |S| \lt |T| and S_1S_2\ldots S_{|S|} = T_1T_2\ldots T_{|S|}.
  2. There is an integer 1 \leq i \leq \min\lbrace |S|, |T| \rbrace such that both of the following hold:
    • S_1S_2\ldots S_{i-1} = T_1T_2\ldots T_{i-1}.
    • The letter S_i comes before T_i in alphabetical order.

Constraints

  • All input values are integers.
  • 2 \le N \le 3 \times 10^5
  • S_i is a string consisting of lowercase English letters.
  • 1 \le |S_i|
  • |S_1| + |S_2| + \dots + |S_N| \le 3 \times 10^5

Input

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

N
S_1
S_2
\vdots
S_N

Output

Print the answer in a single line.


Sample Input 1

5
ab
rac
a
dab
ra

Sample Output 1

3

Here is one way to operate.

  • Swap S_2 and S_3. Now (S_1, \ldots, S_5) = (ab, a, rac, dab, ra).
  • Append z to the end of each string. Now (S_1, \ldots, S_5) = (abz, az, racz, dabz, raz).
  • Swap S_3 and S_4. Now (S_1, \ldots, S_5) = (abz, az, dabz, racz, raz). At this point, we have S_i \leq S_{i+1} for all i = 1, \ldots, N-1.

It is impossible to make S_i \leq S_{i+1} for all i = 1, \ldots, N-1 with fewer than three operations, so you should print 3.


Sample Input 2

3
kitekuma
nok
zkou

Sample Output 2

0

Before any operation is performed, we have S_i \leq S_{i+1} for all i = 1, \ldots, N-1.


Sample Input 3

31
arc
arrc
rc
rac
a
rc
aara
ra
caac
cr
carr
rrra
ac
r
ccr
a
c
aa
acc
rar
r
c
r
a
r
rc
a
r
rc
cr
c

Sample Output 3

175

Note that we may have S_i = S_j for i \neq j.