A - Move Right

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

横一列に 4 つのマスが並んでいます。

各文字が 0 または 1 である長さ 4 の文字列 S が与えられます。
Si 文字目が 1 であるとき、左から i 番目のマスには 1 人の人がおり、
Si 文字目が 0 であるとき、左から i 番目のマスには人がいません。

全ての人が一斉に、1 つ右隣のマスへ移動します。この移動により、もともと右端のマスにいた人は消えます。

移動後の各マスに人がいるかどうかを、S と同様のルールで文字列として出力してください。

制約

  • S0, 1 のみからなる長さ 4 の文字列

入力

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

S

出力

長さ 4 の文字列であって、移動後、左から i 番目のマスに人がいるならば i 文字目が 1、いないならば 0 であるようなものを出力せよ。


入力例 1

1011

出力例 1

0101

移動により、左から 1 番目のマスにいた人は左から 2 番目のマスに、
左から 3 番目のマスにいた人は左から 4 番目のマスに移動し、
左から 4 番目のマス(右端のマス)にいた人は消えます。


入力例 2

0000

出力例 2

0000

入力例 3

1111

出力例 3

0111

Score : 100 points

Problem Statement

There are 4 squares lined up horizontally.

You are given a string S of length 4 consisting of 0 and 1.
If the i-th character of S is 1, there is a person in the i-th square from the left;
if the i-th character of S is 0, there is no person in the i-th square from the left.

Now, everyone will move to the next square to the right simultaneously. By this move, the person who was originally in the rightmost square will disappear.

Determine if there will be a person in each square after the move. Print the result as a string in the same format as S. (See also Sample Input / Output for clarity.)

Constraints

  • S is a string of length 4 consisting of 0 and 1.

Input

Input is given from Standard Input in the following format:

S

Output

Print a string of length 4 such that the i-th character is 1 if there will be a person in the i-th square from the left after the move, and 0 otherwise.


Sample Input 1

1011

Sample Output 1

0101

After the move, the person who was originally in the 1-st square will move to the 2-nd square,
the person in the 3-rd square to the 4-th square,
and the person in the 4-th square will disappear.


Sample Input 2

0000

Sample Output 2

0000

Sample Input 3

1111

Sample Output 3

0111
B - Unique Nicknames

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

1, 人 2, \dotsNN 人の人がいます。人 i の姓は s_i、名は t_i です。

N 人の人すべてにあだ名をつけることを考えます。人 i のあだ名 a_i は以下の条件を満たす必要があります。

  • a_i は人 i の姓あるいは名と一致する。言い換えると、a_i = s_i または a_i = t_i の少なくとも一方が成り立つ。
  • a_i は自分以外の人の姓および名のどちらとも一致しない。言い換えると、1 \leq j \leq N, i \neq j を満たすすべての整数 j について a_i \neq s_j かつ a_i \neq t_j が成り立つ。

N 人全員に条件を満たすあだ名をつけることは可能でしょうか。可能ならば Yes を、そうでないならば No を出力してください。

制約

  • 2 \leq N \leq 100
  • N は整数である。
  • s_i,t_i は英小文字からなる 1 文字以上 10 文字以下の文字列である。

入力

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

N
s_1 t_1
s_2 t_2
\vdots
s_N t_N

出力

N 人すべてにあだ名をつけることが可能ならば Yes を、そうでないならば No を出力せよ。


入力例 1

3
tanaka taro
tanaka jiro
suzuki hanako

出力例 1

Yes

a_1 = taro, a_2 = jiro, a_3 = hanako とすれば、これは問題文にあるあだ名の条件を満たしています。(a_3suzuki でもよいです。)
ここで、a_1 = tanaka とはできないことに注意してください。なぜならば 人 2 の姓 s_2 もまた tanaka であるため、あだ名の条件の 2 つ目を満たさなくなるからです。


入力例 2

3
aaa bbb
xxx aaa
bbb yyy

出力例 2

No

問題文の条件を満たすあだ名のつけ方は存在しません。


入力例 3

2
tanaka taro
tanaka taro

出力例 3

No

同姓同名である人の組が存在する場合もあります。


入力例 4

3
takahashi chokudai
aoki kensho
snu ke

出力例 4

Yes

a_1 = chokudai, a_2 = kensho, a_3 = ke とすればよいです。

Score : 200 points

Problem Statement

There are N people numbered Person 1, Person 2, \dots, and Person N. Person i has a family name s_i and a given name t_i.

Consider giving a nickname to each of the N people. Person i's nickname a_i should satisfy all the conditions below.

  • a_i coincides with Person i's family name or given name. In other words, a_i = s_i and/or a_i = t_i holds.
  • a_i does not coincide with the family name and the given name of any other person. In other words, for all integer j such that 1 \leq j \leq N and i \neq j, it holds that a_i \neq s_j and a_i \neq t_j.

Is it possible to give nicknames to all the N people? If it is possible, print Yes; otherwise, print No.

Constraints

  • 2 \leq N \leq 100
  • N is an integer.
  • s_i and t_i are strings of lengths between 1 and 10 (inclusive) consisting of lowercase English alphabets.

Input

Input is given from Standard Input in the following format:

N
s_1 t_1
s_2 t_2
\vdots
s_N t_N

Output

If it is possible to give nicknames to all the N people, print Yes; otherwise, print No.


Sample Input 1

3
tanaka taro
tanaka jiro
suzuki hanako

Sample Output 1

Yes

The following assignment satisfies the conditions of nicknames described in the Problem Statement: a_1 = taro, a_2 = jiro, a_3 = hanako. (a_3 may be suzuki, too.)
However, note that we cannot let a_1 = tanaka, which violates the second condition of nicknames, since Person 2's family name s_2 is tanaka too.


Sample Input 2

3
aaa bbb
xxx aaa
bbb yyy

Sample Output 2

No

There is no way to give nicknames satisfying the conditions in the Problem Statement.


Sample Input 3

2
tanaka taro
tanaka taro

Sample Output 3

No

There may be a pair of people with the same family name and the same given name.


Sample Input 4

3
takahashi chokudai
aoki kensho
snu ke

Sample Output 4

Yes

We can let a_1 = chokudai, a_2 = kensho, and a_3 = ke.

C - 1 2 1 3 1 2 1

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

S_n を次のように定義します。

  • S_11 つの 1 からなる長さ 1 の列である。
  • S_n (n2 以上の整数) は S_{n-1}, n, S_{n-1} をこの順につなげた列である。

たとえば S_2,S_3 は次のような列です。

  • S_2S_1, 2, S_1 をこの順につなげた列なので 1,2,1 である。
  • S_3S_2, 3, S_2 をこの順につなげた列なので 1,2,1,3,1,2,1 である。

N が与えられるので、列 S_N をすべて出力してください。

制約

  • N は整数
  • 1 \leq N \leq 16

入力

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

N

出力

S_N を空白区切りで出力せよ。


入力例 1

2

出力例 1

1 2 1

問題文の説明にある通り、S_21,2,1 となります。


入力例 2

1

出力例 2

1

入力例 3

4

出力例 3

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

S_4S_3,4,S_3 をこの順につなげた列です。

Score : 300 points

Problem Statement

We define sequences S_n as follows.

  • S_1 is a sequence of length 1 containing a single 1.
  • S_n (n is an integer greater than or equal to 2) is a sequence obtained by concatenating S_{n-1}, n, S_{n-1} in this order.

For example, S_2 and S_3 is defined as follows.

  • S_2 is a concatenation of S_1, 2, and S_1, in this order, so it is 1,2,1.
  • S_3 is a concatenation of S_2, 3, and S_2, in this order, so it is 1,2,1,3,1,2,1.

Given N, print the entire sequence S_N.

Constraints

  • N is an integer.
  • 1 \leq N \leq 16

Input

Input is given from Standard Input in the following format:

N

Output

Print S_N, with spaces in between.


Sample Input 1

2

Sample Output 1

1 2 1

As described in the Problem Statement, S_2 is 1,2,1.


Sample Input 2

1

Sample Output 2

1

Sample Input 3

4

Sample Output 3

1 2 1 3 1 2 1 4 1 2 1 3 1 2 1
  • S_4 is a concatenation of S_3, 4, and S_3, in this order.
D - Cylinder

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

空の筒があります。Q 個のクエリが与えられるので順に処理してください。
クエリは次の 2 種類のいずれかです。

  • 1 x c:数 x が書かれたボールを筒の右側から c 個入れる
  • 2 c:筒の左側からボールを c 個取り出し、取り出したボールに書かれている数の合計を出力する

なお、筒の中でボールの順序が入れ替わることはないものとします。

制約

  • 1 \leq Q \leq 2\times 10^5
  • 0 \leq x \leq 10^9
  • 1 \leq c \leq 10^9
  • 2 c のクエリが与えられるとき、筒の中には c 個以上のボールがある
  • 入力に含まれる値は全て整数である

入力

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

Q
{\rm query}_1
\vdots
{\rm query}_Q

i 番目のクエリを表す {\rm query}_i は以下の 2 種類のいずれかである。

1 x c
2 c

出力

2 c のクエリに対する答えを順に改行区切りで出力せよ。


入力例 1

4
1 2 3
2 2
1 3 4
2 3

出力例 1

4
8
  • 1 番目のクエリでは、2 が書かれたボールを筒の右側から 3 個入れます。
    筒の中のボールに書かれた数は左から順に (2,2,2) となります。
  • 2 番目のクエリでは、筒の左側からボールを 2 個取り出します。
    取り出されたボールに書かれている数はそれぞれ 2,2 であり、合計は 4 であるため、これを出力します。 筒の中のボールに書かれた数は左から順に (2) となります。
  • 3 番目のクエリでは、3 が書かれたボールを筒の右側から 4 個入れます。
    筒の中のボールに書かれた数は左から順に (2,3,3,3,3) となります。
  • 4 番目のクエリでは、筒の左側からボールを 3 個取り出します。
    取り出されたボールに書かれている数はそれぞれ 2,3,3 であり、合計は 8 であるため、これを出力します。 筒の中のボールに書かれた数は左から順に (3,3) となります。

入力例 2

2
1 1000000000 1000000000
2 1000000000

出力例 2

1000000000000000000

入力例 3

5
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1

出力例 3


出力するものがないこともあります。

Score : 400 points

Problem Statement

We have a horizontal cylinder. Given Q queries, process them in the given order.
Each query is of one of the following two types.

  • 1 x c: Insert c balls, with a number x written on each of them, to the right end of the cylinder.
  • 2 c: Take out the c leftmost balls contained in the cylinder and print the sum of the numbers written on the balls that have been taken out.

We assume that the balls do never change their order in the cylinder.

Constraints

  • 1 \leq Q \leq 2\times 10^5
  • 0 \leq x \leq 10^9
  • 1 \leq c \leq 10^9
  • Whenever a query of type 2 c is given, there are c or more balls in the cylinder.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

Q
{\rm query}_1
\vdots
{\rm query}_Q

The i-th query {\rm query}_i is in one of the following two formats.

1 x c
2 c

Output

Print the response to the queries of type 2 c in the given order, with newlines in between.


Sample Input 1

4
1 2 3
2 2
1 3 4
2 3

Sample Output 1

4
8
  • For the 1-st query, insert 3 balls, with a number 2 written on each of them, to the right end of the cylinder.
    The cylinder has now balls with numbers (2,2,2) written on them, from left to right.
  • For the 2-nd query, take out the 2 leftmost balls contained in the cylinder.
    The numbers written on the balls taken out are 2,2, for a sum of 4, which should be printed. The cylinder has now a ball with a number (2) written on it, from left to right.
  • For the 3-rd query, insert 4 balls, with a number 3 written on each of them, to the right end of the cylinder.
    The cylinder has now balls with numbers (2,3,3,3,3) written on them, from left to right.
  • For the 4-th query, take out the 3 leftmost balls contained in the cylinder.
    The numbers written on the balls taken out are 2,3,3, for a sum of 8, which should be printed. The cylinder has now balls with numbers (3,3) written on them, from left to right.

Sample Input 2

2
1 1000000000 1000000000
2 1000000000

Sample Output 2

1000000000000000000

Sample Input 3

5
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1

Sample Output 3


There may be nothing you should print.

E - Max Min

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

長さ N の数列 A = (A_1, A_2, \dots, A_N) および整数 X, Y があります。 次の条件をすべて満たす整数の組 (L, R) の個数を求めてください。

  • 1 \leq L \leq R \leq N
  • A_L, A_{L+1}, \dots, A_R の最大値は X であり、最小値は Y である。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 2 \times 10^5
  • 1 \leq Y \leq X \leq 2 \times 10^5
  • 入力される値はすべて整数である。

入力

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

N X Y
A_1 A_2 \dots A_N

出力

答えを出力せよ。


入力例 1

4 3 1
1 2 3 1

出力例 1

4

条件を満たすのは (L,R)=(1,3),(1,4),(2,4),(3,4)4 通りです。


入力例 2

5 2 1
1 3 2 4 1

出力例 2

0

条件を満たす (L,R) は存在しません。


入力例 3

5 1 1
1 1 1 1 1

出力例 3

15

X=Y である場合もあります。


入力例 4

10 8 1
2 7 1 8 2 8 1 8 2 8

出力例 4

36

Score : 500 points

Problem Statement

We have a number sequence A = (A_1, A_2, \dots, A_N) of length N and integers X and Y. Find the number of pairs of integers (L, R) satisfying all the conditions below.

  • 1 \leq L \leq R \leq N
  • The maximum value of A_L, A_{L+1}, \dots, A_R is X, and the minimum is Y.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 2 \times 10^5
  • 1 \leq Y \leq X \leq 2 \times 10^5
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X Y
A_1 A_2 \dots A_N

Output

Print the answer.


Sample Input 1

4 3 1
1 2 3 1

Sample Output 1

4

4 pairs satisfy the conditions: (L,R)=(1,3),(1,4),(2,4),(3,4).


Sample Input 2

5 2 1
1 3 2 4 1

Sample Output 2

0

No pair (L,R) satisfies the condition.


Sample Input 3

5 1 1
1 1 1 1 1

Sample Output 3

15

It may hold that X=Y.


Sample Input 4

10 8 1
2 7 1 8 2 8 1 8 2 8

Sample Output 4

36
F - Cards

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

1,\ldots,N の番号がついた N 枚のカードがあり、カード i の表には P_i が、裏には Q_i が書かれています。
ここで、P=(P_1,\ldots,P_N) 及び Q=(Q_1,\ldots,Q_N) はそれぞれ (1, 2, \dots, N) の並び替えです。

N 枚のカードから何枚かを選ぶ方法のうち、次の条件を満たすものは何通りありますか? 998244353 で割った余りを求めてください。

条件:1,2,\ldots,N のどの数も選んだカードのいずれかに書かれている

制約

  • 1 \leq N \leq 2\times 10^5
  • 1 \leq P_i,Q_i \leq N
  • P,Q はそれぞれ (1, 2, \dots, N) の並び替えである
  • 入力に含まれる値は全て整数である

入力

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

N
P_1 P_2 \ldots P_N
Q_1 Q_2 \ldots Q_N

出力

答えを出力せよ。


入力例 1

3
1 2 3
2 1 3

出力例 1

3

例えばカード 1,3 を選ぶと、1 はカード 1 の表に、2 はカード 1 の裏に、3 はカード 3 の表に書かれているため条件を満たします。

条件を満たすカードの選び方は \{1,3\},\{2,3\},\{1,2,3\}3 通りです。


入力例 2

5
2 3 5 4 1
4 2 1 3 5

出力例 2

12

入力例 3

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

出力例 3

1

Score : 500 points

Problem Statement

There are N cards numbered 1,\ldots,N. Card i has P_i written on the front and Q_i written on the back.
Here, P=(P_1,\ldots,P_N) and Q=(Q_1,\ldots,Q_N) are permutations of (1, 2, \dots, N).

How many ways are there to choose some of the N cards such that the following condition is satisfied? Find the count modulo 998244353.

Condition: every number 1,2,\ldots,N is written on at least one of the chosen cards.

Constraints

  • 1 \leq N \leq 2\times 10^5
  • 1 \leq P_i,Q_i \leq N
  • P and Q are permutations of (1, 2, \dots, N).
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
P_1 P_2 \ldots P_N
Q_1 Q_2 \ldots Q_N

Output

Print the answer.


Sample Input 1

3
1 2 3
2 1 3

Sample Output 1

3

For example, if you choose Cards 1 and 3, then 1 is written on the front of Card 1, 2 on the back of Card 1, and 3 on the front of Card 3, so this combination satisfies the condition.

There are 3 ways to choose cards satisfying the condition: \{1,3\},\{2,3\},\{1,2,3\}.


Sample Input 2

5
2 3 5 4 1
4 2 1 3 5

Sample Output 2

12

Sample Input 3

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

Sample Output 3

1
G - Dream Team

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

N 人の競技プログラマがいます。
i 人目の競技プログラマは、所属大学が A_i、得意分野が B_i、強さが C_i です。

N 人のうちの何人かによって構成されるチームであって、次の条件をともに満たすものをドリームチームと呼びます。

  • チームに属するどの 2 人の所属大学も異なる
  • チームに属するどの 2 人の得意分野も異なる

構成可能なドリームチームの人数の最大値を k とします。各 i=1,2,\ldots,k について、次の問題を解いてください。

問題:ちょうど i 人で構成されるドリームチームについて、チームに所属する人の強さの合計の最大値を求めてください。

制約

  • 1 \leq N \leq 3\times 10^4
  • 1 \leq A_i,B_i \leq 150
  • 1 \leq C_i \leq 10^9
  • 入力に含まれる値は全て整数である

入力

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

N
A_1 B_1 C_1
A_2 B_2 C_2
\vdots
A_N B_N C_N

出力

構成可能なドリームチームの人数の最大値を k とする。
1行目には k を出力し、その後、k 行にわたって、i=1,2,\ldots,k のときの問題に対する答えを順に出力せよ。


入力例 1

3
1 1 100
1 20 10
2 1 1

出力例 1

2
100
11
  • ちょうど 1 人で構成されるドリームチームは、1 人目の競技プログラマからなるとき強さの合計が 100 で最大になります。
  • ちょうど 2 人で構成されるドリームチームは、2,3 人目の競技プログラマからなるとき強さの合計が 11 で最大になります。
  • ちょうど 3 人で構成されるドリームチームを作ることはできません。

入力例 2

10
1 4 142135623
2 6 457513110
3 1 622776601
5 1 961524227
2 2 360679774
2 4 494897427
3 7 416573867
5 2 915026221
1 7 320508075
5 3 851648071

出力例 2

4
961524227
1537802822
2032700249
2353208324

Score : 600 points

Problem Statement

There are N competitive programmers.
The i-th competitive programmer belongs to University A_i, is good at Subject B_i, and has a power of C_i.

Consider a team consisting of some of the N people. Let us call such a team a dream team if both of the following conditions are satisfied:

  • Any two people belonging to the team belong to different universities.
  • Any two people belonging to the team are good at different subjects.

Let k be the maximum possible number of members of a dream team. For each i=1,2,\ldots,k, solve the following question.

Question: find the maximum sum of power of people belonging to a dream team consisting of i people.

Constraints

  • 1 \leq N \leq 3\times 10^4
  • 1 \leq A_i,B_i \leq 150
  • 1 \leq C_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 B_1 C_1
A_2 B_2 C_2
\vdots
A_N B_N C_N

Output

Let k be the maximum possible number of members of a dream team.
Print k in the first line. Then, print k more lines, each containing the answer to the question for i=1,2,\ldots,k, in this order.


Sample Input 1

3
1 1 100
1 20 10
2 1 1

Sample Output 1

2
100
11
  • The sum of power of members of a dream team consisting of exactly 1 person is 100, when the team consists of the 1-st competitive programmer.
  • The sum of power of members of a dream team consisting of exactly 2 people is 11, when the team consists of the 2-nd and the 3-rd competitive programmers.
  • It is impossible to form a dream team consisting of exactly 3 people.

Sample Input 2

10
1 4 142135623
2 6 457513110
3 1 622776601
5 1 961524227
2 2 360679774
2 4 494897427
3 7 416573867
5 2 915026221
1 7 320508075
5 3 851648071

Sample Output 2

4
961524227
1537802822
2032700249
2353208324
Ex - Rearranging Problem

Time Limit: 4 sec / Memory Limit: 1024 MB

配点 : 600

問題文

1, 人 2, \dots, 人 NN 人の人がいて、前から (1,2,\dots,N) の順に一列に並んでいます。人 i は色 c_i の服を着ています。
高橋君は任意の 2i,j を選んで人 i と人 j の位置を入れ替える操作を K 回繰り返しました。
K 回の操作を終了した時点で、1 \leq i \leq N を満たすすべての整数 i に対して、前から i 番目の人が着ている服の色は c_i と一致しました。
K 回の操作を終了した後にあり得る人の並び方は何通りありますか? 答えを 998244353 で割ったあまりを求めてください。

制約

  • 2 \leq N \leq 200000
  • 1 \leq K \leq 10^9
  • 1 \leq c_i \leq N
  • 入力される値はすべて整数である。

入力

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

N K
c_1 c_2 \dots c_N

出力

答えを出力せよ。


入力例 1

4 1
1 1 2 1

出力例 1

3

高橋君の操作、および操作後の列としてあり得るものをすべて挙げると次のようになります。

  • 1 と人 2 の位置を入れ替える。操作後の並び方は (2, 1, 3, 4) となる。
  • 1 と人 4 の位置を入れ替える。操作後の並び方は (4, 2, 3, 1) となる。
  • 2 と人 4 の位置を入れ替える。操作後の並び方は (1, 4, 3, 2) となる。

入力例 2

3 3
1 1 2

出力例 2

1

あり得る高橋君の操作の例を 1 つ挙げると次のようになります。

  • 1 回目の操作で人 1 と人 3 の位置を入れ替える。操作後の並び方は (3, 2, 1) となる。
    2 回目の操作で人 2 と人 3 の位置を入れ替える。操作後の並び方は (2, 3, 1) となる。
    3 回目の操作で人 1 と人 3 の位置を入れ替える。操作後の並び方は (2, 1, 3) となる。

操作の途中においては、前から i 番目の人の服の色が c_i と必ずしも一致しなくてもよいのに注意してください。


入力例 3

10 4
2 7 1 8 2 8 1 8 2 8

出力例 3

132

Score : 600 points

Problem Statement

There are N people called Person 1, Person 2, \dots, Person N, lined up in a row in the order of (1,2,\dots,N) from the front. Person i is wearing Color c_i.
Takahashi repeated the following operation K times: choose two People i and j arbitrarily and swap the positions of Person i and Person j.
After the K operations have ended, the color that the i-th person from the front is wearing coincided with c_i, for every integer i such that 1 \leq i \leq N.
How many possible permutations of people after the K operations are there? Find the count modulo 998244353.

Constraints

  • 2 \leq N \leq 200000
  • 1 \leq K \leq 10^9
  • 1 \leq c_i \leq N
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
c_1 c_2 \dots c_N

Output

Print the answer.


Sample Input 1

4 1
1 1 2 1

Sample Output 1

3

Here is a comprehensive list of possible Takahashi's operations and permutations of people after each operation.

  • Swap the positions of Person 1 and Person 2, resulting in a permutation (2, 1, 3, 4).
  • Swap the positions of Person 1 and Person 4, resulting in a permutation (4, 2, 3, 1).
  • Swap the positions of Person 2 and Person 4, resulting in a permutation (1, 4, 3, 2).

Sample Input 2

3 3
1 1 2

Sample Output 2

1

Here is an example of a possible sequence of Takahashi's operations.

  • In the 1-st operation, he swaps the positions of Person 1 and Person 3, resulting in a permutation (3, 2, 1).
    In the 2-nd operation, he swaps the positions of Person 2 and Person 3, resulting in a permutation (2, 3, 1).
    In the 3-rd operation, he swaps the positions of Person 1 and Person 3, resulting in a permutation (2, 1, 3).

Note that, during the sequence of operations, the color that the i-th person from the front is wearing does not necessarily coincide with c_i.


Sample Input 3

10 4
2 7 1 8 2 8 1 8 2 8

Sample Output 3

132