A - Round-Trip Patrol

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 133

問題文

高橋君は、一直線に並んだ N 個のチェックポイントを管理する警備員です。

チェックポイントは左から順に 1, 2, \ldots, N と番号が付けられており、隣り合うチェックポイント間を移動するのにちょうど 1 秒かかります。高橋君の待機所はチェックポイント N にあります。

異常検知のアラームが鳴ると、高橋君は待機所(チェックポイント N)を出発し、チェックポイントを N, N-1, \ldots, 1 の順にたどってチェックポイント 1 まで移動します。その後、折り返してチェックポイントを 1, 2, \ldots, N の順にたどり、待機所(チェックポイント N)まで戻ります。各チェックポイントでの確認作業や折り返しにかかる時間は 0 秒とします。したがって、1 回のパトロールにかかる移動時間は 2(N-1) 秒です。

高橋君は必ず待機所に戻ってから次のパトロールに出発します。M 回のパトロールはすべて同じ経路・同じ移動時間で行われます。

ある日、アラームが M 回鳴り、高橋君は M 回の往復パトロールを行いました。高橋君がこの日の M 回のパトロールにおける移動時間の合計(秒)を求めてください。ただし、パトロール間の待機時間は含みません。

制約

  • 1 \leq N \leq 10^9
  • 1 \leq M \leq 10^9
  • 入力はすべて整数

入力

N M

チェックポイントの数を表す整数 N と、アラームが鳴った回数(パトロールの回数)を表す整数 M が、スペース区切りで 1 行に与えられる。

出力

高橋君が移動に費やした合計時間(秒)を整数で 1 行に出力してください。なお、答えは 2 \times 10^{18} 程度の大きさになることがあります。


入力例 1

3 1

出力例 1

4

入力例 2

5 4

出力例 2

32

入力例 3

1000000000 1000000000

出力例 3

1999999998000000000

Score : 133 pts

Problem Statement

Takahashi is a security guard who manages N checkpoints arranged in a straight line.

The checkpoints are numbered 1, 2, \ldots, N from left to right, and it takes exactly 1 second to move between adjacent checkpoints. Takahashi's station is at checkpoint N.

When an anomaly detection alarm sounds, Takahashi departs from his station (checkpoint N) and travels through the checkpoints in the order N, N-1, \ldots, 1 until he reaches checkpoint 1. He then turns around and travels through the checkpoints in the order 1, 2, \ldots, N, returning to his station (checkpoint N). The time required for inspection at each checkpoint and for turning around is 0 seconds. Therefore, the travel time for one patrol is 2(N-1) seconds.

Takahashi always returns to his station before departing for the next patrol. All M patrols follow the same route and take the same travel time.

One day, the alarm sounded M times, and Takahashi performed M round-trip patrols. Find the total travel time (in seconds) for Takahashi's M patrols that day. Note that waiting time between patrols is not included.

Constraints

  • 1 \leq N \leq 10^9
  • 1 \leq M \leq 10^9
  • All inputs are integers

Input

N M

An integer N representing the number of checkpoints and an integer M representing the number of times the alarm sounded (the number of patrols) are given on a single line, separated by a space.

Output

Print the total time (in seconds) Takahashi spent traveling as an integer on a single line. Note that the answer can be as large as approximately 2 \times 10^{18}.


Sample Input 1

3 1

Sample Output 1

4

Sample Input 2

5 4

Sample Output 2

32

Sample Input 3

1000000000 1000000000

Sample Output 3

1999999998000000000
B - Classroom Assignment

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 266

問題文

高橋君は学校の文化祭で N 日間にわたるイベントを運営しています。会場には M 個の教室があり、各教室では毎日それぞれ 1 つの講演が行われる予定です。教室 j1 \leq j \leq M)には 定員 C_j 人が設定されています。

i 日目(1 \leq i \leq N)には K_i 人の来場者が文化祭を訪れます。各来場者は参加したい教室を ちょうど 1 指定します。i 日目の k 番目(1 \leq k \leq K_i)の来場者が希望する教室の番号を P_{i,k} とします。なお、同じ日に同じ教室を希望する来場者が複数いることもあります。

各日ごとに、以下の処理を行います(日ごとの処理は互いに独立であり、ある日の結果が別の日に影響することはありません)。教室ごとにその日の希望者数を集計し、ある教室の希望者数が定員以下(すなわち希望者数 \leq 定員)であれば、希望者全員がその教室の講演に参加できます。一方、希望者数が定員を超えた場合(すなわち希望者数 > 定員)は、安全上の理由によりその教室の講演は 中止 となり、その教室を希望した来場者は 誰も 講演に参加できません。また、講演が中止になった教室の希望者が他の教室に振り替えられることもありません。

N 日間のイベント全体を通して、実際に講演に参加できた来場者の 延べ人数(各日の各教室における参加者数をすべて合計した値)を求めてください。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq M \leq 10^5
  • 1 \leq C_j \leq 10^9 (1 \leq j \leq M)
  • 1 \leq K_i (1 \leq i \leq N)
  • \displaystyle \sum_{i=1}^{N} K_i \leq 2 \times 10^5
  • 1 \leq P_{i,k} \leq M (1 \leq i \leq N,\ 1 \leq k \leq K_i)
  • 入力はすべて整数である。

入力

N M
C_1 C_2 \ldots C_M
K_1
P_{1,1} P_{1,2} \ldots P_{1,K_1}
K_2
P_{2,1} P_{2,2} \ldots P_{2,K_2}
\vdots
K_N
P_{N,1} P_{N,2} \ldots P_{N,K_N}
  • 1 行目には、イベントの日数を表す整数 N と、教室の個数を表す整数 M が、スペース区切りで与えられる。
  • 2 行目には、各教室の定員を表す整数 C_1, C_2, \ldots, C_M が、スペース区切りで与えられる。
  • 続く 2N 行で、各日の来場者情報が与えられる。i 日目の情報は 2 行からなる。
  • 1 行目には、i 日目の来場者数を表す整数 K_i が与えられる。
  • 2 行目には、K_i 人の来場者がそれぞれ希望する教室の番号を表す整数 P_{i,1}, P_{i,2}, \ldots, P_{i,K_i} が、スペース区切りで与えられる。

出力

N 日間を通して実際に講演に参加できた来場者の延べ人数を 1 行で出力せよ。


入力例 1

2 3
3 2 4
5
1 2 1 2 2
3
1 3 3

出力例 1

5

入力例 2

3 4
2 5 1 3
6
1 2 1 4 1 2
4
2 2 2 2
5
1 3 4 3 4

出力例 2

10

入力例 3

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

出力例 3

17

Score : 266 pts

Problem Statement

Takahashi is organizing an event spanning N days at his school's cultural festival. The venue has M classrooms, and each classroom is scheduled to hold exactly one lecture per day. Classroom j (1 \leq j \leq M) has a capacity of C_j people.

On day i (1 \leq i \leq N), K_i visitors come to the cultural festival. Each visitor specifies exactly one classroom they wish to attend. Let P_{i,k} denote the classroom number desired by the k-th visitor (1 \leq k \leq K_i) on day i. Note that multiple visitors may wish to attend the same classroom on the same day.

For each day, the following process is performed (the processing for each day is independent, and the result of one day does not affect another day). The number of applicants for each classroom on that day is tallied. If the number of applicants for a classroom is at most its capacity (i.e., number of applicants \leq capacity), then all applicants can attend that classroom's lecture. On the other hand, if the number of applicants exceeds the capacity (i.e., number of applicants > capacity), the lecture in that classroom is canceled for safety reasons, and none of the visitors who wished to attend that classroom can participate in any lecture. Furthermore, visitors whose desired classroom's lecture was canceled are not reassigned to other classrooms.

Determine the total number of visitors who actually attended a lecture across all N days of the event (the sum of the number of participants in each classroom on each day).

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq M \leq 10^5
  • 1 \leq C_j \leq 10^9 (1 \leq j \leq M)
  • 1 \leq K_i (1 \leq i \leq N)
  • \displaystyle \sum_{i=1}^{N} K_i \leq 2 \times 10^5
  • 1 \leq P_{i,k} \leq M (1 \leq i \leq N,\ 1 \leq k \leq K_i)
  • All input values are integers.

Input

N M
C_1 C_2 \ldots C_M
K_1
P_{1,1} P_{1,2} \ldots P_{1,K_1}
K_2
P_{2,1} P_{2,2} \ldots P_{2,K_2}
\vdots
K_N
P_{N,1} P_{N,2} \ldots P_{N,K_N}
  • The first line contains two space-separated integers: N, the number of days of the event, and M, the number of classrooms.
  • The second line contains the space-separated integers C_1, C_2, \ldots, C_M, representing the capacity of each classroom.
  • The following 2N lines provide the visitor information for each day. The information for day i consists of 2 lines:
  • The first line contains an integer K_i, the number of visitors on day i.
  • The second line contains the space-separated integers P_{i,1}, P_{i,2}, \ldots, P_{i,K_i}, representing the classroom numbers desired by each of the K_i visitors.

Output

Output in a single line the total number of visitors who actually attended a lecture across all N days.


Sample Input 1

2 3
3 2 4
5
1 2 1 2 2
3
1 3 3

Sample Output 1

5

Sample Input 2

3 4
2 5 1 3
6
1 2 1 4 1 2
4
2 2 2 2
5
1 3 4 3 4

Sample Output 2

10

Sample Input 3

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

Sample Output 3

17
C - Bulk Character Conversion

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

高橋君はテキストエディタを開発しています。このエディタには、複数のテキストファイルに対して一括で文字を置換する機能があります。

ある日、高橋君は N 個のテキストファイルを開きました。各ファイルには英小文字のみからなる文字列が1行ずつ書かれています。

高橋君はこれらのファイルに対して、 Q 回の置換操作を順番に実行します。各操作では、現在のすべてのファイルに含まれる特定の文字を、別の文字に一括で置き換えます。

すべての置換操作が完了した後、各ファイルに書かれている文字列を出力してください。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq Q \leq 10^5
  • 1 \leq |S_i| \leq 10^5
  • \sum_{i=1}^{N} |S_i| \leq 10^6
  • S_i は英小文字のみからなる
  • a_j, b_j は英小文字である

入力

N Q
S_1
S_2
:
S_N
a_1 b_1
a_2 b_2
:
a_Q b_Q
  • 1 行目には、ファイルの個数を表す N と、置換操作の回数を表す Q が、スペース区切りで与えられる。
  • 2 行目から N + 1 行目では、各ファイルに書かれた文字列が与えられる。
  • 1 + i 行目では、 i 番目のファイルに書かれた文字列 S_i が与えられる。
  • S_i は英小文字のみからなる。
  • N + 2 行目から N + Q + 1 行目では、各置換操作の内容が与えられる。
  • N + 1 + j 行目では、 j 回目の操作で置き換える元の文字 a_j と、置き換え先の文字 b_j がスペース区切りで与えられる。
  • a_j , b_j はそれぞれ英小文字 1 文字である。

出力

T_1
T_2
:
T_N
  • N 行にわたって、すべての置換操作が完了した後の各ファイルの文字列を出力する。
  • i 行目には、 i 番目のファイルに書かれている文字列 T_i を出力する。

入力例 1

3 2
abc
dog
banana
a b
b c

出力例 1

ccc
dog
ccncnc

入力例 2

2 3
hello
world
l x
o a
x l

出力例 2

hella
warld

入力例 3

5 5
abcdefghij
programming
contest
algorithm
datastructure
a z
e a
z e
t s
s t

出力例 3

ebcdafghij
progremming
contatt
elgorithm
detettructura

入力例 4

10 8
thequickbrownfoxjumpsoverthelazydog
abcdefghijklmnopqrstuvwxyz
aabbccddee
zzzzzyyyyyxxxxx
helloworldfromcompetitiveprogramming
substitutioncipherexample
replacementoperation
queriesandfiles
multiplestrings
finaloutputtest
a b
b c
c d
d e
e f
f g
g h
z a

出力例 4

thhquihkhrownhoxjumpsovhrthhlhayhoh
hhhhhhhhijklmnopqrstuvwxya
hhhhhhhhhh
aaaaayyyyyxxxxx
hhlloworlhhromhomphtitivhprohrhmminh
suhstitutionhiphhrhxhmplh
rhplhhhmhntophrhtion
quhrihshnhhilhs
multiplhstrinhs
hinhloutputthst

入力例 5

1 1
a
a b

出力例 5

b

Score : 300 pts

Problem Statement

Takahashi is developing a text editor. This editor has a feature that performs bulk character replacement across multiple text files.

One day, Takahashi opened N text files. Each file contains a single line consisting of a string of lowercase English letters.

Takahashi will perform Q replacement operations on these files in order. In each operation, a specific character contained in all current files is replaced with another character all at once.

After all replacement operations are completed, output the string written in each file.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq Q \leq 10^5
  • 1 \leq |S_i| \leq 10^5
  • \sum_{i=1}^{N} |S_i| \leq 10^6
  • S_i consists only of lowercase English letters
  • a_j, b_j are lowercase English letters

Input

N Q
S_1
S_2
:
S_N
a_1 b_1
a_2 b_2
:
a_Q b_Q
  • The first line contains N, the number of files, and Q, the number of replacement operations, separated by a space.
  • From the 2nd line to the (N + 1)-th line, the strings written in each file are given.
  • The (1 + i)-th line contains the string S_i written in the i-th file.
  • S_i consists only of lowercase English letters.
  • From the (N + 2)-th line to the (N + Q + 1)-th line, the details of each replacement operation are given.
  • The (N + 1 + j)-th line contains the original character a_j to be replaced and the destination character b_j in the j-th operation, separated by a space.
  • a_j and b_j are each a single lowercase English letter.

Output

T_1
T_2
:
T_N
  • Output N lines, each containing the string in the corresponding file after all replacement operations are completed.
  • The i-th line should contain the string T_i written in the i-th file.

Sample Input 1

3 2
abc
dog
banana
a b
b c

Sample Output 1

ccc
dog
ccncnc

Sample Input 2

2 3
hello
world
l x
o a
x l

Sample Output 2

hella
warld

Sample Input 3

5 5
abcdefghij
programming
contest
algorithm
datastructure
a z
e a
z e
t s
s t

Sample Output 3

ebcdafghij
progremming
contatt
elgorithm
detettructura

Sample Input 4

10 8
thequickbrownfoxjumpsoverthelazydog
abcdefghijklmnopqrstuvwxyz
aabbccddee
zzzzzyyyyyxxxxx
helloworldfromcompetitiveprogramming
substitutioncipherexample
replacementoperation
queriesandfiles
multiplestrings
finaloutputtest
a b
b c
c d
d e
e f
f g
g h
z a

Sample Output 4

thhquihkhrownhoxjumpsovhrthhlhayhoh
hhhhhhhhijklmnopqrstuvwxya
hhhhhhhhhh
aaaaayyyyyxxxxx
hhlloworlhhromhomphtitivhprohrhmminh
suhstitutionhiphhrhxhmplh
rhplhhhmhntophrhtion
quhrihshnhhilhs
multiplhstrinhs
hinhloutputthst

Sample Input 5

1 1
a
a b

Sample Output 5

b
D - Smart Shopper

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 366

問題文

高橋君は予算 K 円を持ってお店にやってきました。

お店には N 個の商品が並んでおり、 i 番目の商品の価格は C_i 円です。高橋君はこれらの商品の中から好きな商品を選んで購入できますが、各商品は最大1つまでしか購入できません。また、何も購入しなくても構いません。

ただし、購入する商品の価格の合計は予算 K 円以下でなければなりません。

高橋君はこの条件を満たしつつ、購入する商品の価格の合計をなるべく大きくしたいと考えています。

購入する商品の価格の合計が K 以下となるような選び方のうち、価格の合計の最大値を求めてください。

制約

  • 1 \leq N \leq 100
  • 1 \leq K \leq 10^4
  • 1 \leq C_i \leq 10^41 \leq i \leq N
  • 入力はすべて整数である

入力

N K
C_1 C_2 \ldots C_N
  • 1 行目には、商品の個数を表す整数 N と、予算を表す整数 K が、スペース区切りで与えられる。
  • 2 行目には、各商品の価格を表す整数 C_1, C_2, \ldots, C_N が、スペース区切りで与えられる。

出力

購入する商品の価格の合計が K 以下となるような選び方における、価格の合計の最大値を 1 行で出力せよ。


入力例 1

3 10
3 5 4

出力例 1

9

入力例 2

5 20
6 8 3 11 7

出力例 2

20

入力例 3

10 100
12 25 8 33 17 45 6 19 30 14

出力例 3

100

Score : 366 pts

Problem Statement

Takahashi has come to a store with a budget of K yen.

The store has N items on display, and the price of the i-th item is C_i yen. Takahashi can choose any items he likes from these items to purchase, but he can buy at most one of each item. He may also choose to buy nothing at all.

However, the total price of the purchased items must not exceed the budget of K yen.

Takahashi wants to maximize the total price of the purchased items while satisfying this condition.

Among all ways to select items such that the total price is at most K, find the maximum possible total price.

Constraints

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

Input

N K
C_1 C_2 \ldots C_N
  • The first line contains an integer N representing the number of items and an integer K representing the budget, separated by a space.
  • The second line contains integers C_1, C_2, \ldots, C_N representing the prices of the items, separated by spaces.

Output

Print in one line the maximum total price among all ways to select items such that the total price of the purchased items is at most K.


Sample Input 1

3 10
3 5 4

Sample Output 1

9

Sample Input 2

5 20
6 8 3 11 7

Sample Output 2

20

Sample Input 3

10 100
12 25 8 33 17 45 6 19 30 14

Sample Output 3

100
E - Peak Collection

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 433

問題文

高橋君は、縦走ルート沿いに一列に並ぶ N 個の山の山頂を巡ろうとしています。山頂にはルートの入口側から順に 1, 2, \ldots, N の番号がついており、高橋君はルートの入口から出発して番号の小さい方から大きい方へ一方通行で進みます。一度通り過ぎた山頂に戻ることはできません。

各山頂 i には「入山料」 C_i 円と「標高」 S_i が設定されています。高橋君はルート上を進みながら、各山頂に差し掛かったとき、その山頂に登頂するか、登頂せずに通過するかを選ぶことができます。登頂せずに通過する場合、入山料はかかりません。登頂する場合はその山頂の入山料を支払う必要があります。

高橋君には登山のこだわりがあり、次の条件をすべて満たすように登頂する山頂を選びたいと考えています。

  • 登頂数の上限: 登頂する山頂の数は K 個以下でなければならない。
  • 標高の単調増加: 登頂する山頂を番号の昇順に並べたとき、それらの標高は狭義単調増加でなければならない。すなわち、山頂 i_1 < i_2 < \cdots < i_m に順に登頂するとき、 S_{i_1} < S_{i_2} < \cdots < S_{i_m} を満たす必要がある。
  • 予算の制限: 登頂する山頂の入山料の合計が所持金 B 円以下でなければならない。すなわち、 C_{i_1} + C_{i_2} + \cdots + C_{i_m} \leq B を満たす必要がある。

高橋君は、上記の条件をすべて満たしつつ、登頂する山頂の数 m を最大化したいです。最大の m を求めてください。なお、一つも登頂しない( m = 0 )ことも許されます。

制約

  • 1 \leq N \leq 500
  • 1 \leq K \leq 50
  • 1 \leq B \leq 500
  • 1 \leq C_i \leq B1 \leq i \leq N
  • 1 \leq S_i \leq 10^91 \leq i \leq N
  • S_i \neq S_ji \neq j
  • 入力はすべて整数である。

入力

N K B
C_1 S_1
C_2 S_2
\vdots
C_N S_N
  • 1 行目には、山頂の数 N 、登頂数の上限 K 、所持金 B がスペース区切りで与えられる。
  • 続く N 行のうち i 行目( 1 \leq i \leq N )には、山頂 i の入山料 C_i と標高 S_i がスペース区切りで与えられる。

出力

条件をすべて満たすように山頂を選んだとき、登頂できる山頂の数の最大値 m1 行で出力せよ。


入力例 1

5 3 10
3 100
2 200
5 150
4 300
1 400

出力例 1

3

入力例 2

4 3 3
2 100
2 200
2 300
2 400

出力例 2

1

入力例 3

10 5 20
3 500
2 100
5 300
1 200
4 800
2 600
3 150
6 900
1 700
5 1000

出力例 3

5

入力例 4

20 10 50
5 320
3 150
7 410
2 80
4 500
6 230
1 600
8 90
3 700
5 350
2 850
4 120
6 950
1 40
3 1100
7 560
2 1200
5 780
4 1350
3 999

出力例 4

10

入力例 5

1 1 1
1 1000000000

出力例 5

1

Score : 433 pts

Problem Statement

Takahashi is trying to visit the summits of N mountains lined up in a row along a traverse route. The summits are numbered 1, 2, \ldots, N from the entrance side of the route, and Takahashi starts from the entrance and proceeds one-way from lower-numbered to higher-numbered summits. He cannot return to a summit he has already passed.

Each summit i has an "entrance fee" of C_i yen and an "elevation" of S_i. As Takahashi proceeds along the route, when he reaches each summit, he can choose either to climb it or to pass through without climbing. If he passes through without climbing, no entrance fee is charged. If he climbs it, he must pay that summit's entrance fee.

Takahashi has particular preferences for his mountaineering and wants to select which summits to climb such that all of the following conditions are satisfied:

  • Upper limit on number of climbs: The number of summits climbed must be at most K.
  • Strictly increasing elevation: When the climbed summits are listed in ascending order of their numbers, their elevations must be strictly increasing. That is, if he climbs summits i_1 < i_2 < \cdots < i_m in order, then S_{i_1} < S_{i_2} < \cdots < S_{i_m} must hold.
  • Budget constraint: The total entrance fees of the climbed summits must not exceed his funds of B yen. That is, C_{i_1} + C_{i_2} + \cdots + C_{i_m} \leq B must hold.

Takahashi wants to maximize the number of summits climbed m while satisfying all the above conditions. Find the maximum value of m. Note that climbing no summits (m = 0) is also allowed.

Constraints

  • 1 \leq N \leq 500
  • 1 \leq K \leq 50
  • 1 \leq B \leq 500
  • 1 \leq C_i \leq B (1 \leq i \leq N)
  • 1 \leq S_i \leq 10^9 (1 \leq i \leq N)
  • S_i \neq S_j (i \neq j)
  • All inputs are integers.

Input

N K B
C_1 S_1
C_2 S_2
\vdots
C_N S_N
  • The first line contains the number of summits N, the upper limit on climbs K, and the available funds B, separated by spaces.
  • In the following N lines, the i-th line (1 \leq i \leq N) contains the entrance fee C_i and elevation S_i of summit i, separated by spaces.

Output

Print in one line the maximum number of summits m that can be climbed while satisfying all the conditions.


Sample Input 1

5 3 10
3 100
2 200
5 150
4 300
1 400

Sample Output 1

3

Sample Input 2

4 3 3
2 100
2 200
2 300
2 400

Sample Output 2

1

Sample Input 3

10 5 20
3 500
2 100
5 300
1 200
4 800
2 600
3 150
6 900
1 700
5 1000

Sample Output 3

5

Sample Input 4

20 10 50
5 320
3 150
7 410
2 80
4 500
6 230
1 600
8 90
3 700
5 350
2 850
4 120
6 950
1 40
3 1100
7 560
2 1200
5 780
4 1350
3 999

Sample Output 4

10

Sample Input 5

1 1 1
1 1000000000

Sample Output 5

1