A - 歌詞検索

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

配点 : 266

問題文

高橋君は音楽が大好きで、お気に入りの曲の歌詞を集めたデータベースを作成しています。

高橋君は N 曲分の歌詞データを持っています。各曲には 1 から N までの番号が付けられており、i 番目の曲の歌詞は英小文字からなる文字列 S_i で表されています。

高橋君は、各曲の「夏らしさスコア」を計算することにしました。曲 i の夏らしさスコアは、文字列 S_i の中に連続する部分文字列(substring)として tanabata が出現する回数として定義されます。

より正確には、S_ij 文字目を S_i[j]1-indexed)と表すとき、S_i[j]S_i[j+1] \cdots S_i[j+7]tanabata と一致するような整数 j1 \leq j \leq |S_i| - 7)の個数です。ここで |S_i| は文字列 S_i の長さを表します。|S_i| \leq 7 の場合は該当する j が存在しないため、夏らしさスコアは 0 です。

N 曲の中で、夏らしさスコアが最大の曲の番号を求めてください。夏らしさスコアが最大の曲が複数ある場合は、そのうち番号が最も小さいものを出力してください。なお、すべての曲の夏らしさスコアが 0 である場合も同様に、番号が最も小さい曲(すなわち曲 1)を出力してください。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq |S_i| \leq 10^5
  • \sum_{i=1}^{N} |S_i| \leq 10^6
  • S_i は英小文字からなる文字列

入力

N
S_1
S_2
\vdots
S_N
  • 1 行目には、曲の数を表す整数 N が与えられる。
  • 続く N 行のうち i 行目(1 \leq i \leq N)には、i 番目の曲の歌詞を表す文字列 S_i が与えられる。

出力

夏らしさスコアが最大の曲の番号を 1 行で出力してください。該当する曲が複数ある場合は、番号が最も小さいものを出力してください。


入力例 1

3
tanabata
summer
tanabatatanabata

出力例 1

3

入力例 2

5
abctanabatadef
tanabata
xxxxxxxxxx
tanabatanabata
holiday

出力例 2

4

入力例 3

10
thesummerstarfestivaltanabataisbeautiful
tanabatatanabatatanabata
abcdefghijklmnopqrstuvwxyz
aaatanabatabbbtanabataccc
summerbeachsernlight
tanabatanabatanabatanabata
zzzzzzzzzzzzzzzzzzzzz
atanabatax
tanabata
mytanabatamemorytanabatadreamtanabatanight

出力例 3

6

Score : 266 pts

Problem Statement

Takahashi loves music and is building a database of lyrics from his favorite songs.

Takahashi has lyrics data for N songs. Each song is numbered from 1 to N, and the lyrics of the i-th song are represented by a string S_i consisting of lowercase English letters.

Takahashi decided to calculate a "summer score" for each song. The summer score of song i is defined as the number of times tanabata appears as a contiguous substring in the string S_i.

More precisely, letting S_i[j] denote the j-th character of S_i (1-indexed), the summer score is the number of integers j (1 \leq j \leq |S_i| - 7) such that S_i[j]S_i[j+1] \cdots S_i[j+7] matches tanabata. Here, |S_i| denotes the length of the string S_i. If |S_i| \leq 7, no such j exists, so the summer score is 0.

Among the N songs, find the number of the song with the maximum summer score. If there are multiple songs with the maximum summer score, output the one with the smallest number. Note that if all songs have a summer score of 0, likewise output the song with the smallest number (i.e., song 1).

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq |S_i| \leq 10^5
  • \sum_{i=1}^{N} |S_i| \leq 10^6
  • S_i is a string consisting of lowercase English letters

Input

N
S_1
S_2
\vdots
S_N
  • The first line contains an integer N representing the number of songs.
  • The i-th line (1 \leq i \leq N) of the following N lines contains the string S_i representing the lyrics of the i-th song.

Output

Output the number of the song with the maximum summer score on a single line. If there are multiple such songs, output the one with the smallest number.


Sample Input 1

3
tanabata
summer
tanabatatanabata

Sample Output 1

3

Sample Input 2

5
abctanabatadef
tanabata
xxxxxxxxxx
tanabatanabata
holiday

Sample Output 2

4

Sample Input 3

10
thesummerstarfestivaltanabataisbeautiful
tanabatatanabatatanabata
abcdefghijklmnopqrstuvwxyz
aaatanabatabbbtanabataccc
summerbeachsernlight
tanabatanabatanabatanabata
zzzzzzzzzzzzzzzzzzzzz
atanabatax
tanabata
mytanabatamemorytanabatadreamtanabatanight

Sample Output 3

6
B - 水やり当番

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

配点 : 300

問題文

高橋君が住むマンションでは、N 人の住人が毎日 1 人ずつ交代で共用花壇の水やり当番を務めています。住人には 1 から N までの番号が付いています。

1 日目の当番は住人 S で、以降は番号順に S, S+1, \ldots, N, 1, 2, \ldots のように巡回します。すなわち、j 日目 (j = 1, 2, \ldots, D) の当番は住人 ((S - 1 + j - 1) \bmod N) + 1 です。

住人 i が当番の日には、花壇に A_i リットルの水をやります。住人 i が複数回当番になる場合でも、毎回同じ A_i リットルの水をやります。

1 日目から D 日目までの D 日間で花壇に使われる水の合計は何リットルか求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq D \leq 10^{14}
  • 1 \leq S \leq N
  • 1 \leq A_i \leq 10^4
  • 入力はすべて整数である

入力

N D S
A_1 A_2 \ldots A_N
  • 1 行目には、住人の人数 N、日数 D1 日目の当番の住人番号 S がスペース区切りで与えられる。
  • 2 行目には、住人 i (1 \leq i \leq N) が当番の日に使う水の量 A_i(リットル)が N 個、スペース区切りで与えられる。

出力

D 日間で使われる水の合計量(リットル)を整数として 1 行で出力せよ。


入力例 1

4 6 2
3 1 4 2

出力例 1

15

入力例 2

5 3 5
10 20 30 40 50

出力例 2

80

入力例 3

8 25 6
7 2 9 4 5 1 8 3

出力例 3

118

入力例 4

20 12345678901234 13
15 230 7 89 154 321 48 76 205 19 410 63 97 182 54 268 11 340 125 72

出力例 4

1719753070941911

入力例 5

1 100000000000000 1
10000

出力例 5

1000000000000000000

Score : 300 pts

Problem Statement

In the apartment building where Takahashi lives, N residents take turns watering the shared flower bed, with one person on duty each day. The residents are numbered from 1 to N.

The person on duty on day 1 is resident S, and from then on, duty rotates in order of their numbers: S, S+1, \ldots, N, 1, 2, \ldots. That is, the person on duty on day j (j = 1, 2, \ldots, D) is resident ((S - 1 + j - 1) \bmod N) + 1.

When resident i is on duty, they water the flower bed with A_i liters of water. Even if resident i is on duty multiple times, they use the same A_i liters of water each time.

Find the total amount of water (in liters) used on the flower bed over the D days from day 1 to day D.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq D \leq 10^{14}
  • 1 \leq S \leq N
  • 1 \leq A_i \leq 10^4
  • All input values are integers.

Input

N D S
A_1 A_2 \ldots A_N
  • The first line contains the number of residents N, the number of days D, and the resident number S of the person on duty on day 1, separated by spaces.
  • The second line contains N space-separated values A_i (1 \leq i \leq N), the amount of water (in liters) that resident i uses when on duty.

Output

Output the total amount of water (in liters) used over the D days as a single integer on one line.


Sample Input 1

4 6 2
3 1 4 2

Sample Output 1

15

Sample Input 2

5 3 5
10 20 30 40 50

Sample Output 2

80

Sample Input 3

8 25 6
7 2 9 4 5 1 8 3

Sample Output 3

118

Sample Input 4

20 12345678901234 13
15 230 7 89 154 321 48 76 205 19 410 63 97 182 54 268 11 340 125 72

Sample Output 4

1719753070941911

Sample Input 5

1 100000000000000 1
10000

Sample Output 5

1000000000000000000
C - 配達員の割り当て

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

配点 : 366

問題文

高橋君は配送センターの管理者です。配送センターには N 人の配達員がおり、配達員には 1 から N までの番号が付けられています。配達員 i の体力値は A_i です。

今日は M 件の配達依頼があり、配達依頼 j をこなすには体力値が B_j 以上の配達員が必要です。各配達依頼にはちょうど 1 人の配達員を割り当てなければなりません。また、1 人の配達員は最大 1 件の配達依頼にしか割り当てられません。

ここで、配達依頼 j に配達員 i を割り当てたとき、 A_i = B_j であればその割り当てはぴったりであるとします。ぴったりの割り当ては、配達員の体力を無駄なく活かせるため理想的です。

高橋君は、以下の 2 つの目標をこの優先順位で達成したいと考えています。

  1. 第一目標: M 件すべての配達依頼に、必要な体力値を満たす配達員を割り当てる(すなわち、配達依頼 j に割り当てる配達員 iA_i \geq B_j を満たす)。もしすべての配達依頼に配達員を割り当てることが不可能な場合は -1 を出力してください。
  2. 第二目標: 第一目標を達成する割り当ての中で、ぴったりの割り当て( A_i = B_j となるペア)の数を最大化する。

第一目標が達成可能な場合、ぴったりの割り当ての最大数を出力してください。

制約

  • 1 \leq M \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq B_j \leq 10^9 (1 \leq j \leq M)
  • 入力はすべて整数である。

入力

N M
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M
  • 1 行目には、配達員の人数を表す N と配達依頼の件数を表す M が、スペース区切りで与えられる。
  • 2 行目には、各配達員の体力値を表す A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。
  • 3 行目には、各配達依頼の必要体力値を表す B_1, B_2, \ldots, B_M が、スペース区切りで与えられる。

出力

すべての配達依頼に配達員を割り当てることが不可能な場合は -1 を出力してください。可能な場合は、ぴったりの割り当ての最大数を 1 行で出力してください。


入力例 1

4 3
3 5 5 7
5 4 3

出力例 1

2

入力例 2

3 3
2 4 6
3 5 7

出力例 2

-1

入力例 3

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

出力例 3

4

入力例 4

20 15
2 5 5 7 8 10 10 12 15 15 18 20 21 25 30 30 35 40 45 50
1 5 6 10 10 14 15 19 20 22 30 31 35 44 50

出力例 4

8

入力例 5

1 1
1000000000
1000000000

出力例 5

1

Score : 366 pts

Problem Statement

Takahashi is the manager of a delivery center. The delivery center has N delivery workers, numbered from 1 to N. The stamina value of delivery worker i is A_i.

Today there are M delivery requests, and delivery request j requires a delivery worker with a stamina value of at least B_j. Exactly one delivery worker must be assigned to each delivery request. Also, each delivery worker can be assigned to at most one delivery request.

Here, when delivery worker i is assigned to delivery request j, if A_i = B_j, then the assignment is called a perfect match. A perfect match is ideal because it makes full use of the delivery worker's stamina without waste.

Takahashi wants to achieve the following two goals in this order of priority:

  1. Primary goal: Assign a delivery worker who meets the required stamina value to all M delivery requests (that is, delivery worker i assigned to delivery request j must satisfy A_i \geq B_j). If it is impossible to assign a delivery worker to every delivery request, output -1.
  2. Secondary goal: Among all assignments that achieve the primary goal, maximize the number of perfect matches (pairs where A_i = B_j).

If the primary goal is achievable, output the maximum number of perfect matches.

Constraints

  • 1 \leq M \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq B_j \leq 10^9 (1 \leq j \leq M)
  • All inputs are integers.

Input

N M
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M
  • The first line contains N, the number of delivery workers, and M, the number of delivery requests, separated by a space.
  • The second line contains A_1, A_2, \ldots, A_N, the stamina values of each delivery worker, separated by spaces.
  • The third line contains B_1, B_2, \ldots, B_M, the required stamina values of each delivery request, separated by spaces.

Output

If it is impossible to assign a delivery worker to every delivery request, output -1. If it is possible, output the maximum number of perfect matches in one line.


Sample Input 1

4 3
3 5 5 7
5 4 3

Sample Output 1

2

Sample Input 2

3 3
2 4 6
3 5 7

Sample Output 2

-1

Sample Input 3

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

Sample Output 3

4

Sample Input 4

20 15
2 5 5 7 8 10 10 12 15 15 18 20 21 25 30 30 35 40 45 50
1 5 6 10 10 14 15 19 20 22 30 31 35 44 50

Sample Output 4

8

Sample Input 5

1 1
1000000000
1000000000

Sample Output 5

1
D - 夏祭りの出店計画

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

配点 : 400

問題文

高橋君は N 日間にわたって開催される夏祭りに出店する計画を立てています。祭りの各日には 1 から N までの番号が付けられており、日 i1 \leq i \leq N)の売上見込みは A_i です。

高橋君は、祭り期間中に 2つの出店期間 を選びます。それぞれの出店期間は ちょうど K 日間 の連続した日からなり、1つ目の出店期間は2つ目の出店期間よりも前の日程とします。さらに、食材の仕入れや機材の整備のため、1つ目の出店期間と2つ目の出店期間は重複してはならず、2つの出店期間の間には 少なくとも1日の空き(どちらの出店期間にも含まれない日が1日以上存在すること)を設ける必要があります。

具体的には、1つ目の出店期間を日 s から日 s+K-1 まで、2つ目の出店期間を日 t から日 t+K-1 までとするとき、以下の条件をすべて満たす必要があります。

  • 1 \leq s かつ s + K - 1 \leq N(1つ目の出店期間が祭り期間内に収まる)
  • 1 \leq t かつ t + K - 1 \leq N(2つ目の出店期間が祭り期間内に収まる)
  • t \geq s + K + 1(1つ目の出店期間の最終日 s+K-1 と2つ目の出店期間の初日 t の間に、少なくとも1日の空きがある)

高橋君は、2つの出店期間に含まれる全日の売上見込みの合計、すなわち

\sum_{i=s}^{s+K-1} A_i + \sum_{i=t}^{t+K-1} A_i

をできるだけ大きくしたいと考えています。

この合計の最大値を求めてください。

制約

  • 3 \leq N \leq 10^6
  • 1 \leq K \leq \lfloor \frac{N-1}{2} \rfloor(すなわち 2K + 1 \leq N
  • 1 \leq A_i \leq 10^91 \leq i \leq N
  • 入力はすべて整数

K の制約により、条件を満たす2つの出店期間の選び方が必ず1つ以上存在することが保証されます。


入力

N K
A_1 A_2 \ldots A_N
  • 1 行目には、祭りの日数を表す整数 N と、各出店期間の長さを表す整数 K が、スペース区切りで与えられる。
  • 2 行目には、各日の売上見込みを表す N 個の整数 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。

出力

2つの出店期間に含まれる全日の売上見込みの合計の最大値を1行で出力せよ。


入力例 1

7 2
3 5 2 6 1 4 3

出力例 1

15

入力例 2

5 2
1 2 3 4 5

出力例 2

12

入力例 3

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

出力例 3

39

入力例 4

20 4
10 20 30 40 50 60 70 80 90 100 90 80 70 60 50 40 30 20 10 5

出力例 4

600

入力例 5

3 1
100 1 100

出力例 5

200

Score : 400 pts

Problem Statement

Takahashi is planning to set up a booth at a summer festival that runs for N days. Each day of the festival is numbered from 1 to N, and the expected sales for day i (1 \leq i \leq N) is A_i.

During the festival period, Takahashi will choose two booth periods. Each booth period consists of exactly K consecutive days, and the first booth period must be scheduled before the second booth period. Furthermore, due to ingredient procurement and equipment maintenance, the two booth periods must not overlap, and there must be at least one day of gap between them (at least one day that is not included in either booth period).

Specifically, if the first booth period runs from day s to day s+K-1, and the second booth period runs from day t to day t+K-1, then all of the following conditions must be satisfied:

  • 1 \leq s and s + K - 1 \leq N (the first booth period fits within the festival period)
  • 1 \leq t and t + K - 1 \leq N (the second booth period fits within the festival period)
  • t \geq s + K + 1 (there is at least one day of gap between the last day s+K-1 of the first booth period and the first day t of the second booth period)

Takahashi wants to maximize the total expected sales over all days included in the two booth periods, that is,

\sum_{i=s}^{s+K-1} A_i + \sum_{i=t}^{t+K-1} A_i

Find the maximum value of this total.

Constraints

  • 3 \leq N \leq 10^6
  • 1 \leq K \leq \lfloor \frac{N-1}{2} \rfloor (i.e., 2K + 1 \leq N)
  • 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
  • All inputs are integers

The constraint on K guarantees that there always exists at least one valid way to choose the two booth periods.


Input

N K
A_1 A_2 \ldots A_N
  • The first line contains two integers separated by a space: N, the number of days of the festival, and K, the length of each booth period.
  • The second line contains N integers separated by spaces: A_1, A_2, \ldots, A_N, representing the expected sales for each day.

Output

Print the maximum value of the total expected sales over all days included in the two booth periods, on a single line.


Sample Input 1

7 2
3 5 2 6 1 4 3

Sample Output 1

15

Sample Input 2

5 2
1 2 3 4 5

Sample Output 2

12

Sample Input 3

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

Sample Output 3

39

Sample Input 4

20 4
10 20 30 40 50 60 70 80 90 100 90 80 70 60 50 40 30 20 10 5

Sample Output 4

600

Sample Input 5

3 1
100 1 100

Sample Output 5

200
E - 山道のハイキングコース

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

配点 : 433

問題文

高橋君は山岳地帯のハイキングコースの整備を担当しています。このコースには N 個のチェックポイントが一列に並んでおり、左から i 番目のチェックポイントの現在の標高は H_i です。

安全管理のため、青木君はコースの整備について次の条件を要求しています:

  • すべてのチェックポイントの標高は非負整数であること。
  • 隣り合うどの 2 つのチェックポイントについても、標高の差の絶対値がちょうど 1 であること。つまり、整備後の i 番目のチェックポイントの標高を H'_i としたとき、すべての 1 \le i \le N - 1 について |H'_i - H'_{i+1}| = 1 を満たすこと。

高橋君は、上の条件を満たすように各チェックポイントの標高を非負整数の範囲で自由に設定し直すことができます。ただし、チェックポイントの標高を変更するには大がかりな工事が必要なため、できるだけ現在の標高のまま残せるチェックポイントの数を多くしたいと考えています。

すなわち、上の条件を満たす非負整数列 H'_1, H'_2, \ldots, H'_N を適切に定めたとき、H_i = H'_i となるチェックポイントの個数の最大値を求めてください。

制約

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

入力

N
H_1 H_2 \ldots H_N
  • 1 行目には、チェックポイントの個数を表す整数 N が与えられる。
  • 2 行目には、各チェックポイントの現在の標高を表す N 個の整数 H_1, H_2, \ldots, H_N がスペース区切りで与えられる。

出力

現在の標高のまま残せるチェックポイントの個数の最大値を 1 行で出力せよ。


入力例 1

5
0 1 2 1 0

出力例 1

5

入力例 2

6
2 2 2 2 2 2

出力例 2

3

入力例 3

15
2 3 4 5 4 3 2 100 99 98 97 0 1 1000000000 999999999

出力例 3

7

入力例 4

50
0 1 0 1 2 3 10 9 8 20 19 18 17 16 15 14 13 12 11 10 0 0 0 5 4 3 2 1 0 1 2 100 101 102 103 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36

出力例 4

15

入力例 5

1
1000000000

出力例 5

1

Score : 433 pts

Problem Statement

Takahashi is in charge of maintaining a hiking course in a mountainous area. This course consists of N checkpoints lined up in a row, and the current elevation of the i-th checkpoint from the left is H_i.

For safety management, Aoki requires the following conditions for the maintenance of the course:

  • The elevation of every checkpoint must be a non-negative integer.
  • For any two adjacent checkpoints, the absolute difference in their elevations must be exactly 1. That is, if we denote the elevation of the i-th checkpoint after maintenance as H'_i, then |H'_i - H'_{i+1}| = 1 must hold for all 1 \le i \le N - 1.

Takahashi can freely reassign the elevation of each checkpoint to non-negative integers so that the conditions above are satisfied. However, since changing the elevation of a checkpoint requires large-scale construction, he wants to keep as many checkpoints at their current elevations as possible.

In other words, when we appropriately determine a sequence of non-negative integers H'_1, H'_2, \ldots, H'_N that satisfies the conditions above, find the maximum number of checkpoints for which H_i = H'_i.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq H_i \leq 10^9
  • All input values are integers.

Input

N
H_1 H_2 \ldots H_N
  • The first line contains an integer N, representing the number of checkpoints.
  • The second line contains N space-separated integers H_1, H_2, \ldots, H_N, representing the current elevations of the checkpoints.

Output

Print the maximum number of checkpoints that can be kept at their current elevations in a single line.


Sample Input 1

5
0 1 2 1 0

Sample Output 1

5

Sample Input 2

6
2 2 2 2 2 2

Sample Output 2

3

Sample Input 3

15
2 3 4 5 4 3 2 100 99 98 97 0 1 1000000000 999999999

Sample Output 3

7

Sample Input 4

50
0 1 0 1 2 3 10 9 8 20 19 18 17 16 15 14 13 12 11 10 0 0 0 5 4 3 2 1 0 1 2 100 101 102 103 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36

Sample Output 4

15

Sample Input 5

1
1000000000

Sample Output 5

1