A - Max Add

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

数列 a = (a_1, a_2, a_3, \dots, a_k) に対して、f(a) を、以下の操作を行った後の a の要素の総和と定義します。

  • i = 1, 2, 3, \dots, k の順に以下の操作を行う :
    現在の a の要素の最大値を a_i に足す

長さ N の数列 A = (A_1, A_2, A_3, \dots, A_N) が与えられます。
1 以上 N 以下の各整数 k について、a = (A_1, A_2, A_3, \dots, A_k) としたときの f(a) を求めてください。

制約

  • 1 \le N \le 2 \times 10^5
  • 1 \le A_i \le 10^7
  • 入力に含まれる値は全て整数

入力

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

N
A_1 A_2 A_3 \dots A_N

出力

N 行にわたって出力せよ。k 行目には a = (A_1, A_2, A_3, \dots, A_k) としたときの f(a) を出力せよ。


入力例 1

3
1 2 3

出力例 1

2
8
19

例えば a = (A_1, A_2, A_3) のときの f(a) は以下のように計算されます。

  • まず i = 1 として、現在の a の最大値である 3a_1 に足す。a = (4, 2, 3) となる。
  • 次に i = 2 として、現在の a の最大値である 4a_2 に足す。a = (4, 6, 3) となる。
  • 最後に i = 3 として、現在の a の最大値である 6a_3 に足す。a = (4, 6, 9) となる。
  • 操作後の a の総和である 19f(a) の値である。

Score : 400 points

Problem Statement

For a sequence a = (a_1, a_2, a_3, \dots, a_k), let f(a) be the sum of its elements after the following is done:

  • For each i = 1, 2, 3, \dots, k, in this order, do the following operation:
    add the current maximum value of an element in a to a_i.

You are given a sequence of length N: A = (A_1, A_2, A_3, \dots, A_N).
For each integer k between 1 and N (inclusive), find f(a) when a = (A_1, A_2, A_3, \dots, A_k).

Constraints

  • 1 \le N \le 2 \times 10^5
  • 1 \le A_i \le 10^7
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 A_3 \dots A_N

Output

Print N lines. The k-th line should contain f(a) when a = (A_1, A_2, A_3, \dots, A_k).


Sample Input 1

3
1 2 3

Sample Output 1

2
8
19

For example, when a = (A_1, A_2, A_3), f(a) is computed as follows:

  • First, for i = 1, add to a_1 the current maximum value of a, which is 3, making a = (4, 2, 3).
  • Next, for i = 2, add to a_2 the current maximum value of a, which is 4, making a = (4, 6, 3).
  • Finally, for i = 3, add to a_3 the current maximum value of a, which is 6, making a = (4, 6, 9).
  • f(a) is the sum of the elements of a now, which is 19.
B - Uniformly Distributed

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

HW 列のマス目があります。上から i 行目、左から j 列目のマスをマス (i, j) と呼ぶことにします。
マス目の状態を表す H 個の文字列 S_1, S_2, S_3, \dots, S_H が与えられます。マス (i, j) の状態は以下の通りです。

  • S_ij 文字目が R ならば赤く塗られている
  • S_ij 文字目が B ならば青く塗られている
  • S_ij 文字目が . ならば色が塗られていない

色が塗られていないマスの個数を k とすると、そのようなマスそれぞれを赤と青のどちらかで塗る方法は 2^k 通りありますが、このうち以下の条件を満たすものの個数を求めてください。

  • マス (1, 1) からマス (H, W) に、右または下へ 1 マス移動することを繰り返して辿り着くとき、途中で通過するマス (マス (1, 1), (H, W) を含む) のうち赤に塗られたものの数が、通る経路によらず一定である

ただし、答えは非常に大きくなることがあるので、998244353 で割った余りを出力してください。

制約

  • 2 \le H \le 500
  • 2 \le W \le 500
  • S_iR, B, . からなる長さ W の文字列

入力

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

H W
S_1
S_2
S_3
\hspace{3pt} \vdots
S_H

出力

答えを 998244353 で割った余りを出力せよ。


入力例 1

2 2
B.
.R

出力例 1

2

マス (1, 1) からマス (2, 2) に、右または下へ 1 マス移動することを繰り返して辿り着く方法は以下の 2 通りあります。

  • マス (1, 1) \rightarrow (1, 2) \rightarrow (2, 2)
  • マス (1, 1) \rightarrow (2, 1) \rightarrow (2, 2)

マス (1, 2) とマス (2, 1) を異なる色で塗った場合、上記の 2 通りの移動方法のどちらを選ぶかによって、通過する赤く塗られたマスの数が変わってしまうため条件を満たしません。
一方で、マス (1, 2) とマス (2, 1) を同じ色で塗った場合、通過する赤く塗られたマスの数は移動方法によって変わらないため、条件を満たします。
よって条件を満たす塗り方は 2 通りあります。


入力例 2

3 3
R.R
BBR
...

出力例 2

0

条件を満たす塗り方が存在しないかもしれません。


入力例 3

2 2
BB
BB

出力例 3

1

色が塗られていないマスが存在せず、現在のマス目は条件を満たすため、答えは 1 となります。

Score : 400 points

Problem Statement

We have a grid with H rows and W columns. Let (i, j) denote the square at the i-th row and j-th column.
You are given H strings S_1, S_2, S_3, \dots, S_H that describe the square, as follows:

  • if the j-th character of S_i is R, (i, j) is painted red;
  • if the j-th character of S_i is B, (i, j) is painted blue;
  • if the j-th character of S_i is ., (i, j) is unpainted.

Let k be the number of unpainted squares. Among the 2^k ways to paint each of those squares red or blue, how many satisfy the following condition?

  • The number of red squares visited while getting from (1, 1) to (H, W) by repeatedly moving one square right or down, including (1, 1) and (H, W), is always the same regardless of the path taken.

Since the count may be enormous, print it modulo 998244353.

Constraints

  • 2 \le H \le 500
  • 2 \le W \le 500
  • S_i is a string of length W consisting of R, B, and ..

Input

Input is given from Standard Input in the following format:

H W
S_1
S_2
S_3
\hspace{3pt} \vdots
S_H

Output

Print the count modulo 998244353.


Sample Input 1

2 2
B.
.R

Sample Output 1

2

There are two ways to get from (1, 1) to (2, 2) by repeatedly moving right or down, as follows:

  • (1, 1) \rightarrow (1, 2) \rightarrow (2, 2)
  • (1, 1) \rightarrow (2, 1) \rightarrow (2, 2)

If we paint (1, 2) and (2, 1) in different colors, the above two paths will contain different numbers of red squares, violating the condition.
On the other hand, if we paint (1, 2) and (2, 1) in the same color, the two paths will contain the same numbers of red squares, satisfying the condition.
Thus, there are two ways to satisfy the condition.


Sample Input 2

3 3
R.R
BBR
...

Sample Output 2

0

There may be no way to satisfy the condition.


Sample Input 3

2 2
BB
BB

Sample Output 3

1

There is no unpainted square, and the current grid satisfies the condition, so the answer is 1.

C - Swaps 2

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

長さ N の数列 A = (A_1, A_2, A_3, \dots, A_N), B = (B_1, B_2, B_3, \dots, B_N) が与えられます。
以下の操作を繰り返す (1 回も行わなくてもよい) ことで AB に一致させることが可能かを判定してください。また、可能なら、AB に一致させるのに必要な最小の操作回数を求めてください。

  • 1 \le i \lt N を満たす整数 i を選び、以下のことを順に行う
    • A_iA_{i + 1} を入れ替える
    • A_i1 を足す
    • A_{i + 1} から 1 を引く

制約

  • 2 \le N \le 2 \times 10^5
  • 0 \le A_i \le 10^9
  • 0 \le B_i \le 10^9
  • 入力に含まれる値は全て整数

入力

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

N
A_1 A_2 A_3 \dots A_N
B_1 B_2 B_3 \dots B_N

出力

AB に一致させることが不可能なら -1 を出力せよ。
可能なら、そのために必要な最小の操作回数を出力せよ。


入力例 1

3
3 1 4
6 2 0

出力例 1

2

以下のように操作すると、2 回の操作で AB に一致させることができます。

  • まず、i = 2 として操作する。A = (3, 5, 0) となる。
  • 次に、i = 1 として操作する。A = (6, 2, 0) となる。

1 回以下の操作で目的を達成することはできません。


入力例 2

3
1 1 1
1 1 2

出力例 2

-1

この場合、AB に一致させることは不可能です。


入力例 3

5
5 4 1 3 2
5 4 1 3 2

出力例 3

0

1 回も操作をしなくても AB に一致している可能性があります。


入力例 4

6
8 5 4 7 4 5
10 5 6 7 4 1

出力例 4

7

Score : 500 points

Problem Statement

Given are two sequences of length N each: A = (A_1, A_2, A_3, \dots, A_N) and B = (B_1, B_2, B_3, \dots, B_N).
Determine whether it is possible to make A equal B by repeatedly doing the operation below (possibly zero times). If it is possible, find the minimum number of operations required to do so.

  • Choose an integer i such that 1 \le i \lt N, and do the following in order:
    • swap A_i and A_{i + 1};
    • add 1 to A_i;
    • subtract 1 from A_{i + 1}.

Constraints

  • 2 \le N \le 2 \times 10^5
  • 0 \le A_i \le 10^9
  • 0 \le B_i \le 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 A_3 \dots A_N
B_1 B_2 B_3 \dots B_N

Output

If it is impossible to make A equal B, print -1.
Otherwise, print the minimum number of operations required to do so.


Sample Input 1

3
3 1 4
6 2 0

Sample Output 1

2

We can match A with B in two operations, as follows:

  • First, do the operation with i = 2, making A = (3, 5, 0).
  • Next, do the operation with i = 1, making A = (6, 2, 0).

We cannot meet our objective in one or fewer operations.


Sample Input 2

3
1 1 1
1 1 2

Sample Output 2

-1

In this case, it is impossible to match A with B.


Sample Input 3

5
5 4 1 3 2
5 4 1 3 2

Sample Output 3

0

A may equal B before doing any operation.


Sample Input 4

6
8 5 4 7 4 5
10 5 6 7 4 1

Sample Output 4

7
D - Bracket Score 2

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

括弧の対応が取れている文字列を、次のうちいずれかの条件を満たす文字列と定義します。

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

また、括弧の対応が取れている文字列 si 文字目と j 文字目が対応しているとは、以下の条件を全て満たすこととします。

  • 1 \le i < j \le |s|
  • s_i = (
  • s_j = )
  • si 文字目と j 文字目の間にある文字列 (i 文字目と j 文字目は含まない) は括弧の対応が取れている文字列である

長さ 2N の数列 A = (A_1, A_2, A_3, \dots, A_{2N}) が与えられます。
括弧の対応が取れている長さ 2N の文字列 sスコアを、si 文字目と j 文字目が対応しているような全ての組 (i, j) について |A_i - A_j| を足し合わせたものと定義します。

括弧の対応が取れている長さ 2N の文字列のうち、スコアが最大となるような文字列を 1 つ求めてください。

制約

  • 1 \le N \le 2 \times 10^5
  • 1 \le A_i \le 10^9
  • 入力に含まれる値は全て整数

入力

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

N
A_1 A_2 A_3 \dots A_{2N}

出力

長さ 2N の括弧の対応が取れている文字列のうち、スコアが最大となるような文字列を 1 つ出力せよ。
正しい答えが複数存在する場合、どれを出力しても正解と判定される。


入力例 1

2
1 2 3 4

出力例 1

(())

長さ 4 の括弧の対応が取れている文字列は (())()()2 種類あり、それぞれのスコアは以下の通りです。

  • (()) : |1 - 4| + |2 - 3| = 4
  • ()() : |1 - 2| + |3 - 4| = 2

よって、(()) のみが正しい答えとなります。


入力例 2

2
2 3 2 3

出力例 2

()()

(())()() のスコアは以下の通りです。

  • (()) : |2 - 3| + |3 - 2| = 2
  • ()() : |2 - 3| + |2 - 3| = 2

よって、この場合どちらを出力しても正解となります。

Score : 600 points

Problem Statement

Let us define balanced parenthesis string as a string satisfying one of the following conditions:

  • An empty string.
  • A concatenation of s and t in this order, for some non-empty balanced parenthesis strings s and t.
  • A concatenation of (, s, ) in this order, for some balanced parenthesis string s.

Also, let us say that the i-th and j-th characters of a parenthesis string s is corresponding to each other when all of the following conditions are satisfied:

  • 1 \le i < j \le |s|.
  • s_i = (.
  • s_j = ).
  • The string between the i-th and j-th characters of s, excluding the i-th and j-th characters, is a balanced parenthesis string.

You are given a sequence of length 2N: A = (A_1, A_2, A_3, \dots, A_{2N}).
Let us define the score of a balanced parenthesis string s of length 2N as the sum of |A_i - A_j| over all pairs (i, j) such that the i-th and j-th characters of s are corresponding to each other.

Among the balanced parenthesis strings of length 2N, find one with the maximum score.

Constraints

  • 1 \le N \le 2 \times 10^5
  • 1 \le A_i \le 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 A_3 \dots A_{2N}

Output

Print a balanced parenthesis string of length 2N with the maximum score.
If there are multiple such strings, any of them will be accepted.


Sample Input 1

2
1 2 3 4

Sample Output 1

(())

There are two balanced parenthesis strings of length 4: (()) and ()(), with the following scores:

  • (()): |1 - 4| + |2 - 3| = 4
  • ()(): |1 - 2| + |3 - 4| = 2

Thus, (()) is the only valid answer.


Sample Input 2

2
2 3 2 3

Sample Output 2

()()

(()) and ()() have the following scores:

  • (()) : |2 - 3| + |3 - 2| = 2
  • ()() : |2 - 3| + |2 - 3| = 2

Thus, either is a valid answer in this case.

E - 1D Party

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 700

問題文

1 から人 N までの N 人の人が数直線上に並んでいます。人 i は今数直線上の座標 A_i にいます。
ここで、A_1 < A_2 < A_3 < \dots < A_N であり、A_i は全て偶数です。

今から k 秒間のパーティーを開きます。
パーティー中、各人は毎秒 1 以下の速さで数直線上を自由に動くことができます。(速さが毎秒 1 以下であれば負の向きに動くこともできます。)
参加者の希望により、1 \le i \lt N を満たす全ての整数 i について以下の条件を満たす必要があります。

  • i と人 i + 1 が同じ座標にいる瞬間が、パーティー中 (終了の瞬間も含む) に少なくとも 1 回存在する

各人が最適に行動すると条件を全て満たすことができるような最小の k を求めてください。
この問題の制約下で答えが整数になることが証明できます。

制約

  • 2 \le N \le 2 \times 10^5
  • 0 \le A_i \le 10^9
  • A_1 < A_2 < A_3 < \dots < A_N
  • A_i は偶数

入力

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

N
A_1 A_2 A_3 \dots A_N

出力

答えを整数として出力せよ。


入力例 1

3
0 6 10

出力例 1

5

5 秒間のパーティーの間、各人は以下のように移動するとよいです。

  • 1 : 常に正の向きに速さ 1 で移動する
  • 2 : 最初の 2 秒間は正の向きに速さ 1 で移動し、残りの 3 秒間は負の向きに速さ 1 で移動する
  • 3 : 常に負の向きに速さ 1 で移動する

各人がこのように移動した場合、開始からちょうど 2 秒後に人 2 と人 3 が同じ座標にいます。また、パーティーの終了時に人 1 と人 2 が同じ座標にいます。
よって、k = 5 の場合、条件を全て満たすことができます。これより小さい k では条件を全て満たすような移動の仕方は存在しません。


入力例 2

5
0 2 4 6 8

出力例 2

3

3 秒間のパーティーの間、各人は例えば以下のように移動するとよいです。

  • 1 : 常に正の向きに速さ 1 で移動する
  • 2 : 最初の 2 秒間は正の向きに速さ 1 で移動し、残りの 1 秒間は負の向きに速さ 1 で移動する
  • 3 : 常に移動しない
  • 4 : 最初の 2 秒間は負の向きに速さ 1 で移動し、残りの 1 秒間は正の向きに速さ 1 で移動する
  • 5 : 常に負の向きに速さ 1 で移動する

入力例 3

10
0 2 4 6 8 92 94 96 98 100

出力例 3

44

Score : 700 points

Problem Statement

There are N people, Person 1 through Person N, standing on a number line. Person i is now at coordinate A_i on the number line.
Here, A_1 < A_2 < A_3 < \dots < A_N and all A_i are even numbers.

We will now hold a party for k seconds.
During the party, each person can freely move on the number line at a speed of at most 1 per second. (It is also allowed to move in the negative direction within the speed limit.)
The people request that the following condition be satisfied for every integer i such that 1 \le i \lt N:

  • there is at least one moment during the party (including the moment it ends) when Person i and Person i + 1 are at the same coordinate.

Find the minimum k for which there is a strategy that the people can take to satisfy all the conditions.
We can prove that the answer is an integer under the Constraints of this problem.

Constraints

  • 2 \le N \le 2 \times 10^5
  • 0 \le A_i \le 10^9
  • A_1 < A_2 < A_3 < \dots < A_N
  • A_i is an even number.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 A_3 \dots A_N

Output

Print the answer as an integer.


Sample Input 1

3
0 6 10

Sample Output 1

5

During the party lasting 5 seconds, each person should move as follows:

  • Person 1: always moves in the positive direction at a speed of 1.
  • Person 2: moves in the positive direction at a speed of 1 during the first 2 seconds, then in the negative direction at a speed of 1 during the remaining 3 seconds.
  • Person 3: always moves in the negative direction at a speed of 1.

If they move in this way, Person 2 and Person 3 will be at the same coordinate exactly 2 seconds after the beginning, and Person 1 and Person 2 will be at the same coordinate at the end of the party.
Thus, they can satisfy all the conditions for k = 5. For smaller k, there is no strategy to satisfy all of them.


Sample Input 2

5
0 2 4 6 8

Sample Output 2

3

During the party lasting 3 seconds, each person should, for example, move as follows:

  • Person 1: always moves in the positive direction at a speed of 1.
  • Person 2: moves in the positive direction at a speed of 1 during the first 2 seconds, then in the negative direction at a speed of 1 during the remaining 1 second.
  • Person 3: does not move at all.
  • Person 4: moves in the negative direction at a speed of 1 during the first 2 seconds, then in the positive direction at a speed of 1 during the remaining 1 second.
  • Person 5: always moves in the negative direction at a speed of 1.

Sample Input 3

10
0 2 4 6 8 92 94 96 98 100

Sample Output 3

44
F - Wine Thief

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 800

問題文

問題 F と問題 F2 は同じ問題ですが、制約と実行時間制限が異なります。

高橋君の倉庫には N 本のワインがあり、左右方向 1 列に並んでいます。左から i 番目のワインの美味しさは A_i です。
青木君は今からこの N 本のうち、ちょうど K 本を選んで盗みます。しかし、高橋君は注意深いので、以下の条件が満たされると盗まれたことに気付いてしまいます。

  • 連続で並ぶ D 本のワインであって、そのうち 2 本以上盗まれているようなものが存在する (この問題では D = 2 です)

高橋君に気付かれないような全ての盗み方について、盗んだワインの美味しさの和を求め、それを足し合わせた値を求めてください。
なお、答えは非常に大きくなることがあるので、998244353 で割った余りを出力してください。

制約

  • D = 2
  • 2 \le N \le 3 \times 10^5
  • 1 \le K \le \left\lceil \frac{N}{D} \right\rceil\left\lceil x \right\rceilx 以上の最小の整数を表す)
  • 1 \le A_i \lt 998244353
  • 入力に含まれる値は全て整数

入力

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

N K D
A_1 A_2 A_3 \dots A_N

出力

答えを 998244353 で割った余りを出力せよ。


入力例 1

4 2 2
1 4 2 3

出力例 1

14

盗み方と盗んだワインの美味しさの和は以下の通りです。

  • 左から 1 本目のワインと 3 本目のワインを盗んだ場合 : 美味しさの和は 1 + 2 = 3
  • 左から 1 本目のワインと 4 本目のワインを盗んだ場合 : 美味しさの和は 1 + 3 = 4
  • 左から 2 本目のワインと 4 本目のワインを盗んだ場合 : 美味しさの和は 4 + 3 = 7

よって答えは 3 + 4 + 7 = 14 となります。


入力例 2

5 3 2
4 7 5 3 8

出力例 2

17

左から 1, 3, 5 本目のワインを盗むほかありません。


入力例 3

12 4 2
107367523 266126484 149762920 57456082 857431610 400422663 768881284 494753774 152155823 740238343 871191740 450057094

出力例 3

136993014

Score : 800 points

Problem Statement

Problems F and F2 have the same Problem Statement but different Constraints and Time Limits.

There are N bottles of wine in Takahashi's cellar, arranged in a row from left to right.
The tastiness of the i-th bottle from the left is A_i.
Aoki will choose K bottles from these N and steal them. However, Takahashi is careful enough to notice the theft if the following condition is satisfied:

  • There exist D consecutive bottles such that two or more of them are stolen. (D = 2 in this problem.)

For every way of stealing bottles without getting noticed by Takahashi, find the total tastiness of the bottles stolen, and then find the sum of all those values.
Since the sum can be enormous, print it modulo 998244353.

Constraints

  • D = 2
  • 2 \le N \le 3 \times 10^5
  • 1 \le K \le \left\lceil \frac{N}{D} \right\rceil (\left\lceil x \right\rceil represents the smallest integer not less than x.)
  • 1 \le A_i \lt 998244353
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K D
A_1 A_2 A_3 \dots A_N

Output

Print the sum modulo 998244353.


Sample Input 1

4 2 2
1 4 2 3

Sample Output 1

14

The possible ways to steal bottles and the total tastiness for each of them are as follows:

  • Steal the 1-st and 3-rd bottles from the left, for the total tastiness of 1 + 2 = 3.
  • Steal the 1-st and 4-th bottles from the left, for the total tastiness of 1 + 3 = 4.
  • Steal the 2-nd and 4-th bottles from the left, for the total tastiness of 4 + 3 = 7.

Thus, the answer is 3 + 4 + 7 = 14.


Sample Input 2

5 3 2
4 7 5 3 8

Sample Output 2

17

There is no choice but to steal the 1-st, 3-rd, and 5-th bottles.


Sample Input 3

12 4 2
107367523 266126484 149762920 57456082 857431610 400422663 768881284 494753774 152155823 740238343 871191740 450057094

Sample Output 3

136993014
F2 - Wine Thief

Time Limit: 10 sec / Memory Limit: 1024 MB

配点 : 800

問題文

問題 F と問題 F2 は同じ問題ですが、制約と実行時間制限が異なります。

高橋君の倉庫には N 本のワインがあり、左右方向 1 列に並んでいます。左から i 番目のワインの美味しさは A_i です。
青木君は今からこの N 本のうち、ちょうど K 本を選んで盗みます。しかし、高橋君は注意深いので、以下の条件が満たされると盗まれたことに気付いてしまいます。

  • 連続で並ぶ D 本のワインであって、そのうち 2 本以上盗まれているようなものが存在する

高橋君に気付かれないような全ての盗み方について、盗んだワインの美味しさの和を求め、それを足し合わせた値を求めてください。
なお、答えは非常に大きくなることがあるので、998244353 で割った余りを出力してください。

制約

  • 2 \le D \le N \le 10^6
  • 1 \le K \le \left\lceil \frac{N}{D} \right\rceil\left\lceil x \right\rceilx 以上の最小の整数を表す)
  • 1 \le A_i \lt 998244353
  • 入力に含まれる値は全て整数

入力

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

N K D
A_1 A_2 A_3 \dots A_N

出力

答えを 998244353 で割った余りを出力せよ。


入力例 1

4 2 2
1 4 2 3

出力例 1

14

盗み方と盗んだワインの美味しさの和は以下の通りです。

  • 左から 1 本目のワインと 3 本目のワインを盗んだ場合 : 美味しさの和は 1 + 2 = 3
  • 左から 1 本目のワインと 4 本目のワインを盗んだ場合 : 美味しさの和は 1 + 3 = 4
  • 左から 2 本目のワインと 4 本目のワインを盗んだ場合 : 美味しさの和は 4 + 3 = 7

よって答えは 3 + 4 + 7 = 14 となります。


入力例 2

5 2 3
1 5 7 7 3

出力例 2

20

盗み方と盗んだワインの美味しさの和は以下の通りです。

  • 左から 1 本目のワインと 4 本目のワインを盗んだ場合 : 美味しさの和は 1 + 7 = 8
  • 左から 1 本目のワインと 5 本目のワインを盗んだ場合 : 美味しさの和は 1 + 3 = 4
  • 左から 2 本目のワインと 5 本目のワインを盗んだ場合 : 美味しさの和は 5 + 3 = 8

入力例 3

18 4 4
107367523 266126484 149762920 57456082 857431610 400422663 768881284 494753774 152155823 740238343 871191740 450057094 208762450 787961742 90197530 77329823 193815114 707323467

出力例 3

228955567

Score : 800 points

Problem Statement

Problems F and F2 have the same Problem Statement but different Constraints and Time Limits.

There are N bottles of wine in Takahashi's cellar, arranged in a row from left to right.
The tastiness of the i-th bottle from the left is A_i.
Aoki will choose K bottles from these N and steal them. However, Takahashi is careful enough to notice the theft if the following condition is satisfied:

  • There exist D consecutive bottles such that two or more of them are stolen.

For every way of stealing bottles without getting noticed by Takahashi, find the total tastiness of the bottles stolen, and then find the sum of all those values.
Since the sum can be enormous, print it modulo 998244353.

Constraints

  • 2 \le D \le N \le 10^6
  • 1 \le K \le \left\lceil \frac{N}{D} \right\rceil (\left\lceil x \right\rceil represents the smallest integer not less than x.)
  • 1 \le A_i \lt 998244353
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K D
A_1 A_2 A_3 \dots A_N

Output

Print the sum modulo 998244353.


Sample Input 1

4 2 2
1 4 2 3

Sample Output 1

14

The possible ways to steal bottles and the total tastiness for each of them are as follows:

  • Steal the 1-st and 3-rd bottles from the left, for the total tastiness of 1 + 2 = 3.
  • Steal the 1-st and 4-th bottles from the left, for the total tastiness of 1 + 3 = 4.
  • Steal the 2-nd and 4-th bottles from the left, for the total tastiness of 4 + 3 = 7.

Thus, the answer is 3 + 4 + 7 = 14.


Sample Input 2

5 2 3
1 5 7 7 3

Sample Output 2

20

The possible ways to steal bottles and the total tastiness for each of them are as follows:

  • Steal the 1-st and 4-th bottles from the left, for the total tastiness of 1 + 7 = 8.
  • Steal the 1-st and 5-th bottles from the left, for the total tastiness of 1 + 3 = 4.
  • Steal the 2-nd and 5-th bottles from the left, for the total tastiness of 5 + 3 = 8.

Sample Input 3

18 4 4
107367523 266126484 149762920 57456082 857431610 400422663 768881284 494753774 152155823 740238343 871191740 450057094 208762450 787961742 90197530 77329823 193815114 707323467

Sample Output 3

228955567