A - 合格者への拍手

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 200

問題文

高橋君は、プログラミングコンテストの運営スタッフとして、参加者の表彰を担当しています。

今回のコンテストには N 人の参加者がいて、i 番目の参加者の最終スコアは P_i 点です。

高橋君は、最終スコアが K 点以上の参加者全員を表彰することにしました。表彰の際、高橋君は表彰対象の各参加者に対して、その参加者の最終スコアの数値と同じ回数だけ拍手を送ります。例えば、最終スコアが 80 点の参加者には 80 回の拍手を送ります。

高橋君が拍手を送る回数の合計を求めてください。なお、表彰対象の参加者がいない場合、合計は 0 となります。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq K \leq 100
  • 1 \leq P_i \leq 100 (1 \leq i \leq N)
  • 入力はすべて整数

入力

N K
P_1 P_2 \ldots P_N
  • 1 行目には、参加者の人数 N と、表彰の基準となるスコア K が、スペース区切りで与えられる。
  • 2 行目には、各参加者の最終スコア P_1, P_2, \ldots, P_N が、スペース区切りで与えられる。
  • P_ii 番目の参加者の最終スコア(点)を表す。

出力

高橋君が拍手を送る回数の合計を 1 行で出力してください。


入力例 1

5 60
45 72 88 55 60

出力例 1

220

入力例 2

10 50
30 85 42 91 50 67 25 100 48 73

出力例 2

466

入力例 3

15 70
65 78 92 45 70 83 69 100 55 71 88 40 95 62 77

出力例 3

754

Score : 200 pts

Problem Statement

Takahashi is in charge of the award ceremony as a staff member of a programming contest.

There are N participants in this contest, and the final score of the i-th participant is P_i points.

Takahashi decided to award all participants whose final score is K points or more. During the ceremony, Takahashi claps for each awarded participant a number of times equal to that participant's final score. For example, he claps 80 times for a participant whose final score is 80 points.

Find the total number of times Takahashi claps. If there are no participants to be awarded, the total is 0.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq K \leq 100
  • 1 \leq P_i \leq 100 (1 \leq i \leq N)
  • All inputs are integers

Input

N K
P_1 P_2 \ldots P_N
  • The first line contains the number of participants N and the threshold score K for the award, separated by a space.
  • The second line contains the final scores P_1, P_2, \ldots, P_N of each participant, separated by spaces.
  • P_i represents the final score (in points) of the i-th participant.

Output

Print the total number of times Takahashi claps in one line.


Sample Input 1

5 60
45 72 88 55 60

Sample Output 1

220

Sample Input 2

10 50
30 85 42 91 50 67 25 100 48 73

Sample Output 2

466

Sample Input 3

15 70
65 78 92 45 70 83 69 100 55 71 88 40 95 62 77

Sample Output 3

754
B - 果樹園の収穫区間

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 233

問題文

高橋君は果樹園の管理人として働いています。この果樹園には N 本の果物の木が一列に並んでおり、左から順に木 1, 木 2, \ldots, 木 N と番号が付けられています。木 i (1 \leq i \leq N) には果実がちょうど 1 つ実っており、その糖度は T_i です。

高橋君は、出荷基準を満たす果実だけを収穫したいと考えています。果実の糖度が L 以上 R 以下であるとき、かつそのときに限り、その果実は出荷基準を満たします。

高橋君は、番号が連続する木の区間を選び、その区間内のすべての果実を収穫しようとしています。具体的には、整数 l, r (1 \leq l \leq r \leq N) を選び、木 l, 木 l+1, \ldots, 木 r からなる区間を考えます。この区間内のすべての木の果実が出荷基準を満たすとき、この区間は収穫可能であるといいます。

収穫可能な区間に含まれる木の本数 r - l + 1 の最大値を求めてください。収穫可能な区間が 1 つも存在しない場合は 0 を出力してください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq L \leq R \leq 10^9
  • 1 \leq T_i \leq 10^9 (1 \leq i \leq N)
  • 入力はすべて整数

入力

N L R
T_1 T_2 \ldots T_N
  • 1 行目には、木の本数を表す整数 N、出荷基準の糖度の下限(以上)を表す整数 L、出荷基準の糖度の上限(以下)を表す整数 R が、スペース区切りで与えられる。
  • 2 行目には、各木の果実の糖度を表す整数 T_1, T_2, \ldots, T_N が、スペース区切りで与えられる。

出力

収穫可能な区間に含まれる木の本数の最大値を 1 行で出力してください。収穫可能な区間が存在しない場合は 0 を出力してください。


入力例 1

5 10 20
15 12 18 25 14

出力例 1

3

入力例 2

10 5 15
8 10 12 7 9 20 6 11 13 14

出力例 2

5

入力例 3

15 100 500
150 200 300 250 180 50 400 350 300 200 150 100 450 600 200

出力例 3

7

Score : 233 pts

Problem Statement

Takahashi works as a manager of an orchard. In this orchard, there are N fruit trees lined up in a row, numbered Tree 1, Tree 2, \ldots, Tree N from left to right. Tree i (1 \leq i \leq N) bears exactly one fruit with a sweetness level of T_i.

Takahashi wants to harvest only the fruits that meet the shipping standard. A fruit meets the shipping standard if and only if its sweetness level is at least L and at most R.

Takahashi plans to select a contiguous interval of consecutively numbered trees and harvest all the fruits within that interval. Specifically, he chooses integers l, r (1 \leq l \leq r \leq N) and considers the interval consisting of Tree l, Tree l+1, \ldots, Tree r. If all fruits on the trees within this interval meet the shipping standard, the interval is called harvestable.

Find the maximum number of trees r - l + 1 contained in a harvestable interval. If no harvestable interval exists, output 0.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq L \leq R \leq 10^9
  • 1 \leq T_i \leq 10^9 (1 \leq i \leq N)
  • All input values are integers.

Input

N L R
T_1 T_2 \ldots T_N
  • The first line contains the integer N representing the number of trees, the integer L representing the lower bound (inclusive) of the sweetness level for the shipping standard, and the integer R representing the upper bound (inclusive) of the sweetness level for the shipping standard, separated by spaces.
  • The second line contains the integers T_1, T_2, \ldots, T_N representing the sweetness levels of the fruits on each tree, separated by spaces.

Output

Output in one line the maximum number of trees contained in a harvestable interval. If no harvestable interval exists, output 0.


Sample Input 1

5 10 20
15 12 18 25 14

Sample Output 1

3

Sample Input 2

10 5 15
8 10 12 7 9 20 6 11 13 14

Sample Output 2

5

Sample Input 3

15 100 500
150 200 300 250 180 50 400 350 300 200 150 100 450 600 200

Sample Output 3

7
C - 期末テストのやり直し

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 300

問題文

高橋君と青木君は同じクラスに通っており、期末テストの結果で競い合っています。

期末テストは N 科目あり、各科目にはそれぞれ満点が設定されています。i 番目の科目の満点は P_i 点です。各科目の得点は 0 点以上 P_i 点以下の整数です。

テストの結果、高橋君は i 番目の科目で A_i 点を、青木君は i 番目の科目で B_i 点を獲得しました。

ここで、高橋君の全科目の合計得点から青木君の全科目の合計得点を引いた値を得点差と呼ぶことにします。高橋君は、この得点差をできるだけ大きくしたいと考えています。

先生の計らいにより、高橋君は N 科目の中から必ずちょうど 1 科目を選んで、その科目のテストをやり直すことができます。やり直した科目の高橋君の得点は、0 以上その科目の満点以下の任意の整数に変更されます。変更後の得点は元の得点と同じ値であっても構いません。なお、青木君の得点は変わりません。

高橋君が最適に科目を選び得点を設定し直したとき、やり直し後の得点差としてあり得る最大の値を求めてください。なお、この値は負になることもあります。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq P_i \leq 10^9
  • 0 \leq A_i \leq P_i
  • 0 \leq B_i \leq P_i
  • 入力はすべて整数である。

入力

N
P_1 P_2 \cdots P_N
A_1 A_2 \cdots A_N
B_1 B_2 \cdots B_N
  • 1 行目には、科目数を表す整数 N が与えられる。
  • 2 行目には、各科目の満点を表す N 個の整数 P_1, P_2, \ldots, P_N がスペース区切りで与えられる。
  • 3 行目には、高橋君の各科目の得点を表す N 個の整数 A_1, A_2, \ldots, A_N がスペース区切りで与えられる。
  • 4 行目には、青木君の各科目の得点を表す N 個の整数 B_1, B_2, \ldots, B_N がスペース区切りで与えられる。

出力

高橋君が最適に行動したときの、やり直し後の得点差の最大値を 1 行で出力せよ。


入力例 1

3
100 100 100
60 80 50
70 70 70

出力例 1

30

入力例 2

5
200 150 300 100 250
200 100 250 80 50
150 120 200 90 100

出力例 2

220

入力例 3

8
1000000000 500 1000 200 300 400 600 800
0 500 1000 200 300 400 600 800
1000000000 0 0 0 0 0 0 0

出力例 3

3800

Score : 300 pts

Problem Statement

Takahashi and Aoki are in the same class and competing based on their final exam results.

The final exam consists of N subjects, each with its own maximum score. The maximum score for the i-th subject is P_i points. The score for each subject is an integer between 0 and P_i, inclusive.

As a result of the exam, Takahashi scored A_i points and Aoki scored B_i points on the i-th subject.

Here, we define the score difference as the value obtained by subtracting Aoki's total score across all subjects from Takahashi's total score across all subjects. Takahashi wants to maximize this score difference.

Thanks to the teacher's arrangement, Takahashi can choose exactly 1 subject from the N subjects and retake that subject's exam. Takahashi's score on the retaken subject will be changed to any integer between 0 and that subject's maximum score, inclusive. The new score may be the same as the original score. Aoki's scores do not change.

When Takahashi optimally chooses the subject and sets his new score, find the maximum possible score difference after the retake. Note that this value may be negative.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq P_i \leq 10^9
  • 0 \leq A_i \leq P_i
  • 0 \leq B_i \leq P_i
  • All inputs are integers.

Input

N
P_1 P_2 \cdots P_N
A_1 A_2 \cdots A_N
B_1 B_2 \cdots B_N
  • The first line contains an integer N representing the number of subjects.
  • The second line contains N integers P_1, P_2, \ldots, P_N separated by spaces, representing the maximum score for each subject.
  • The third line contains N integers A_1, A_2, \ldots, A_N separated by spaces, representing Takahashi's score for each subject.
  • The fourth line contains N integers B_1, B_2, \ldots, B_N separated by spaces, representing Aoki's score for each subject.

Output

Print in one line the maximum possible score difference after the retake when Takahashi acts optimally.


Sample Input 1

3
100 100 100
60 80 50
70 70 70

Sample Output 1

30

Sample Input 2

5
200 150 300 100 250
200 100 250 80 50
150 120 200 90 100

Sample Output 2

220

Sample Input 3

8
1000000000 500 1000 200 300 400 600 800
0 500 1000 200 300 400 600 800
1000000000 0 0 0 0 0 0 0

Sample Output 3

3800
D - 科目の履修順序

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 400

問題文

高橋君は大学で N 個の科目をすべて履修しようとしています。

科目には 1 から N までの番号が付けられており、各科目はちょうど 1 回ずつ履修します。一部の科目には前提科目(いわゆる「履修前提」)が設定されており、その科目を履修するには、前提科目をすべて先に履修し終えている必要があります。1 つの科目に対して前提科目が複数設定されていることもあります。前提科目が 1 つも設定されていない科目は、最初から履修可能です。

前提関係は M 個与えられ、各前提関係は「科目 B_j を履修するには、科目 A_j を先に履修し終えていなければならない」という形式です。

高橋君は科目を 1 つずつ履修していきます。各ステップにおいて、まだ履修していない科目のうち、前提科目をすべて履修済みであるもの(これを 履修可能な科目 と呼びます)の中から、番号が最も小さい科目を選んで履修します。

高橋君がすべての科目を履修するとき、科目を履修する順番を出力してください。

なお、前提関係に矛盾はなく(循環は存在せず)、必ずすべての科目を履修できることが保証されています。また、上記のルールにより履修順序は一意に定まります。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 1 \leq A_j \leq N (1 \leq j \leq M)
  • 1 \leq B_j \leq N (1 \leq j \leq M)
  • A_j \neq B_j (1 \leq j \leq M)
  • i \neq j ならば (A_i, B_i) \neq (A_j, B_j)(同じ前提関係は複数回与えられない)
  • 前提関係に循環は存在しない
  • 入力はすべて整数である

入力

N M
A_1 B_1
A_2 B_2
\vdots
A_M B_M
  • 1 行目には、科目の数を表す整数 N と、前提関係の数を表す整数 M が、スペース区切りで与えられる。
  • 続く M 行のうち j 番目の行 (1 \leq j \leq M) には、科目 B_j を履修する前に科目 A_j を履修し終えている必要があることを表す整数 A_jB_j が、スペース区切りで与えられる。

出力

高橋君が科目を履修する順番を、スペース区切りで 1 行で出力せよ。すなわち、N 個の整数を、履修する順に左から並べて出力せよ。


入力例 1

4 3
1 2
3 4
2 4

出力例 1

1 2 3 4

入力例 2

5 3
2 1
3 1
5 4

出力例 2

2 3 1 5 4

入力例 3

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

出力例 3

1 2 3 4 5 6 7 8 9 10

入力例 4

15 12
15 14
14 13
13 12
12 11
1 5
2 5
5 6
3 7
8 9
9 10
4 10
6 11

出力例 4

1 2 3 4 5 6 7 8 9 10 15 14 13 12 11

入力例 5

1 0

出力例 5

1

Score : 400 pts

Problem Statement

Takahashi is trying to take all N courses at his university.

The courses are numbered from 1 to N, and each course is taken exactly once. Some courses have prerequisite courses, and in order to take such a course, all of its prerequisite courses must have been completed beforehand. A single course may have multiple prerequisites. Courses with no prerequisites can be taken from the beginning.

There are M prerequisite relationships given, each in the form: "In order to take course B_j, course A_j must have been completed first."

Takahashi takes courses one at a time. At each step, among the courses he has not yet taken whose prerequisites have all been completed (these are called available courses), he selects the one with the smallest number and takes it.

Output the order in which Takahashi takes all the courses.

It is guaranteed that there are no contradictions in the prerequisite relationships (i.e., no cycles exist), and that it is always possible to take all courses. Furthermore, the above rule uniquely determines the order of enrollment.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 1 \leq A_j \leq N (1 \leq j \leq M)
  • 1 \leq B_j \leq N (1 \leq j \leq M)
  • A_j \neq B_j (1 \leq j \leq M)
  • If i \neq j, then (A_i, B_i) \neq (A_j, B_j) (the same prerequisite relationship is not given more than once)
  • There are no cycles in the prerequisite relationships
  • All input values are integers

Input

N M
A_1 B_1
A_2 B_2
\vdots
A_M B_M
  • The first line contains an integer N representing the number of courses and an integer M representing the number of prerequisite relationships, separated by a space.
  • The j-th of the following M lines (1 \leq j \leq M) contains integers A_j and B_j separated by a space, indicating that course A_j must be completed before taking course B_j.

Output

Output the order in which Takahashi takes the courses on a single line, separated by spaces. That is, output N integers arranged from left to right in the order they are taken.


Sample Input 1

4 3
1 2
3 4
2 4

Sample Output 1

1 2 3 4

Sample Input 2

5 3
2 1
3 1
5 4

Sample Output 2

2 3 1 5 4

Sample Input 3

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

Sample Output 3

1 2 3 4 5 6 7 8 9 10

Sample Input 4

15 12
15 14
14 13
13 12
12 11
1 5
2 5
5 6
3 7
8 9
9 10
4 10
6 11

Sample Output 4

1 2 3 4 5 6 7 8 9 10 15 14 13 12 11

Sample Input 5

1 0

Sample Output 5

1
E - 部分列のカウント

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 433

問題文

高橋君は数列に関するアルゴリズムの研究をしています。今日は、数列における部分列(subsequence)のマッチングの問題に取り組んでいます。

長さ N の数列 A = (A_1, A_2, \ldots, A_N) と、長さ K のパターン数列 P = (P_1, P_2, \ldots, P_K) が与えられます。数列 A およびパターン数列 P の各要素はいずれも正の整数です。

高橋君は、数列 A の中から異なる K 個の位置を選び、それらを元の順序を保ったまま並べた数列がパターン数列 P と一致するような選び方が何通りあるかを求めたいと思っています。

より正確には、1 \leq i_1 < i_2 < \cdots < i_K \leq N を満たす添字の組 (i_1, i_2, \ldots, i_K) であって、すべての j (1 \leq j \leq K) に対して A_{i_j} = P_j となるものの個数を求めてください。

ここで、選んだ添字の組が 1 つでも異なれば、たとえ選ばれた要素の値がすべて同じであっても、異なる選び方として数えることに注意してください。

答えは非常に大きくなる可能性があるため、10^9 + 7 で割った余りを求めてください。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq K \leq 100
  • K \leq N
  • 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq P_j \leq 10^9 (1 \leq j \leq K)
  • 入力はすべて整数

入力

N K
A_1 A_2 \ldots A_N
P_1 P_2 \ldots P_K
  • 1 行目には、数列 A の長さを表す整数 N と、パターン数列 P の長さを表す整数 K が、スペース区切りで与えられる。
  • 2 行目には、数列 A の要素 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。
  • 3 行目には、パターン数列 P の要素 P_1, P_2, \ldots, P_K が、スペース区切りで与えられる。

出力

条件を満たす添字の組の個数を 10^9 + 7 で割った余りを 1 行で出力せよ。


入力例 1

5 2
1 2 1 2 1
1 2

出力例 1

3

入力例 2

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

出力例 2

2

入力例 3

15 4
2 3 2 3 2 3 2 3 2 3 2 3 2 3 2
2 3 2 3

出力例 3

126

Score : 433 pts

Problem Statement

Takahashi is researching algorithms related to sequences. Today, he is working on a subsequence matching problem in sequences.

You are given a sequence A = (A_1, A_2, \ldots, A_N) of length N and a pattern sequence P = (P_1, P_2, \ldots, P_K) of length K. Each element of the sequence A and the pattern sequence P is a positive integer.

Takahashi wants to find the number of ways to choose K distinct positions from the sequence A such that the subsequence formed by those positions, preserving the original order, matches the pattern sequence P.

More precisely, find the number of index tuples (i_1, i_2, \ldots, i_K) satisfying 1 \leq i_1 < i_2 < \cdots < i_K \leq N such that A_{i_j} = P_j holds for all j (1 \leq j \leq K).

Note that if the chosen index tuples differ in even one position, they are counted as distinct selections, even if the values of all selected elements are the same.

Since the answer can be very large, find it modulo 10^9 + 7.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq K \leq 100
  • K \leq N
  • 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq P_j \leq 10^9 (1 \leq j \leq K)
  • All inputs are integers

Input

N K
A_1 A_2 \ldots A_N
P_1 P_2 \ldots P_K
  • The first line contains two integers N and K, separated by a space, representing the length of the sequence A and the length of the pattern sequence P, respectively.
  • The second line contains the elements A_1, A_2, \ldots, A_N of the sequence A, separated by spaces.
  • The third line contains the elements P_1, P_2, \ldots, P_K of the pattern sequence P, separated by spaces.

Output

Print on a single line the number of index tuples satisfying the condition, modulo 10^9 + 7.


Sample Input 1

5 2
1 2 1 2 1
1 2

Sample Output 1

3

Sample Input 2

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

Sample Output 2

2

Sample Input 3

15 4
2 3 2 3 2 3 2 3 2 3 2 3 2 3 2
2 3 2 3

Sample Output 3

126