A - イベント払い戻し

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

配点 : 233

問題文

高橋君は、とあるイベント会場の受付で働いています。このイベントでは、チケットの種類として一般チケット学割チケット2 種類を販売しています。

ある日、イベントの一部プログラムが中止になったため、来場者に対してチケット代金を払い戻すことになりました。払い戻しのルールは以下の通りです。

  • 一般チケット(入力では normal と表記)を持っている来場者には、その来場者の購入金額 P_i 円の全額、すなわち P_i 円を払い戻す。
  • 学割チケット(入力では half と表記)を持っている来場者には、その来場者の購入金額 P_i 円の半額(小数点以下切り捨て)、すなわち \lfloor P_i / 2 \rfloor 円を払い戻す。

高橋君のもとに N 人の来場者が払い戻しの列に並んでいます。各来場者 i (1 \leq i \leq N) について、チケットの種類を表す文字列 T_i と購入金額 P_i (円)が与えられます。

高橋君が全員に払い戻す合計金額(円)を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • T_inormal または half のいずれかである。
  • 1 \leq P_i \leq 10^5
  • N および P_i はすべて整数である。

入力

N
T_1 P_1
T_2 P_2
\vdots
T_N P_N

1 行目には、来場者の人数を表す整数 N が与えられる。

続く N 行のうち i 行目 (1 \leq i \leq N) には、i 番目の来場者のチケットの種類を表す文字列 T_i と購入金額を表す整数 P_i (円)がスペース区切りで与えられる。

出力

高橋君が全員に払い戻す合計金額(円)を整数で 1 行に出力せよ。


入力例 1

3
normal 1000
half 500
half 300

出力例 1

1400

入力例 2

5
normal 2000
half 1999
normal 500
half 750
normal 1200

出力例 2

5074

入力例 3

10
normal 100000
half 99999
normal 50000
half 1
normal 12345
half 67890
normal 1
half 100000
normal 99999
half 55555

出力例 3

424066

Score : 233 pts

Problem Statement

Takahashi is working at the reception desk of a certain event venue. For this event, two types of tickets are sold: regular tickets and student discount tickets.

One day, part of the event program was canceled, so it was decided to refund the ticket prices to the visitors. The refund rules are as follows:

  • For visitors holding a regular ticket (denoted as normal in the input), the full amount of their purchase price P_i yen is refunded, i.e., P_i yen.
  • For visitors holding a student discount ticket (denoted as half in the input), half of their purchase price (rounded down) is refunded, i.e., \lfloor P_i / 2 \rfloor yen.

There are N visitors lined up for refunds at Takahashi's counter. For each visitor i (1 \leq i \leq N), a string T_i representing the ticket type and the purchase price P_i (in yen) are given.

Find the total amount (in yen) that Takahashi needs to refund to all visitors.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • T_i is either normal or half.
  • 1 \leq P_i \leq 10^5
  • N and all P_i are integers.

Input

N
T_1 P_1
T_2 P_2
\vdots
T_N P_N

The first line contains an integer N representing the number of visitors.

In the following N lines, the i-th line (1 \leq i \leq N) contains a string T_i representing the ticket type of the i-th visitor and an integer P_i representing the purchase price (in yen), separated by a space.

Output

Output the total refund amount (in yen) that Takahashi needs to pay to all visitors, as an integer on a single line.


Sample Input 1

3
normal 1000
half 500
half 300

Sample Output 1

1400

Sample Input 2

5
normal 2000
half 1999
normal 500
half 750
normal 1200

Sample Output 2

5074

Sample Input 3

10
normal 100000
half 99999
normal 50000
half 1
normal 12345
half 67890
normal 1
half 100000
normal 99999
half 55555

Sample Output 3

424066
B - 本棚の並び順

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

配点 : 300

問題文

高橋君の本棚には N 冊の本が一列に並んでいます。左から i 番目の本には整数 A_i で表される番号が書かれています。

青木君は、ある書店のショーウィンドウで M 冊の本が一列に並んでいるのを見ました。左から j 番目の本には整数 B_j で表される番号が書かれています。

なお、本の番号は重複することもあります。

高橋君の本棚の中に、ショーウィンドウの並びと完全に一致する連続区間が存在するかどうかを調べてください。

すなわち、A_i, A_{i+1}, \dots, A_{i+M-1} がそれぞれ B_1, B_2, \dots, B_M と一致するような整数 i (1 \le i \le N - M + 1) が存在するか判定してください。存在する場合はそのような i のうち最小のものを、存在しない場合は -1 を出力してください。

制約

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

入力

N M
A_1 A_2 \dots A_N
B_1 B_2 \dots B_M
  • 1 行目には、高橋君の本棚の冊数 N と、ショーウィンドウの冊数 M がスペース区切りで与えられる。
  • 2 行目には、高橋君の本棚の各本の番号 A_1, A_2, \dots, A_N がスペース区切りで与えられる。
  • 3 行目には、ショーウィンドウの各本の番号 B_1, B_2, \dots, B_M がスペース区切りで与えられる。

出力

A_i, A_{i+1}, \dots, A_{i+M-1} がそれぞれ B_1, B_2, \dots, B_M と一致するような最小の整数 i を 1 行で出力してください。そのような i が存在しない場合は -1 を出力してください。


入力例 1

6 3
4 1 2 3 5 6
2 3 5

出力例 1

3

入力例 2

4 2
1 3 1 3
3 3

出力例 2

-1

入力例 3

18 5
5 1 2 1 2 3 4 1 2 3 4 5 1 2 3 4 6 7
1 2 3 4 5

出力例 3

8

入力例 4

46 10
100 200 300 400 500 100 200 300 999 888 777 666 555 444 333 222 111 123 234 345 42 84 126 168 210 252 294 336 378 420 42 84 126 168 210 999999937 1 2 3 4 5 6 7 8 9 10
42 84 126 168 210 252 294 336 378 420

出力例 4

21

入力例 5

1 1
1000000000
1000000000

出力例 5

1

Score : 300 pts

Problem Statement

Takahashi's bookshelf has N books lined up in a row. The i-th book from the left has a number written on it represented by the integer A_i.

Aoki saw M books lined up in a row in a bookstore's show window. The j-th book from the left has a number written on it represented by the integer B_j.

Note that book numbers may have duplicates.

Determine whether there exists a contiguous subsequence in Takahashi's bookshelf that exactly matches the arrangement in the show window.

Specifically, determine whether there exists an integer i (1 \le i \le N - M + 1) such that A_i, A_{i+1}, \dots, A_{i+M-1} match B_1, B_2, \dots, B_M respectively. If such an i exists, output the smallest such i. If it does not exist, output -1.

Constraints

  • 1 \le M \le N \le 5 \times 10^5
  • 1 \le A_i \le 10^9 (1 \le i \le N)
  • 1 \le B_j \le 10^9 (1 \le j \le M)
  • All input values are integers

Input

N M
A_1 A_2 \dots A_N
B_1 B_2 \dots B_M
  • The first line contains the number of books on Takahashi's bookshelf N and the number of books in the show window M, separated by a space.
  • The second line contains the numbers of each book on Takahashi's bookshelf A_1, A_2, \dots, A_N, separated by spaces.
  • The third line contains the numbers of each book in the show window B_1, B_2, \dots, B_M, separated by spaces.

Output

Output in one line the smallest integer i such that A_i, A_{i+1}, \dots, A_{i+M-1} match B_1, B_2, \dots, B_M respectively. If no such i exists, output -1.


Sample Input 1

6 3
4 1 2 3 5 6
2 3 5

Sample Output 1

3

Sample Input 2

4 2
1 3 1 3
3 3

Sample Output 2

-1

Sample Input 3

18 5
5 1 2 1 2 3 4 1 2 3 4 5 1 2 3 4 6 7
1 2 3 4 5

Sample Output 3

8

Sample Input 4

46 10
100 200 300 400 500 100 200 300 999 888 777 666 555 444 333 222 111 123 234 345 42 84 126 168 210 252 294 336 378 420 42 84 126 168 210 999999937 1 2 3 4 5 6 7 8 9 10
42 84 126 168 210 252 294 336 378 420

Sample Output 4

21

Sample Input 5

1 1
1000000000
1000000000

Sample Output 5

1
C - 水やり当番

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

配点 : 366

問題文

高橋君は学校の園芸委員会に所属しています。校庭には東西方向に一直線に並んだ N 個のプランターがあり、西から順にプランター 1, プランター 2, \ldots, プランター N と番号が付けられています。水やりが始まる前の時点では、各プランターの水の量はすべて 0 リットルです。

今日から Q 日間、高橋君は水やり当番を担当することになりました。i 日目 (1 \leq i \leq Q) には、プランター L_i からプランター R_i まで(両端を含む)のそれぞれに C_i リットルずつの水を与えます。

Q 日間の水やりがすべて終わった後、プランター 1, プランター 2, \ldots, プランター N のそれぞれに与えられた水の合計量を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq L_i \leq R_i \leq N
  • 1 \leq C_i \leq 10^4
  • 入力はすべて整数

入力

N Q
L_1 R_1 C_1
L_2 R_2 C_2
\vdots
L_Q R_Q C_Q
  • 1 行目には、プランターの数 N と、水やりを行う日数 Q がスペース区切りで与えられる。
  • i + 1 行目 (1 \leq i \leq Q) には、i 日目に水を与えるプランターの番号の範囲の始点 L_i 、終点 R_i 、各プランターに与える水の量 C_i がスペース区切りで与えられる。

出力

N 個の整数をスペース区切りで 1 行に出力せよ。j 番目 (1 \leq j \leq N) の整数は、プランター j に与えられた水の合計量(リットル)を表す。すなわち、プランター 1 から順にプランター N までの合計量を出力せよ。


入力例 1

5 3
1 3 2
2 4 3
4 5 1

出力例 1

2 5 5 4 1

入力例 2

10 5
1 10 1
3 7 2
5 5 10
1 1 5
8 10 3

出力例 2

6 1 3 3 13 3 3 4 4 4

入力例 3

20 8
1 20 100
5 15 50
1 5 30
16 20 30
10 10 500
3 8 25
12 18 40
7 14 15

出力例 3

130 130 155 155 205 175 190 190 165 665 165 205 205 205 190 170 170 170 130 130

Score : 366 pts

Problem Statement

Takahashi is a member of the school gardening committee. In the schoolyard, there are N planters arranged in a straight line from east to west, numbered Planter 1, Planter 2, \ldots, Planter N from west to east. Before watering begins, the amount of water in each planter is 0 liters.

Starting today, Takahashi is assigned watering duty for Q days. On day i (1 \leq i \leq Q), he waters each planter from Planter L_i to Planter R_i (inclusive) with C_i liters of water.

After all Q days of watering are completed, find the total amount of water given to each of Planter 1, Planter 2, \ldots, Planter N.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq L_i \leq R_i \leq N
  • 1 \leq C_i \leq 10^4
  • All input values are integers

Input

N Q
L_1 R_1 C_1
L_2 R_2 C_2
\vdots
L_Q R_Q C_Q
  • The first line contains the number of planters N and the number of watering days Q, separated by a space.
  • The (i + 1)-th line (1 \leq i \leq Q) contains the starting planter number L_i, the ending planter number R_i, and the amount of water C_i given to each planter on day i, separated by spaces.

Output

Print N integers separated by spaces on a single line. The j-th integer (1 \leq j \leq N) represents the total amount of water (in liters) given to Planter j. That is, print the total amounts from Planter 1 through Planter N in order.


Sample Input 1

5 3
1 3 2
2 4 3
4 5 1

Sample Output 1

2 5 5 4 1

Sample Input 2

10 5
1 10 1
3 7 2
5 5 10
1 1 5
8 10 3

Sample Output 2

6 1 3 3 13 3 3 4 4 4

Sample Input 3

20 8
1 20 100
5 15 50
1 5 30
16 20 30
10 10 500
3 8 25
12 18 40
7 14 15

Sample Output 3

130 130 155 155 205 175 190 190 165 665 165 205 205 205 190 170 170 170 130 130
D - 細胞分裂

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

配点 : 400

問題文

高橋君は、N 個の細胞からなる培養サンプルを観察している。

各細胞には正の整数で表される活性値がある。はじめ、i 番目の細胞の活性値は A_i である。

高橋君は、培養サンプル中に活性値が 2 以上の細胞が存在する限り、以下の一連の手順を 1 回の操作 として繰り返し行う。

  1. 培養サンプル中の活性値が 2 以上の細胞を 1 つ選ぶ。選んだ細胞の活性値を x とする。
  2. x を割り切る素数 p を 1 つ選ぶ。
  3. その細胞を培養サンプルから取り除き、代わりに活性値 x / p の細胞を p 個培養サンプルに加える。(px の約数であるため x / p は正の整数である。)

1 回の操作で細胞の総数は p - 1 個増える。新たに加えられた細胞の活性値が 2 以上であれば、以降の操作で選ぶ対象となる。活性値が 1 の細胞は操作の対象にならない。

各操作において、どの細胞を選ぶか、またどの素数 p を選ぶかは自由に決めることができる。これらの選び方によって、すべての細胞の活性値が 1 になるまでの操作回数は変わりうる。

すべての細胞の活性値を 1 にするために必要な操作回数の 最小値最大値 を求めよ。

制約

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

入力

N
A_1 A_2 \cdots A_N

1 行目には、細胞の個数を表す整数 N が与えられる。

2 行目には、N 個の整数 A_1, A_2, \ldots, A_N が空白区切りで与えられる。

出力

必要な操作回数の最小値と最大値をこの順に、スペース区切りで 1 行に出力せよ。


入力例 1

3
2 3 4

出力例 1

5 5

入力例 2

4
1 6 8 9

出力例 2

14 15

入力例 3

10
12 15 16 18 20 25 27 30 49 64

出力例 3

141 171

入力例 4

24
2 4 6 8 9 10 12 15 16 18 20 21 24 25 27 30 32 36 40 45 49 64 81 1000000

出力例 4

250346 988666

入力例 5

1
1

出力例 5

0 0

Score : 400 pts

Problem Statement

Takahashi is observing a culture sample consisting of N cells.

Each cell has an activity value represented by a positive integer. Initially, the activity value of the i-th cell is A_i.

As long as there exists a cell with an activity value of 2 or more in the culture sample, Takahashi repeatedly performs the following sequence of steps as one operation:

  1. Choose one cell from the culture sample whose activity value is 2 or more. Let x be the activity value of the chosen cell.
  2. Choose a prime number p that divides x.
  3. Remove that cell from the culture sample, and add p new cells each with activity value x / p to the culture sample. (Since p is a divisor of x, x / p is a positive integer.)

Each operation increases the total number of cells by p - 1. If the activity value of the newly added cells is 2 or more, they become eligible to be chosen in subsequent operations. Cells with an activity value of 1 cannot be chosen for operations.

In each operation, Takahashi is free to choose which cell to select and which prime number p to use. Depending on these choices, the number of operations required until all cells have an activity value of 1 may vary.

Find the minimum and maximum number of operations required to make the activity values of all cells equal to 1.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^6
  • All input values are integers.

Input

N
A_1 A_2 \cdots A_N

The first line contains an integer N representing the number of cells.

The second line contains N integers A_1, A_2, \ldots, A_N separated by spaces.

Output

Print the minimum and maximum number of required operations in this order, separated by a space, on a single line.


Sample Input 1

3
2 3 4

Sample Output 1

5 5

Sample Input 2

4
1 6 8 9

Sample Output 2

14 15

Sample Input 3

10
12 15 16 18 20 25 27 30 49 64

Sample Output 3

141 171

Sample Input 4

24
2 4 6 8 9 10 12 15 16 18 20 21 24 25 27 30 32 36 40 45 49 64 81 1000000

Sample Output 4

250346 988666

Sample Input 5

1
1

Sample Output 5

0 0
E - 区間の評価値

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

配点 : 466

問題文

高橋君は長さ N の整数列 A_1, A_2, \ldots, A_N を持っています。

高橋君は、この数列から連続する W 個の要素からなる区間を 1 つ選び、その 評価値 を最大化したいと考えています。

具体的には、整数 l1 \leq l \leq N - W + 1 )を 1 つ選び、区間 A_l, A_{l+1}, \ldots, A_{l+W-1} の評価値を次の式で定義します:

\text{評価値} = \left(\sum_{i=l}^{l+W-1} A_i\right) + K \times \min(A_l, A_{l+1}, \ldots, A_{l+W-1})

すなわち、評価値は区間内の要素の総和に、区間内の最小値と整数 K の積を加えたものです。

K および各 A_i は正・零・負のいずれの値もとりえることに注意してください。

l としてありうるすべての値( 1 \leq l \leq N - W + 1 )に対する評価値の最大値を求めてください。

制約

  • 1 \leq N \leq 5 \times 10^5
  • 1 \leq W \leq N
  • -10^6 \leq K \leq 10^6
  • -10^9 \leq A_i \leq 10^91 \leq i \leq N
  • 入力はすべて整数である

入力

N W K
A_1 A_2 \ldots A_N
  • 1 行目には、数列の長さを表す整数 N、区間の長さを表す整数 W、最小値に掛ける係数を表す整数 K が、スペース区切りで与えられる。
  • 2 行目には、数列の各要素を表す整数 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。

出力

評価値の最大値を整数として 1 行で出力せよ。


入力例 1

5 3 2
1 3 2 5 4

出力例 1

15

入力例 2

4 2 -3
5 1 4 3

出力例 2

3

入力例 3

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

出力例 3

7

入力例 4

20 5 -2
10 -7 3 8 -1 6 2 -4 9 5 -3 7 1 -8 4 11 -6 2 0 3

出力例 4

31

入力例 5

1 1 1000000
-1000000000

出力例 5

-1000001000000000

Score : 466 pts

Problem Statement

Takahashi has an integer sequence A_1, A_2, \ldots, A_N of length N.

Takahashi wants to select one contiguous interval of W elements from this sequence and maximize its evaluation score.

Specifically, he chooses an integer l (1 \leq l \leq N - W + 1) and defines the evaluation score of the interval A_l, A_{l+1}, \ldots, A_{l+W-1} by the following formula:

\text{Evaluation Score} = \left(\sum_{i=l}^{l+W-1} A_i\right) + K \times \min(A_l, A_{l+1}, \ldots, A_{l+W-1})

That is, the evaluation score is the sum of the elements in the interval plus the product of the minimum value in the interval and the integer K.

Note that K and each A_i can be positive, zero, or negative.

Find the maximum evaluation score over all possible values of l (1 \leq l \leq N - W + 1).

Constraints

  • 1 \leq N \leq 5 \times 10^5
  • 1 \leq W \leq N
  • -10^6 \leq K \leq 10^6
  • -10^9 \leq A_i \leq 10^9 (1 \leq i \leq N)
  • All inputs are integers

Input

N W K
A_1 A_2 \ldots A_N
  • The first line contains the integer N representing the length of the sequence, the integer W representing the length of the interval, and the integer K representing the coefficient multiplied by the minimum value, separated by spaces.
  • The second line contains the integers A_1, A_2, \ldots, A_N representing the elements of the sequence, separated by spaces.

Output

Output the maximum evaluation score as an integer on a single line.


Sample Input 1

5 3 2
1 3 2 5 4

Sample Output 1

15

Sample Input 2

4 2 -3
5 1 4 3

Sample Output 2

3

Sample Input 3

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

Sample Output 3

7

Sample Input 4

20 5 -2
10 -7 3 8 -1 6 2 -4 9 5 -3 7 1 -8 4 11 -6 2 0 3

Sample Output 4

31

Sample Input 5

1 1 1000000
-1000000000

Sample Output 5

-1000001000000000