A - Unsupported Type

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

長さ N の整数列 A=(A_1,A_2,\dots,A_N) と整数 X が与えられます。
XA に含まれるか判定してください。

制約

  • 入力は全て整数
  • 1 \le N \le 100
  • 1 \le A_i \le 100
  • 1 \le X \le 100

入力

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

N
A_1 A_2 \dots A_N
X

出力

XA に含まれるなら Yes 、含まれないなら No と出力せよ。


入力例 1

5
3 1 4 1 5
4

出力例 1

Yes

A=(3,1,4,1,5), X=4 であり、 XA に含まれます。


入力例 2

4
100 100 100 100
100

出力例 2

Yes

X が複数回 A に含まれる場合もあります。


入力例 3

6
2 3 5 7 11 13
1

出力例 3

No

Score : 100 points

Problem Statement

You are given an integer sequence A=(A_1,A_2,\dots,A_N) of length N and an integer X.
Determine whether X is contained in A.

Constraints

  • All input values are integers.
  • 1 \le N \le 100
  • 1 \le A_i \le 100
  • 1 \le X \le 100

Input

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

N
A_1 A_2 \dots A_N
X

Output

If X is contained in A, print Yes; otherwise, print No.


Sample Input 1

5
3 1 4 1 5
4

Sample Output 1

Yes

We have A=(3,1,4,1,5) and X=4; X is contained in A.


Sample Input 2

4
100 100 100 100
100

Sample Output 2

Yes

X may be contained in A multiple times.


Sample Input 3

6
2 3 5 7 11 13
1

Sample Output 3

No
B - Election 2

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

AtCoder 市では市長選挙が行われています。候補者は高橋氏と青木氏です。

2 人のどちらかに投じられた有効票は N 票あり、現在開票が行われています。なお、 N は奇数です。

現在の開票作業の途中経過は高橋氏に T 票、青木氏に A 票です。

現時点で勝敗が確定しているかを判定してください。

制約

  • 1 \leq N \leq 99
  • N は奇数
  • 0 \leq T,A \leq N
  • T+A \leq N
  • 入力はすべて整数

入力

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

N T A

出力

現時点で勝敗が確定しているならば Yes 、そうでなければ No と出力せよ。


入力例 1

7 4 2

出力例 1

Yes

残りの 1 票が青木氏に入っても、高橋氏は勝利します。高橋氏の勝利が確定しているため、Yes と出力します。


入力例 2

99 12 48

出力例 2

No

現時点では青木氏が多く票を獲得していますが、高橋氏が残りの 39 票を獲得すると高橋氏が勝利します。よって、No と出力します。


入力例 3

1 0 0

出力例 3

No

Score : 100 points

Problem Statement

A mayoral election is being held in AtCoder City. The candidates are Takahashi and Aoki.

There are N valid votes cast for either of the two candidates, and the counting is currently underway. Here, N is an odd number.

The current vote count is T votes for Takahashi and A votes for Aoki.

Determine if the outcome of the election is already decided at this point.

Constraints

  • 1 \leq N \leq 99
  • N is an odd number.
  • 0 \leq T, A \leq N
  • T + A \leq N
  • All input values are integers.

Input

The input is given from standard input in the following format:

N T A

Output

Print Yes if the outcome of the election is already decided, and No otherwise.


Sample Input 1

7 4 2

Sample Output 1

Yes

Even if the remaining one vote goes to Aoki, Takahashi will still win. That is, his victory is decided, so print Yes.


Sample Input 2

99 12 48

Sample Output 2

No

Although Aoki currently has more votes, Takahashi would win if he receives the remaining 39 votes. Therefore, print No.


Sample Input 3

1 0 0

Sample Output 3

No
C - Maritozzo

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

英小文字からなる 3 つの文字列 S_1, S_2, S_3 と、123 のみからなる文字列 T が与えられます。

T の各文字に対応する文字列を連結してできる文字列を出力してください。より厳密には、以下の指示にしたがって文字列を出力してください。

  • 1 \leq i \leq |T| を満たす整数 i に対し、文字列 s_i を次のように定める。
    • Ti 文字目が 1 のとき、S_1
    • Ti 文字目が 2 のとき、S_2
    • Ti 文字目が 3 のとき、S_3
  • s_1, s_2, \dots, s_{|T|} をこの順に連結してできる文字列を出力する。

制約

  • 1 \leq |S_1|, |S_2|, |S_3| \leq 10
  • 1 \leq |T| \leq 1000
  • S_1, S_2, S_3 は英小文字からなる。
  • T123 のみからなる。

入力

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

S_1
S_2
S_3
T

出力

答えを出力せよ。


入力例 1

mari
to
zzo
1321

出力例 1

marizzotomari

s_1 = mari, s_2 = zzo, s_3 = to, s_4 = mari であるので、これらを連結してできる文字列である marizzotomari を出力します。


入力例 2

abra
cad
abra
123

出力例 2

abracadabra

入力例 3

a
b
c
1

出力例 3

a

Score : 200 points

Problem Statement

You are given three strings S_1, S_2, S_3 consisting of lowercase English letters, and a string T consisting of 1, 2, 3.

Concatenate the three strings according to the characters in T and print the resulting string. Formally, conform to the following instructions.

  • For each integer i such that 1 \leq i \leq |T|, let the string s_i be defined as follows:
    • S_1, if the i-th character of T is 1;
    • S_2, if the i-th character of T is 2;
    • S_3, if the i-th character of T is 3.
  • Concatenate the strings s_1, s_2, \dots, s_{|T|} in this order and print the resulting string.

Constraints

  • 1 \leq |S_1|, |S_2|, |S_3| \leq 10
  • 1 \leq |T| \leq 1000
  • S_1, S_2, and S_3 consist of lowercase English letters.
  • T consists of 1, 2, and 3.

Input

Input is given from Standard Input in the following format:

S_1
S_2
S_3
T

Output

Print the answer.


Sample Input 1

mari
to
zzo
1321

Sample Output 1

marizzotomari

We have s_1 = mari, s_2 = zzo, s_3 = to, s_4 = mari. Concatenate these and print the resulting string: marizzotomari.


Sample Input 2

abra
cad
abra
123

Sample Output 2

abracadabra

Sample Input 3

a
b
c
1

Sample Output 3

a
D - Card Pile

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

整数 0 の書かれたカードが 100 枚積み重なったカードの山があります。

Q 個のクエリを処理してください。それぞれのクエリは以下のいずれかです。

  • タイプ 1 : 整数 x の書かれたカードを 1 枚カードの山の一番上に積み重ねる。
  • タイプ 2 : カードの山の一番上のカードを取り除き、取り除いたカードに書かれている整数を出力する。ここで、本問題の制約下では必ず山にカードが存在する。

制約

  • 1\le Q\le 100
  • 1\le x\le 100
  • タイプ 2 のクエリが 1 つ以上存在する。
  • 入力される値は全て整数

入力

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

Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

i 番目のクエリ \text{query}_i では、まずクエリのタイプ c_i1,2 のいずれか)が与えられる。 c_i=1 の場合はさらに整数 x が追加で与えられる。

すなわち、各クエリは以下に示す 2 つの形式のいずれかである。

1 x
2

出力

c_i=2 を満たすクエリの回数を q として、 q 行出力せよ。

j (1\le j\le q) 行目では j 番目のそのようなクエリに対する答えを出力せよ。


入力例 1

6
2
1 4
1 3
2
2
2

出力例 1

0
3
4
0

各クエリを処理した後の山は順に以下のようになります:

  • カードの山の一番上のカードを取り除く。取り除いたカードに書かれた整数は 0 であるため、 0 を出力する。
    • カードの山は 0 の書かれたカードが 99 枚となる。
  • 4 が書かれたカードを山の上に追加する。
    • カードの山は上から順に 4 の書かれたカードが 1 枚、 0 の書かれたカードが 99 枚となる。
  • 3 が書かれたカードを山の上に追加する。
    • カードの山は上から順に 3 の書かれたカードが 1 枚、 4 の書かれたカードが 1 枚、 0 の書かれたカードが 99 枚となる。
  • カードの山の一番上のカードを取り除く。取り除いたカードに書かれた整数は 3 であるため、 3 を出力する。
    • カードの山は上から順に 4 の書かれたカードが 1 枚、 0 の書かれたカードが 99 枚となる。
  • カードの山の一番上のカードを取り除く。取り除いたカードに書かれた整数は 4 であるため、 4 を出力する。
    • カードの山は 0 の書かれたカードが 99 枚となる。
  • カードの山の一番上のカードを取り除く。取り除いたカードに書かれた整数は 0 であるため、 0 を出力する。
    • カードの山は 0 の書かれたカードが 98 枚となる。

入力例 2

5
2
2
2
2
2

出力例 2

0
0
0
0
0

Score : 200 points

Problem Statement

There is a stack of 100 cards, each labeled with the integer 0.

Process Q queries. Each query is of one of the following:

  • Type 1: Place a card labeled with an integer x on top of the stack.
  • Type 2: Remove the top card of the stack and output the integer written on that removed card. Under the constraints of this problem, the stack always has at least one card.

Constraints

  • 1 \le Q \le 100
  • 1 \le x \le 100
  • There is at least one query of type 2.
  • All input values are integers.

Input

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

Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

The i-th query \text{query}_i starts with the query type c_i (1 or 2), followed by the integer x if c_i=1.

That is, each query is in one of the following two formats:

1 x
2

Output

Let q be the number of queries with c_i=2. Print q lines.

The j-th line (1 \le j \le q) should contain the answer to the j-th such query.


Sample Input 1

6
2
1 4
1 3
2
2
2

Sample Output 1

0
3
4
0

After processing each query, the stack is as follows:

  • Remove the top card of the stack. The integer on the removed card is 0, so output 0.
    • The stack then has 99 cards labeled with 0.
  • Add a card labeled 4 on top.
    • The stack then has 1 card labeled 4, and 99 cards labeled 0, from top to bottom.
  • Add a card labeled 3 on top.
    • The stack then has 1 card labeled 3, 1 card labeled 4, and 99 cards labeled 0, from top to bottom.
  • Remove the top card. The integer on that card is 3, so output 3.
    • The stack then has 1 card labeled 4, and 99 cards labeled 0, from top to bottom.
  • Remove the top card. The integer on that card is 4, so output 4.
    • The stack then has 99 cards labeled 0.
  • Remove the top card. The integer on that card is 0, so output 0.
    • The stack then has 98 cards labeled 0.

Sample Input 2

5
2
2
2
2
2

Sample Output 2

0
0
0
0
0
E - XX to XXX

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

英小文字からなる 2 つの文字列 S, T が与えられます。 次の操作を好きな回数( 0 回でも良い)行うことで、ST と一致させることができるかを判定してください。

S において同じ文字が 2 文字連続しているところの間に、その文字と同じ文字を 1 つ挿入する。 すなわち、下記の 3 つの手順からなる操作を行う。

  1. 現在の S の長さを N とし、S = S_1S_2\ldots S_N とする。
  2. 1 以上 N-1 以下の整数 i であって、S_i = S_{i+1} を満たすものを 1 つ選択する。(ただし、そのような i が存在しない場合は、何もせずに手順 3.をスキップして操作を終了する。)
  3. Si 文字目と i+1 文字目の間に文字 S_i(= S_{i+1})1 つ挿入する。その結果、S は長さ N+1 の文字列 S_1S_2\ldots S_i S_i S_{i+1} \ldots S_N となる。

制約

  • ST はそれぞれ英小文字からなる長さ 2 以上 2 \times 10^5 以下の文字列

入力

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

S
T

出力

ST と一致させることができる場合は Yes を、そうでない場合は No を出力せよ。 ジャッジは英小文字と英大文字を厳密に区別することに注意せよ。


入力例 1

abbaac
abbbbaaac

出力例 1

Yes

下記の 3 回の操作によって、S = abbaacT = abbbbaaac に一致させることができます。

  • まず、S2 文字目と 3 文字目の間に b を挿入する。その結果、S = abbbaac となる。
  • 次に、再び S2 文字目と 3 文字目の間に b を挿入する。その結果、S = abbbbaac となる。
  • 最後に、S6 文字目と 7 文字目の間に a を挿入する。その結果、S = abbbbaaac となる。

よって、Yes を出力します。


入力例 2

xyzz
xyyzz

出力例 2

No

どのように操作を行っても、 S = xyzzT = xyyzz に一致させることはできません。 よって、No を出力します。

Score : 300 points

Problem Statement

You are given two strings S and T. Determine whether it is possible to make S equal T by performing the following operation some number of times (possibly zero).

Between two consecutive equal characters in S, insert a character equal to these characters. That is, take the following three steps.

  1. Let N be the current length of S, and S = S_1S_2\ldots S_N.
  2. Choose an integer i between 1 and N-1 (inclusive) such that S_i = S_{i+1}. (If there is no such i, do nothing and terminate the operation now, skipping step 3.)
  3. Insert a single copy of the character S_i(= S_{i+1}) between the i-th and (i+1)-th characters of S. Now, S is a string of length N+1: S_1S_2\ldots S_i S_i S_{i+1} \ldots S_N.

Constraints

  • Each of S and T is a string of length between 2 and 2 \times 10^5 (inclusive) consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S
T

Output

If it is possible to make S equal T, print Yes; otherwise, print No. Note that the judge is case-sensitive.


Sample Input 1

abbaac
abbbbaaac

Sample Output 1

Yes

You can make S = abbaac equal T = abbbbaaac by the following three operations.

  • First, insert b between the 2-nd and 3-rd characters of S. Now, S = abbbaac.
  • Next, insert b again between the 2-nd and 3-rd characters of S. Now, S = abbbbaac.
  • Lastly, insert a between the 6-th and 7-th characters of S. Now, S = abbbbaaac.

Thus, Yes should be printed.


Sample Input 2

xyzz
xyyzz

Sample Output 2

No

No sequence of operations makes S = xyzz equal T = xyyzz. Thus, No should be printed.

F - Reorder Cards

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

HW 列の格子状に HW 枚のカードが並べられています。
i=1,\ldots,N について、上から A_i 行目、左から B_i 列目にあるカードには数 i が書かれており、それ以外の HW-N 枚のカードには何も書かれていません。

これらのカードに対し、以下の 2 種類の操作を可能な限り繰り返します。

  • 数の書かれたカードを含まない行が存在するとき、その行のカードを全て取り除き、残りのカードを上へ詰める
  • 数の書かれたカードを含まない列が存在するとき、その列のカードを全て取り除き、残りのカードを左へ詰める

操作が終了したとき、数が書かれたカードがそれぞれどこにあるか求めてください。なお、答えは操作の仕方に依らず一意に定まることが証明されます。

制約

  • 1 \leq H,W \leq 10^9
  • 1 \leq N \leq \min(10^5,HW)
  • 1 \leq A_i \leq H
  • 1 \leq B_i \leq W
  • (A_i,B_i) は相異なる
  • 入力に含まれる値は全て整数である

入力

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

H W N
A_1 B_1
\vdots
A_N B_N

出力

N 行出力せよ。

操作終了後に数 i が書かれたカードが上から C_i 行目、左から D_i 列目に存在するとき、i 行目には C_i,D_i をこの順に空白区切りで出力せよ。


入力例 1

4 5 2
3 2
2 5

出力例 1

2 1
1 2

何も書かれていないカードを * で表すことにします。最初、カードの配置は以下の通りです。

*****
****2
*1***
*****

操作終了後、カードの配置は以下の通りになります。

*2
1*

1 が書かれたカードは上から 2 行目、左から 1 列目にあり、2 が書かれたカードは上から 1 行目、左から 2 列目にあります。


入力例 2

1000000000 1000000000 10
1 1
10 10
100 100
1000 1000
10000 10000
100000 100000
1000000 1000000
10000000 10000000
100000000 100000000
1000000000 1000000000

出力例 2

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

Score : 300 points

Problem Statement

We have HW cards arranged in a matrix with H rows and W columns.
For each i=1, \ldots, N, the card at the A_i-th row from the top and B_i-th column from the left has a number i written on it. The other HW-N cards have nothing written on them.

We will repeat the following two kinds of operations on these cards as long as we can.

  • If there is a row without a numbered card, remove all the cards in that row and fill the gap by shifting the remaining cards upward.
  • If there is a column without a numbered card, remove all the cards in that column and fill the gap by shifting the remaining cards to the left.

Find the position of each numbered card after the end of the process above. It can be proved that these positions are uniquely determined without depending on the order in which the operations are done.

Constraints

  • 1 \leq H,W \leq 10^9
  • 1 \leq N \leq \min(10^5,HW)
  • 1 \leq A_i \leq H
  • 1 \leq B_i \leq W
  • All pairs (A_i,B_i) are distinct.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

H W N
A_1 B_1
\vdots
A_N B_N

Output

Print N lines.

If, after the end of the process, the card with the number i is at the C_i-th row from the top and D_i-th column from the left, the i-th line should contain C_i and D_i in this order with a space between them.


Sample Input 1

4 5 2
3 2
2 5

Sample Output 1

2 1
1 2

Let * represent a card with nothing written on it. The initial arrangement of cards is:

*****
****2
*1***
*****

After the end of the process, they will be arranged as follows:

*2
1*

Here, the card with 1 is at the 2-nd row from the top and 1-st column from the left, and the card with 2 is at the 1-st row from the top and 2-nd column from the left.


Sample Input 2

1000000000 1000000000 10
1 1
10 10
100 100
1000 1000
10000 10000
100000 100000
1000000 1000000
10000000 10000000
100000000 100000000
1000000000 1000000000

Sample Output 2

1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
G - Prefix K-th Max

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

(1,2,\ldots,N) の順列 P=(P_1,P_2,\ldots,P_N)、および正整数 K が与えられます。

i=K,K+1,\ldots,N について、以下を求めてください。

  • P の先頭 i 項のうち、K 番目に大きい値

制約

  • 1 \leq K \leq N \leq 5 \times 10^5
  • (P_1,P_2,\ldots,P_N)(1,2,\ldots,N) の並び替えによって得られる
  • 入力はすべて整数

入力

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

N K
P_1 P_2 \ldots P_N

出力

i=K,K+1,\ldots,N についてこの順に、問題文中で問われている値を改行区切りで出力せよ。


入力例 1

3 2
1 2 3

出力例 1

1
2
  • P の先頭 2 項、すなわち (P_1,P_2)=(1,2) の中で K=2 番目に大きい値は 1 となります。
  • P の先頭 3 項、すなわち (P_1,P_2,P_3)=(1,2,3) の中で K=2 番目に大きい値は 2 となります。

入力例 2

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

出力例 2

2
3
3
5
6
7
7

Score : 400 points

Problem Statement

Given are a permutation P=(P_1,P_2,\ldots,P_N) of (1,2,\ldots,N) and a positive integer K.

For each i=K,K+1,\ldots,N, find the following.

  • The K-th greatest value among the first i terms of P.

Constraints

  • 1 \leq K \leq N \leq 5 \times 10^5
  • (P_1,P_2,\ldots,P_N) is a permutation of (1,2,\ldots,N).
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
P_1 P_2 \ldots P_N

Output

For each i=K, K+1, \ldots, N, in this order, print the specified value in Problem Statement, separated by newlines.


Sample Input 1

3 2
1 2 3

Sample Output 1

1
2
  • The (K=) 2-nd greatest value among the first 2 terms of P, (P_1,P_2)=(1,2), is 1.
  • The (K=) 2-nd greatest value among the first 3 terms of P, (P_1,P_2,P_3)=(1,2,3), is 2.

Sample Input 2

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

Sample Output 2

2
3
3
5
6
7
7
H - Shift String

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 450

問題文

0, 1 からなる長さの等しい文字列 A,B が与えられます。

A に対して以下の操作を 0 回以上何度でも行うことができます。

  • A の先頭の文字を末尾に移動させる。

A=B とするために必要な最小の操作回数を求めてください。
但し、どのように操作しても A=B とできない場合、代わりに -1 と出力してください。

T 個のテストケースが与えられるので、それぞれについて答えを求めてください。

制約

  • 1 \le T \le 10000
  • A,B0, 1 からなる文字列
  • 2 \le |A|=|B| \le 10^6
  • ひとつの入力について、 |A| の総和は 10^6 を超えない

入力

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

T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T

各テストケースは以下の形式で与えられる。

A
B

出力

T 行出力せよ。

i 行目には i 番目のテストケースについて、答えを出力せよ。


入力例 1

5
1010001
1000110
000
111
01010
01010
0101
0011
100001101110000001010110110001
101100011000011011100000010101

出力例 1

2
-1
0
-1
22

この入力には 5 個のテストケースが含まれます。

  • 1 番目のテストケースについて、 A= 1010001B= 1000110 です。
    • A に操作を 2 回行うと A1010001 \rightarrow 0100011 \rightarrow 1000110 となり、 A=B とできます。
  • 2 番目のテストケースについて、どのように操作を行っても 000111 にすることはできません。
  • 3 番目のテストケースについて、はじめから A=B です。

Score : 450 points

Problem Statement

You are given strings A and B of equal length consisting of 0 and 1.

You can perform the following operation on A zero or more times.

  • Move the first character of A to the end.

Find the minimum number of operations required to make A=B.
If it is impossible to make A=B no matter how you operate, print -1 instead.

You are given T test cases; find the answer for each of them.

Constraints

  • 1 \le T \le 10000
  • A and B are strings consisting of 0 and 1.
  • 2 \le |A|=|B| \le 10^6
  • For a single input, the sum of |A| does not exceed 10^6.

Input

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

T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T

Each test case is given in the following format:

A
B

Output

Print T lines.

The i-th line should contain the answer for the i-th test case.


Sample Input 1

5
1010001
1000110
000
111
01010
01010
0101
0011
100001101110000001010110110001
101100011000011011100000010101

Sample Output 1

2
-1
0
-1
22

This input contains five test cases.

  • For the first test case, A= 1010001 and B= 1000110.
    • By performing the operation on A twice, A becomes 1010001 \rightarrow 0100011 \rightarrow 1000110, which makes A=B.
  • For the second test case, no matter how you perform the operation, you cannot change 000 to 111.
  • For the third test case, A=B from the beginning.
I - Almost Sorted 2

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : {500}

問題文

長さ N の整数列 A=(A_1,A_2,\ldots,A_N) および正整数 D が与えられます。

A を並び替えることで得られる整数列 B=(B_1, B_2, \ldots, B_N) であって、次の条件を満たすものの個数を 998244353 で割ったあまりを求めてください。

  • すべての i\ (1\leq i\leq N-1) に対して B_{i+1}\geq B_i-D が成り立つ。

制約

  • 2\leq N\leq 2\times 10^5
  • 1\leq D\leq 10^6
  • 1\leq A_i\leq 10^6
  • 入力は全て整数

入力

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

N D
A_1 A_2 \ldots A_N

出力

答えを出力せよ。


入力例 1

4 1
5 2 1 2

出力例 1

3

条件を満たす整数列は (1,2,2,5),(2,1,2,5),(2,2,1,5)3 つです。


入力例 2

5 10
20 40 60 80 100

出力例 2

1

入力例 3

15 12345
18270 31252 27543 31406 22271 13402 12279 25697 18349 27615 39360 22790 32581 23990 36154

出力例 3

858152905

Score : 500 points

Problem Statement

You are given an integer sequence A=(A_1,A_2,\ldots,A_N) of length N and a positive integer D.

Find the number, modulo 998244353, of integer sequences B=(B_1, B_2, \ldots, B_N) that can be obtained by rearranging A and satisfy the following condition:

  • B_{i+1}\geq B_i-D holds for all i\ (1\leq i\leq N-1).

Constraints

  • 2\leq N\leq 2\times 10^5
  • 1\leq D\leq 10^6
  • 1\leq A_i\leq 10^6
  • All input values are integers.

Input

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

N D
A_1 A_2 \ldots A_N

Output

Print the answer.


Sample Input 1

4 1
5 2 1 2

Sample Output 1

3

The integer sequences satisfying the condition are (1,2,2,5),(2,1,2,5),(2,2,1,5), which are three sequences.


Sample Input 2

5 10
20 40 60 80 100

Sample Output 2

1

Sample Input 3

15 12345
18270 31252 27543 31406 22271 13402 12279 25697 18349 27615 39360 22790 32581 23990 36154

Sample Output 3

858152905