Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 266 点
問題文
高橋君と青木君が生徒会長選挙に立候補しています。
N 人の生徒(生徒 1, 生徒 2, \ldots, 生徒 N)がこの選挙の投票者であり、各生徒は初期状態で高橋君と青木君のどちらか一方を支持しています。各生徒の初期の支持状態は文字列 S で与えられ、S の i 文字目が T なら生徒 i は高橋君を、A なら青木君を支持しています。初期状態では、両候補ともに少なくとも 1 人の支持者がいます。
選挙に先立ち、M 回の演説が 1 回目から順に行われます。i 回目(1 \leq i \leq M)の演説では、生徒 R_i が説得を受け、その支持が反対側の候補者に変わります。すなわち、その時点で高橋君を支持していた場合は青木君の支持に、青木君を支持していた場合は高橋君の支持に変わります。なお、同じ生徒が複数回の演説で指定されることもあります。
各演説の直後に、いずれかの候補者の支持者数が 0 人になっているかを確認します。0 人になった候補者がいる場合、その候補者は立候補を取り下げ、もう一方の候補者の当選が確定します。当選が確定した時点で、それ以降の演説は行われません。なお、確認は各演説の直後にのみ行われ、初期状態での確認は行いません。
M 回すべての演説が終了するまでに当選が確定した場合は、確定した時点の演説の番号を出力してください。すなわち、i 回目の演説の直後に当選が確定した場合は i を出力してください。M 回すべての演説が終了しても双方に支持者が残っている場合は、-1 を出力してください。
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- S は長さ N の文字列であり、各文字は
TまたはAである - S は
TとAをそれぞれ 1 つ以上含む - 1 \leq R_i \leq N (1 \leq i \leq M)
- N, M, R_i はすべて整数である
入力
N M S R_1 R_2 \vdots R_M
- 1 行目には、生徒の人数を表す整数 N と、演説の回数を表す整数 M が、スペース区切りで与えられる。
- 2 行目には、各生徒の初期の支持状態を表す長さ N の文字列 S が与えられる。
- 第 2 + i 行目(1 \leq i \leq M)には、i 回目の演説で支持が変更される生徒の番号 R_i が与えられる。
出力
M 回すべての演説が終了するまでに当選が確定した場合は、確定した時点の演説の番号を 1 行で出力せよ。M 回すべての演説が終了しても双方に支持者が残っている場合は、-1 を 1 行で出力せよ。
入力例 1
4 4 TTAA 1 2 3 4
出力例 1
2
入力例 2
3 2 TTA 1 1
出力例 2
-1
入力例 3
6 5 TTTAAA 4 5 6 1 2
出力例 3
3
入力例 4
10 8 TTTTAAAAAA 1 2 3 4 1 2 3 4
出力例 4
4
入力例 5
2 1 TA 1
出力例 5
1
Score : 266 pts
Problem Statement
Takahashi and Aoki are candidates in a student council president election.
N students (student 1, student 2, \ldots, student N) are the voters in this election, and each student initially supports exactly one of the two candidates. The initial support of each student is given by a string S: if the i-th character of S is T, then student i supports Takahashi; if it is A, then student i supports Aoki. In the initial state, both candidates have at least one supporter.
Prior to the election, M speeches are given in order from the 1st to the M-th. In the i-th speech (1 \leq i \leq M), student R_i is persuaded and their support switches to the opposite candidate. That is, if they currently support Takahashi, they switch to supporting Aoki, and if they currently support Aoki, they switch to supporting Takahashi. Note that the same student may be specified in multiple speeches.
Immediately after each speech, it is checked whether either candidate has 0 supporters. If a candidate has 0 supporters, that candidate withdraws from the race, and the other candidate's victory is confirmed. Once a victory is confirmed, no further speeches are given. Note that this check is performed only immediately after each speech, and no check is performed on the initial state.
If a victory is confirmed before all M speeches are completed, output the number of the speech at which it was confirmed. That is, if the victory is confirmed immediately after the i-th speech, output i. If both candidates still have supporters after all M speeches have been completed, output -1.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- S is a string of length N, where each character is
TorA - S contains at least one
Tand at least oneA - 1 \leq R_i \leq N (1 \leq i \leq M)
- N, M, R_i are all integers
Input
N M S R_1 R_2 \vdots R_M
- The first line contains an integer N representing the number of students and an integer M representing the number of speeches, separated by a space.
- The second line contains a string S of length N representing the initial support of each student.
- The (2 + i)-th line (1 \leq i \leq M) contains R_i, the number of the student whose support is changed in the i-th speech.
Output
If a victory is confirmed before all M speeches are completed, output the number of the speech at which it was confirmed, on a single line. If both candidates still have supporters after all M speeches have been completed, output -1 on a single line.
Sample Input 1
4 4 TTAA 1 2 3 4
Sample Output 1
2
Sample Input 2
3 2 TTA 1 1
Sample Output 2
-1
Sample Input 3
6 5 TTTAAA 4 5 6 1 2
Sample Output 3
3
Sample Input 4
10 8 TTTTAAAAAA 1 2 3 4 1 2 3 4
Sample Output 4
4
Sample Input 5
2 1 TA 1
Sample Output 5
1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
高橋君のクラスには N 人の生徒がおり、生徒には 1 から N までの出席番号がついています。
現在、生徒たちは一列に並んでおり、左から順に出席番号 p_1, p_2, \ldots, p_N の生徒が並んでいます。先生が指定した目標の並び順は、左から順に出席番号 q_1, q_2, \ldots, q_N です。
高橋君は、列の中から連続する区間を 1 つ選び、その区間内の生徒の並び順を反転させる操作をちょうど 1 回行います。具体的には、1 \leq L \leq R \leq N を満たす整数 L, R を選び、位置 L から位置 R までの生徒の順番を逆にします。L = R の場合は並びは変化しませんが、これも 1 回の操作を行ったものとみなします。
L, R の選び方を適切に決めることで、操作後の並びを目標の並び q と一致させることができるか判定してください。
制約
- 1 \leq N \leq 10^6
- (p_1, p_2, \ldots, p_N) は (1, 2, \ldots, N) の順列である
- (q_1, q_2, \ldots, q_N) は (1, 2, \ldots, N) の順列である
- 入力はすべて整数である
入力
N p_1 p_2 \cdots p_N q_1 q_2 \cdots q_N
- 1 行目には、生徒の人数を表す整数 N が与えられる。
- 2 行目には、現在の並び順を表す順列 p の要素 p_1, p_2, \ldots, p_N がスペース区切りで与えられる。
- 3 行目には、目標の並び順を表す順列 q の要素 q_1, q_2, \ldots, q_N がスペース区切りで与えられる。
出力
ちょうど 1 回の反転操作で現在の並びを目標の並びに一致させることができるなら Yes を、できないなら No を出力してください。
入力例 1
5 1 2 3 4 5 1 4 3 2 5
出力例 1
Yes
入力例 2
4 1 2 3 4 2 1 4 3
出力例 2
No
入力例 3
10 3 1 4 2 5 6 7 8 9 10 3 1 8 7 6 5 2 4 9 10
出力例 3
Yes
入力例 4
25 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 1 2 3 4 5 6 20 19 18 17 16 15 14 13 12 11 10 9 8 7 21 22 23 24 25
出力例 4
Yes
入力例 5
1 1 1
出力例 5
Yes
Score : 300 pts
Problem Statement
There are N students in Takahashi's class, and the students are assigned attendance numbers from 1 to N.
Currently, the students are standing in a line, and from left to right, the students with attendance numbers p_1, p_2, \ldots, p_N are lined up. The target order specified by the teacher is, from left to right, attendance numbers q_1, q_2, \ldots, q_N.
Takahashi will select one contiguous interval from the line and reverse the order of students within that interval exactly once. Specifically, he chooses integers L, R satisfying 1 \leq L \leq R \leq N, and reverses the order of students from position L to position R. When L = R, the arrangement does not change, but this is still considered as having performed one operation.
Determine whether it is possible to make the arrangement after the operation match the target arrangement q by appropriately choosing L and R.
Constraints
- 1 \leq N \leq 10^6
- (p_1, p_2, \ldots, p_N) is a permutation of (1, 2, \ldots, N)
- (q_1, q_2, \ldots, q_N) is a permutation of (1, 2, \ldots, N)
- All inputs are integers
Input
N p_1 p_2 \cdots p_N q_1 q_2 \cdots q_N
- The first line contains an integer N representing the number of students.
- The second line contains the elements p_1, p_2, \ldots, p_N of the permutation p representing the current order, separated by spaces.
- The third line contains the elements q_1, q_2, \ldots, q_N of the permutation q representing the target order, separated by spaces.
Output
If it is possible to make the current arrangement match the target arrangement with exactly one reversal operation, print Yes; otherwise, print No.
Sample Input 1
5 1 2 3 4 5 1 4 3 2 5
Sample Output 1
Yes
Sample Input 2
4 1 2 3 4 2 1 4 3
Sample Output 2
No
Sample Input 3
10 3 1 4 2 5 6 7 8 9 10 3 1 8 7 6 5 2 4 9 10
Sample Output 3
Yes
Sample Input 4
25 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 1 2 3 4 5 6 20 19 18 17 16 15 14 13 12 11 10 9 8 7 21 22 23 24 25
Sample Output 4
Yes
Sample Input 5
1 1 1
Sample Output 5
Yes
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 366 点
問題文
高橋君は高校の新入生で、これから参加する部活動を決めようとしています。
高橋君の学校には N 個の部活動があり、それぞれの部活動には「活動ポイント」が設定されています。 i 番目の部活動の活動ポイントは A_i です。
高橋君は複数の部活動を掛け持ちすることができますが、独自のこだわりがあります。
- 選んだ部活動の活動ポイントの合計が K の倍数になるような組み合わせを「バランスが良い」とみなします。
- 何も部活動に入らない(空の組み合わせ)は、高校生活として寂しいため、認められません。
高橋君は、バランスが良い部活動の組み合わせが何通りあるかを知りたいと思っています。
N 個の部活動から 1 つ以上を選ぶ方法のうち、選んだ部活動の活動ポイントの合計が K の倍数となるような選び方の総数を求めてください。答えは非常に大きくなる可能性があるため、 10^9 + 7 で割った余りを出力してください。
制約
- 1 \leq N \leq 10^5
- 1 \leq K \leq 100
- 1 \leq A_i \leq 10^9
- 入力はすべて整数である
入力
N K A_1 A_2 \ldots A_N
- 1 行目には、部活動の数を表す N と、倍数の基準となる値 K が、スペース区切りで与えられる。
- 2 行目には、各部活動の活動ポイントを表す A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。
出力
選んだ部活動の活動ポイントの合計が K の倍数となるような、 1 つ以上の部活動の選び方の総数を 10^9 + 7 で割った余りを 1 行で出力せよ。
入力例 1
3 3 1 2 3
出力例 1
3
入力例 2
5 4 2 4 6 8 10
出力例 2
15
入力例 3
10 7 7 14 21 1 2 3 4 5 6 49
出力例 3
159
Score : 366 pts
Problem Statement
Takahashi is a new high school student who is trying to decide which club activities to join.
There are N club activities at Takahashi's school, and each club activity has an "activity point" value assigned to it. The activity points of the i-th club activity is A_i.
Takahashi can join multiple club activities simultaneously, but he has his own particular preferences.
- He considers a combination "well-balanced" if the total activity points of the chosen club activities is a multiple of K.
- Joining no club activities (the empty combination) is not allowed, as it would make for a lonely high school life.
Takahashi wants to know how many well-balanced combinations of club activities exist.
Among all ways to choose 1 or more club activities from the N available, find the total number of ways such that the sum of activity points of the chosen club activities is a multiple of K. Since the answer can be very large, output it modulo 10^9 + 7.
Constraints
- 1 \leq N \leq 10^5
- 1 \leq K \leq 100
- 1 \leq A_i \leq 10^9
- All input values are integers.
Input
N K A_1 A_2 \ldots A_N
- The first line contains N, the number of club activities, and K, the value for the multiple criterion, separated by a space.
- The second line contains A_1, A_2, \ldots, A_N, the activity points of each club activity, separated by spaces.
Output
Output in a single line the number of ways to choose 1 or more club activities such that the total activity points is a multiple of K, modulo 10^9 + 7.
Sample Input 1
3 3 1 2 3
Sample Output 1
3
Sample Input 2
5 4 2 4 6 8 10
Sample Output 2
15
Sample Input 3
10 7 7 14 21 1 2 3 4 5 6 49
Sample Output 3
159
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
高橋君は、縦 H 行、横 W 列のマス目からなる照明パネルを持っています。上から r 行目、左から c 列目のマスをマス (r, c) と呼びます。
はじめ、すべてのマスの照明は消灯しています。高橋君は N 回の操作を順に行います。
i 回目の操作では、行が A_i 以上 B_i 以下、かつ列が C_i 以上 D_i 以下であるすべてのマスの状態を反転します。すなわち、点灯しているマスは消灯し、消灯しているマスは点灯します。
すべての操作が終わった後の照明パネルの状態を最終状態と呼びます。
最終状態において、点灯しているマスどうしの辺を共有する上下左右 4 方向の隣接関係による連結成分を、それぞれ島と呼びます。点灯しているマスが 1 つも存在しない場合、島の数は 0 です。
青木君は最終状態に対して M 個の質問をします。高橋君は各質問に答えなければなりません。
j 個目の質問では、行が P_j 以上 Q_j 以下、かつ列が R_j 以上 S_j 以下である長方形の領域を指定します。
各質問について、以下の 2 つの値を求めてください。
- 指定された領域内にある、点灯しているマスの個数
- 最終状態の島のうち、指定された領域内に完全に収まっているものの個数
ここで、ある島が領域内に完全に収まっているとは、その島に属するすべてのマスが指定された領域内にあることをいいます。
注意: 島は最終状態のパネル全体で定義されたものです。質問で指定された領域内だけで改めて連結成分を求め直すわけではありません。
制約
- 1 \leq H \leq 1000
- 1 \leq W \leq 1000
- 0 \leq N \leq 10^5
- 1 \leq M \leq 10^5
- N + M \leq 10^5
- M \times H \times W \leq 5 \times 10^7
- 1 \leq A_i \leq B_i \leq H (1 \leq i \leq N)
- 1 \leq C_i \leq D_i \leq W (1 \leq i \leq N)
- 1 \leq P_j \leq Q_j \leq H (1 \leq j \leq M)
- 1 \leq R_j \leq S_j \leq W (1 \leq j \leq M)
- 入力はすべて整数である
入力
入力は以下の形式で標準入力から与えられる。
H W N A_1 B_1 C_1 D_1 A_2 B_2 C_2 D_2 \vdots A_N B_N C_N D_N M P_1 Q_1 R_1 S_1 P_2 Q_2 R_2 S_2 \vdots P_M Q_M R_M S_M
出力
M 行出力せよ。
j 行目には、j 個目の質問に対する、領域内の点灯しているマスの個数と、領域内に完全に収まっている島の個数を、この順にスペース区切りで出力せよ。
入力例 1
4 5 3 1 2 1 3 2 4 2 5 3 3 1 5 4 1 4 1 5 1 2 1 3 3 4 1 5 2 3 2 4
出力例 1
11 3 4 0 5 1 1 0
入力例 2
3 3 2 1 3 1 3 1 3 1 3 3 1 3 1 3 1 1 1 1 2 3 2 3
出力例 2
0 0 0 0 0 0
入力例 3
8 10 8 1 3 1 4 2 6 3 8 5 8 1 5 4 4 6 10 1 8 10 10 7 8 7 9 3 5 2 2 6 6 4 10 8 1 8 1 10 1 4 1 5 5 8 1 6 2 7 3 8 1 8 10 10 4 6 6 10 3 3 1 10 7 8 7 9
出力例 3
51 8 13 1 16 1 21 1 6 2 6 3 6 0 6 0
入力例 4
30 40 20 1 10 1 15 5 20 10 30 12 30 5 25 3 3 1 40 1 30 20 20 8 18 18 35 22 30 30 40 15 25 1 8 2 28 38 40 10 12 12 28 6 24 6 24 1 1 1 40 30 30 1 40 1 30 1 1 1 30 40 40 14 17 14 17 19 21 19 21 4 9 26 33 23 27 12 18 11 29 32 37 15 1 30 1 40 1 10 1 15 5 20 10 30 12 30 5 25 1 30 20 20 8 18 18 35 22 30 30 40 15 25 1 8 2 28 38 40 10 12 12 28 1 5 1 5 26 30 36 40 14 17 14 17 19 21 19 21 7 24 7 34
出力例 4
591 48 84 5 170 16 210 15 18 7 70 8 26 4 63 2 46 2 27 4 14 2 6 2 0 0 4 2 231 16
入力例 5
1 1 0 1 1 1 1 1
出力例 5
0 0
Score : 400 pts
Problem Statement
Takahashi has a lighting panel consisting of a grid with H rows and W columns. The cell at the r-th row from the top and the c-th column from the left is called cell (r, c).
Initially, all cells are turned off. Takahashi performs N operations in order.
In the i-th operation, he toggles the state of all cells whose row is between A_i and B_i (inclusive) and whose column is between C_i and D_i (inclusive). That is, cells that are on are turned off, and cells that are off are turned on.
The state of the lighting panel after all operations are completed is called the final state.
In the final state, each connected component of lit cells under the 4-directional (up, down, left, right) adjacency relation (sharing an edge) is called an island. If there are no lit cells at all, the number of islands is 0.
Aoki asks M questions about the final state. Takahashi must answer each question.
In the j-th question, a rectangular region is specified where the row is between P_j and Q_j (inclusive) and the column is between R_j and S_j (inclusive).
For each question, find the following two values:
- The number of lit cells within the specified region.
- The number of islands in the final state that are completely contained within the specified region.
Here, an island is completely contained within a region if all cells belonging to that island are within the specified region.
Note: Islands are defined with respect to the entire panel in the final state. We do NOT recompute connected components within only the region specified by each question.
Constraints
- 1 \leq H \leq 1000
- 1 \leq W \leq 1000
- 0 \leq N \leq 10^5
- 1 \leq M \leq 10^5
- N + M \leq 10^5
- M \times H \times W \leq 5 \times 10^7
- 1 \leq A_i \leq B_i \leq H (1 \leq i \leq N)
- 1 \leq C_i \leq D_i \leq W (1 \leq i \leq N)
- 1 \leq P_j \leq Q_j \leq H (1 \leq j \leq M)
- 1 \leq R_j \leq S_j \leq W (1 \leq j \leq M)
- All inputs are integers.
Input
The input is given from standard input in the following format:
H W N A_1 B_1 C_1 D_1 A_2 B_2 C_2 D_2 \vdots A_N B_N C_N D_N M P_1 Q_1 R_1 S_1 P_2 Q_2 R_2 S_2 \vdots P_M Q_M R_M S_M
Output
Output M lines.
On the j-th line, print the number of lit cells within the region and the number of islands completely contained within the region for the j-th question, separated by a space, in this order.
Sample Input 1
4 5 3 1 2 1 3 2 4 2 5 3 3 1 5 4 1 4 1 5 1 2 1 3 3 4 1 5 2 3 2 4
Sample Output 1
11 3 4 0 5 1 1 0
Sample Input 2
3 3 2 1 3 1 3 1 3 1 3 3 1 3 1 3 1 1 1 1 2 3 2 3
Sample Output 2
0 0 0 0 0 0
Sample Input 3
8 10 8 1 3 1 4 2 6 3 8 5 8 1 5 4 4 6 10 1 8 10 10 7 8 7 9 3 5 2 2 6 6 4 10 8 1 8 1 10 1 4 1 5 5 8 1 6 2 7 3 8 1 8 10 10 4 6 6 10 3 3 1 10 7 8 7 9
Sample Output 3
51 8 13 1 16 1 21 1 6 2 6 3 6 0 6 0
Sample Input 4
30 40 20 1 10 1 15 5 20 10 30 12 30 5 25 3 3 1 40 1 30 20 20 8 18 18 35 22 30 30 40 15 25 1 8 2 28 38 40 10 12 12 28 6 24 6 24 1 1 1 40 30 30 1 40 1 30 1 1 1 30 40 40 14 17 14 17 19 21 19 21 4 9 26 33 23 27 12 18 11 29 32 37 15 1 30 1 40 1 10 1 15 5 20 10 30 12 30 5 25 1 30 20 20 8 18 18 35 22 30 30 40 15 25 1 8 2 28 38 40 10 12 12 28 1 5 1 5 26 30 36 40 14 17 14 17 19 21 19 21 7 24 7 34
Sample Output 4
591 48 84 5 170 16 210 15 18 7 70 8 26 4 63 2 46 2 27 4 14 2 6 2 0 0 4 2 231 16
Sample Input 5
1 1 0 1 1 1 1 1
Sample Output 5
0 0
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 466 点
問題文
高橋君は庭師で、横一列に並んだ N 本の花の手入れを任されています。i 番目の花の現在の高さは H_i です。
依頼主の要望は「どの連続する K 本の花を見ても、その中で最も高い花と最も低い花の高さの差が D 以下になるようにしたい」というものです。
高橋君は花の茎を 切る ことしかできません(伸ばすことはできません)。各花は好きな非負整数の高さに切ることができますが、元の高さより高くすることはできません。つまり、i 番目の花の最終的な高さ H'_i は 0 \leq H'_i \leq H_i を満たす整数でなければなりません。
高橋君はできるだけ花を高く残したいので、全ての花の最終的な高さの合計 H'_1 + H'_2 + \cdots + H'_N を最大化したいと考えています。
条件を満たすように各花を切った(あるいはそのまま残した)とき、最終的な花の高さの合計の最大値を求めてください。
形式的には、以下の条件をすべて満たす整数列 H'_1, H'_2, \ldots, H'_N のうち、 \sum_{i=1}^{N} H'_i の最大値を求めてください。
- すべての i (1 \leq i \leq N) について 0 \leq H'_i \leq H_i
- すべての j (1 \leq j \leq N - K + 1) について \max(H'_j, H'_{j+1}, \ldots, H'_{j+K-1}) - \min(H'_j, H'_{j+1}, \ldots, H'_{j+K-1}) \leq D
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq K \leq N
- 0 \leq D \leq 10^9
- 0 \leq H_i \leq 10^9 (1 \leq i \leq N)
- 入力はすべて整数
入力
N K D H_1 H_2 \ldots H_N
- 1 行目には、花の本数を表す N 、連続する花の本数を表す K 、許容される高さの差を表す D が、スペース区切りで与えられる。
- 2 行目には、各花の現在の高さを表す H_1, H_2, \ldots, H_N が、スペース区切りで与えられる。
出力
条件を満たすように花を切ったとき、最終的な花の高さの合計の最大値を 1 行で出力せよ。
入力例 1
5 3 2 3 1 4 1 5
出力例 1
11
入力例 2
6 2 0 2 2 5 5 1 1
出力例 2
6
入力例 3
12 4 5 10 3 8 15 6 7 20 12 11 4 9 14
出力例 3
89
入力例 4
30 7 10 25 40 18 33 47 12 29 55 21 36 44 9 31 50 27 16 38 60 22 35 48 14 30 53 26 41 19 37 45 11
出力例 4
576
入力例 5
1 1 0 1000000000
出力例 5
1000000000
Score : 466 pts
Problem Statement
Takahashi is a gardener in charge of caring for N flowers lined up in a row. The current height of the i-th flower is H_i.
The client's request is: "For any contiguous K flowers, the difference between the maximum height and the minimum height among them should be at most D."
Takahashi can only cut the stems of the flowers (he cannot make them grow). Each flower can be cut to any non-negative integer height, but it cannot be made taller than its original height. That is, the final height H'_i of the i-th flower must be an integer satisfying 0 \leq H'_i \leq H_i.
Since Takahashi wants to keep the flowers as tall as possible, he wants to maximize the sum of the final heights of all flowers, H'_1 + H'_2 + \cdots + H'_N.
Find the maximum possible sum of the final heights of the flowers when they are cut (or left as they are) to satisfy the conditions.
Formally, find the maximum value of \sum_{i=1}^{N} H'_i among all integer sequences H'_1, H'_2, \ldots, H'_N that satisfy the following conditions:
- 0 \leq H'_i \leq H_i for all i (1 \leq i \leq N)
- \max(H'_j, H'_{j+1}, \ldots, H'_{j+K-1}) - \min(H'_j, H'_{j+1}, \ldots, H'_{j+K-1}) \leq D for all j (1 \leq j \leq N - K + 1)
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq K \leq N
- 0 \leq D \leq 10^9
- 0 \leq H_i \leq 10^9 (1 \leq i \leq N)
- All input values are integers.
Input
N K D H_1 H_2 \ldots H_N
- The first line contains the number of flowers N, the number of contiguous flowers K, and the allowed height difference D, separated by spaces.
- The second line contains the current heights of the flowers H_1, H_2, \ldots, H_N, separated by spaces.
Output
Print the maximum possible sum of the final heights of the flowers when they are cut to satisfy the conditions in a single line.
Sample Input 1
5 3 2 3 1 4 1 5
Sample Output 1
11
Sample Input 2
6 2 0 2 2 5 5 1 1
Sample Output 2
6
Sample Input 3
12 4 5 10 3 8 15 6 7 20 12 11 4 9 14
Sample Output 3
89
Sample Input 4
30 7 10 25 40 18 33 47 12 29 55 21 36 44 9 31 50 27 16 38 60 22 35 48 14 30 53 26 41 19 37 45 11
Sample Output 4
576
Sample Input 5
1 1 0 1000000000
Sample Output 5
1000000000