A - Typhoon Damage Survey

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

高橋君は、果樹園の管理人です。この果樹園には N 本の果樹が植えられており、それぞれ 1, 2, \ldots, N と番号が付けられています。果樹 i の高さは H_i です。

昨夜、大型の台風がこの地域を通過しました。台風の強風により、高さが T 以下の果樹はまだ根が十分に張っておらず、すべて倒れてしまいました。すなわち、果樹 iH_i \leq T を満たすとき、またそのときに限り倒れています。

高橋君は果樹園を復旧するため、倒れた果樹をすべて植え直すことにしました。果樹 i を植え直して元の状態に戻すには C_i のコストがかかります。

倒れたすべての果樹を元の状態に戻すために必要な総コストを求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq T \leq 10^9
  • 1 \leq H_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq C_i \leq 10^9 (1 \leq i \leq N)
  • 入力はすべて整数
  • 答えは 2 \times 10^{14} 以下であることが保証される

入力

入力は以下の形式で標準入力から与えられる。

N T
H_1 H_2 \ldots H_N
C_1 C_2 \ldots C_N
  • 1 行目には、果樹の本数を表す整数 N と、果樹が倒れる高さの基準値を表す整数 T が、スペース区切りで与えられる。
  • 2 行目には、各果樹の高さを表す整数 H_1, H_2, \ldots, H_N が、スペース区切りで与えられる。
  • 3 行目には、各果樹を植え直す際の復旧コストを表す整数 C_1, C_2, \ldots, C_N が、スペース区切りで与えられる。

出力

倒れた果樹(H_i \leq T を満たす果樹 i)をすべて元に戻すために必要な総コストを 1 行で出力せよ。倒れた果樹が 1 本もない場合は 0 を出力せよ。末尾に改行を含めること。


入力例 1

5 3
1 5 3 4 2
10 20 30 40 50

出力例 1

90

入力例 2

4 2
5 10 8 3
100 200 300 400

出力例 2

0

入力例 3

10 50
10 20 30 40 50 60 70 80 90 100
5 15 25 35 45 55 65 75 85 95

出力例 3

125

入力例 4

20 500
100 200 300 400 500 600 700 800 900 1000 150 250 350 450 550 650 750 850 950 50
10 20 30 40 50 60 70 80 90 100 15 25 35 45 55 65 75 85 95 5

出力例 4

275

入力例 5

1 1000000000
1000000000
999999999

出力例 5

999999999

Score : 200 pts

Problem Statement

Takahashi is the caretaker of an orchard. There are N fruit trees planted in this orchard, numbered 1, 2, \ldots, N. The height of fruit tree i is H_i.

Last night, a large typhoon passed through this area. Due to the typhoon's strong winds, fruit trees with height T or less had not yet developed sufficient roots and all fell over. That is, fruit tree i has fallen if and only if H_i \leq T.

To restore the orchard, Takahashi decided to replant all the fallen fruit trees. The cost to replant fruit tree i and restore it to its original state is C_i.

Find the total cost required to restore all fallen fruit trees to their original state.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq T \leq 10^9
  • 1 \leq H_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq C_i \leq 10^9 (1 \leq i \leq N)
  • All inputs are integers
  • It is guaranteed that the answer is at most 2 \times 10^{14}

Input

Input is given from standard input in the following format.

N T
H_1 H_2 \ldots H_N
C_1 C_2 \ldots C_N
  • The first line contains an integer N representing the number of fruit trees and an integer T representing the height threshold for trees falling, separated by a space.
  • The second line contains integers H_1, H_2, \ldots, H_N representing the height of each fruit tree, separated by spaces.
  • The third line contains integers C_1, C_2, \ldots, C_N representing the restoration cost for replanting each fruit tree, separated by spaces.

Output

Output in a single line the total cost required to restore all fallen fruit trees (fruit trees i satisfying H_i \leq T). If no fruit trees have fallen, output 0. Include a newline at the end.


Sample Input 1

5 3
1 5 3 4 2
10 20 30 40 50

Sample Output 1

90

Sample Input 2

4 2
5 10 8 3
100 200 300 400

Sample Output 2

0

Sample Input 3

10 50
10 20 30 40 50 60 70 80 90 100
5 15 25 35 45 55 65 75 85 95

Sample Output 3

125

Sample Input 4

20 500
100 200 300 400 500 600 700 800 900 1000 150 250 350 450 550 650 750 850 950 50
10 20 30 40 50 60 70 80 90 100 15 25 35 45 55 65 75 85 95 5

Sample Output 4

275

Sample Input 5

1 1000000000
1000000000
999999999

Sample Output 5

999999999
B - Factory Quality Inspection

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 233

問題文

高橋君は工場の品質管理を担当しています。今日は N 個の製品がベルトコンベアで流れてきます。

それぞれの製品には「内部温度」が測定されており、i 番目の製品の内部温度は W_i です。内部温度が閾値 T 以上である製品は 不良品 です。不良品をそのまま出荷すると、1 個あたり損失 D が発生します。内部温度が T 未満の製品は不良品ではないため、出荷しても損失は発生しません。

高橋君はベルトコンベア上で、任意の個数の製品を選んで「追加冷却処理」を施すことができます。各製品に対して追加冷却処理を施すのは高々 1 回で、1 個あたりコスト C がかかります。追加冷却処理を施された製品は、不良品であった場合は不良品でなくなり、出荷しても損失が発生しなくなります。不良品でない製品に追加冷却処理を施すこともできますが、その製品にはもともと損失が発生しないため、コスト C だけが無駄にかかります。

すべての製品は最終的に出荷されます。高橋君は、追加冷却処理にかかるコストの総額と、追加冷却処理を施さなかった不良品を出荷したことによる損失の総額の を最小化したいです。どの製品に追加冷却処理を施すかを自由に選べるとき、この和の最小値を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq T \leq 10^9
  • 1 \leq C \leq 10^9
  • 1 \leq D \leq 10^9
  • 1 \leq W_i \leq 10^9 (1 \leq i \leq N)
  • 入力はすべて整数である。

入力

N T C D
W_1 W_2 \ldots W_N
  • 1 行目には、製品の個数を表す N 、不良品となる閾値を表す T 、追加冷却処理 1 個あたりのコストを表す C 、不良品 1 個あたりの損失を表す D が、スペース区切りで与えられる。
  • 2 行目には、各製品の内部温度を表す W_1, W_2, \ldots, W_N がスペース区切りで与えられる。

出力

コストの総額と損失の総額の和の最小値を整数で 1 行に出力せよ。


入力例 1

5 30 10 25
15 30 25 40 35

出力例 1

30

入力例 2

7 50 100 40
60 30 55 45 80 50 10

出力例 2

160

入力例 3

10 1000000000 500000000 300000000
999999999 1000000000 500000000 1000000000 999999998 1000000000 200000000 1000000000 999999999 1000000000

出力例 3

1500000000

Score : 233 pts

Problem Statement

Takahashi is in charge of quality control at a factory. Today, N products are coming along a conveyor belt.

Each product has a measured "internal temperature", and the internal temperature of the i-th product is W_i. A product whose internal temperature is at least the threshold T is a defective product. If a defective product is shipped as-is, a loss of D is incurred per product. Products with internal temperature less than T are not defective, so no loss is incurred when they are shipped.

Takahashi can select any number of products on the conveyor belt and apply "additional cooling treatment" to them. Each product can receive additional cooling treatment at most once, at a cost of C per product. If a defective product receives additional cooling treatment, it is no longer defective and can be shipped without incurring any loss. Additional cooling treatment can also be applied to non-defective products, but since those products would not have incurred any loss anyway, only the cost C is wasted.

All products will ultimately be shipped. Takahashi wants to minimize the sum of the total cost of additional cooling treatments and the total loss from shipping defective products that did not receive additional cooling treatment. Given that he can freely choose which products to apply additional cooling treatment to, find the minimum value of this sum.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq T \leq 10^9
  • 1 \leq C \leq 10^9
  • 1 \leq D \leq 10^9
  • 1 \leq W_i \leq 10^9 (1 \leq i \leq N)
  • All inputs are integers.

Input

N T C D
W_1 W_2 \ldots W_N
  • The first line contains N representing the number of products, T representing the threshold for defective products, C representing the cost per additional cooling treatment, and D representing the loss per defective product, separated by spaces.
  • The second line contains W_1, W_2, \ldots, W_N representing the internal temperatures of the products, separated by spaces.

Output

Output the minimum value of the sum of total cost and total loss as an integer on a single line.


Sample Input 1

5 30 10 25
15 30 25 40 35

Sample Output 1

30

Sample Input 2

7 50 100 40
60 30 55 45 80 50 10

Sample Output 2

160

Sample Input 3

10 1000000000 500000000 300000000
999999999 1000000000 500000000 1000000000 999999998 1000000000 200000000 1000000000 999999999 1000000000

Sample Output 3

1500000000
C - Team Formation

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

高橋君は、プログラミングコンテストに出場するチームを編成する担当者です。

高橋君の所属するサークルには N 人のメンバーがおり、各メンバーは 1 から N までの番号で識別されています。メンバー i のコンテスト出場経験の有無は値 H_i で表され、H_i = 1 のとき経験者、H_i = 0 のとき初心者です。また、メンバー i のプログラミングスキルは値 P_i で表されます。

高橋君は、コンテストに出場するために N 人の中から K 人のメンバーを重複なく選んでチームを編成したいと考えています。ただし、コンテストのルールにより、選ばれた K 人の中に経験者がちょうど M 人、初心者がちょうど K - M 人含まれている必要があります。

チームの総合力は、選ばれた K 人のメンバーのスキル値 P_i の合計で評価されます。

条件を満たすようにメンバーを選んだとき、チームの総合力の最大値を求めてください。条件を満たす選び方が存在しない場合は -1 を出力してください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq N
  • 0 \leq M \leq K
  • H_i \in \{0, 1\} (1 \leq i \leq N)
  • 1 \leq P_i \leq 10^9 (1 \leq i \leq N)
  • 入力はすべて整数

入力

N K M
H_1 P_1
H_2 P_2
\vdots
H_N P_N
  • 1 行目には、メンバーの総数 N、選ぶメンバーの人数 K、必要な経験者の人数 M がスペース区切りで与えられる。
  • 続く N 行のうち i 行目 (1 \leq i \leq N) には、メンバー i の経験の有無を表す H_i とスキル値 P_i がスペース区切りで与えられる。

出力

条件を満たすようにメンバーを選んだときのチームの総合力(スキル値の合計)の最大値を 1 行で出力せよ。条件を満たす選び方が存在しない場合は -1 を出力せよ。


入力例 1

5 3 1
1 100
0 80
0 90
1 50
0 70

出力例 1

270

入力例 2

6 4 2
1 500
0 300
1 400
0 600
1 200
0 350

出力例 2

1850

入力例 3

10 5 3
1 1000000000
0 999999999
1 800000000
0 750000000
1 600000000
0 500000000
1 400000000
0 300000000
0 200000000
0 100000000

出力例 3

4149999999

Score : 300 pts

Problem Statement

Takahashi is in charge of forming a team to participate in a programming contest.

The club Takahashi belongs to has N members, and each member is identified by a number from 1 to N. Whether member i has contest experience is represented by the value H_i: H_i = 1 means experienced, and H_i = 0 means beginner. Additionally, member i's programming skill is represented by the value P_i.

Takahashi wants to select K members from the N members (without duplicates) to form a team for the contest. However, due to the contest rules, exactly M of the selected K members must be experienced, and exactly K - M must be beginners.

The team's overall strength is evaluated as the sum of the skill values P_i of the selected K members.

Find the maximum possible overall strength of the team when members are selected to satisfy the conditions. If no valid selection exists, output -1.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq N
  • 0 \leq M \leq K
  • H_i \in \{0, 1\} (1 \leq i \leq N)
  • 1 \leq P_i \leq 10^9 (1 \leq i \leq N)
  • All inputs are integers

Input

N K M
H_1 P_1
H_2 P_2
\vdots
H_N P_N
  • The first line contains the total number of members N, the number of members to select K, and the required number of experienced members M, separated by spaces.
  • The following N lines, where the i-th line (1 \leq i \leq N) contains H_i representing whether member i is experienced and their skill value P_i, separated by a space.

Output

Output in one line the maximum overall team strength (sum of skill values) when members are selected to satisfy the conditions. If no valid selection exists, output -1.


Sample Input 1

5 3 1
1 100
0 80
0 90
1 50
0 70

Sample Output 1

270

Sample Input 2

6 4 2
1 500
0 300
1 400
0 600
1 200
0 350

Sample Output 2

1850

Sample Input 3

10 5 3
1 1000000000
0 999999999
1 800000000
0 750000000
1 600000000
0 500000000
1 400000000
0 300000000
0 200000000
0 100000000

Sample Output 3

4149999999
D - Escape from the Maze

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

高橋君は古い建物の中で迷路に閉じ込められてしまいました。

この建物は NM 列のグリッドで表されます。上から i 行目、左から j 列目のマスをマス (i, j) と呼びます。グリッドの i 行目の状態は文字列 S_i で表され、S_ij 文字目が # ならマス (i, j) は壁、. ならマス (i, j) は通路です。

高橋君は現在マス (1, 1) におり、右下のマス (N, M) にある出口を目指しています。

高橋君は、現在いるマスから上下左右に隣接するマス1つへ1回の操作で移動できます。ただし、グリッドの範囲外へ移動することはできません。移動先のマスが通路であれば、そのまま移動します。移動先のマスが壁であれば、ハンマーを使ってその壁を破壊して通路に変えたうえで移動します。壁を1つ破壊するたびに、破壊回数が 1 加算されます。一度破壊して通路に変わったマスは、以降も通路のままです。ハンマーの使用回数に上限はなく、また移動の回数にも上限はありません。

ハンマーで壁を何度でも破壊できるため、高橋君は必ずマス (N, M) に到達できます。高橋君がマス (1, 1) からマス (N, M) に到達するまでに破壊する壁の数の最小値を求めてください。

なお、マス (1, 1) とマス (N, M) は必ず通路であることが保証されています。

制約

  • 1 \leq N \leq 500
  • 1 \leq M \leq 500
  • N, M は整数
  • S_i (1 \leq i \leq N)#. からなる長さ M の文字列
  • S_11 文字目は .
  • S_NM 文字目は .

入力

N M
S_1
S_2
\vdots
S_N
  • 1 行目には、グリッドの行数 N と列数 M が、スペース区切りで与えられる。
  • 1 + i 行目 (1 \leq i \leq N) には、グリッドの i 行目の状態を表す長さ M の文字列 S_i が与えられる。S_ij 文字目がマス (i, j) の状態を表し、# は壁、. は通路を意味する。

出力

高橋君がマス (1, 1) からマス (N, M) に到達するために破壊する必要がある壁の数の最小値を 1 行で出力せよ。


入力例 1

3 5
...#.
.###.
...#.

出力例 1

1

入力例 2

3 3
...
...
...

出力例 2

0

入力例 3

5 7
..#.#.#
.#...#.
##.#.##
.#...#.
..#.#..

出力例 3

2

入力例 4

10 10
..........
.########.
..........
.########.
..........
.########.
..........
.########.
..........
..........

出力例 4

0

入力例 5

1 1
.

出力例 5

0

入力例 6

3 5
...#.
.###.
...#.

出力例 6

1

入力例 7

3 3
...
...
...

出力例 7

0

入力例 8

5 7
.#.#.#.
.#.#.#.
.#.#.#.
.#.#.#.
.......

出力例 8

0

入力例 9

10 10
..........
.########.
..........
########.#
..........
.########.
..........
########.#
..........
..........

出力例 9

0

入力例 10

1 1
.

出力例 10

0

Score : 400 pts

Problem Statement

Takahashi is trapped in a maze inside an old building.

The building is represented as a grid with N rows and M columns. The cell at the i-th row from the top and the j-th column from the left is called cell (i, j). The state of the i-th row of the grid is represented by the string S_i: if the j-th character of S_i is #, then cell (i, j) is a wall; if it is ., then cell (i, j) is a passage.

Takahashi is currently at cell (1, 1) and aims to reach the exit at cell (N, M) in the bottom-right corner.

In one operation, Takahashi can move from his current cell to one of the four adjacent cells (up, down, left, or right). However, he cannot move outside the grid. If the destination cell is a passage, he simply moves there. If the destination cell is a wall, he uses his hammer to destroy the wall, turning it into a passage, and then moves there. Each time a wall is destroyed, the destruction count increases by 1. A cell that has been destroyed and turned into a passage remains a passage thereafter. There is no limit on the number of times the hammer can be used, and there is no limit on the number of moves.

Since Takahashi can destroy walls as many times as needed, he can always reach cell (N, M). Find the minimum number of walls Takahashi needs to destroy to travel from cell (1, 1) to cell (N, M).

It is guaranteed that cell (1, 1) and cell (N, M) are always passages.

Constraints

  • 1 \leq N \leq 500
  • 1 \leq M \leq 500
  • N, M are integers
  • S_i (1 \leq i \leq N) is a string of length M consisting of # and .
  • The 1st character of S_1 is .
  • The M-th character of S_N is .

Input

N M
S_1
S_2
\vdots
S_N
  • The first line contains the number of rows N and the number of columns M of the grid, separated by a space.
  • The (1 + i)-th line (1 \leq i \leq N) contains a string S_i of length M representing the state of the i-th row of the grid. The j-th character of S_i represents the state of cell (i, j), where # means a wall and . means a passage.

Output

Print in one line the minimum number of walls Takahashi needs to destroy to travel from cell (1, 1) to cell (N, M).


Sample Input 1

3 5
...#.
.###.
...#.

Sample Output 1

1

Sample Input 2

3 3
...
...
...

Sample Output 2

0

Sample Input 3

5 7
..#.#.#
.#...#.
##.#.##
.#...#.
..#.#..

Sample Output 3

2

Sample Input 4

10 10
..........
.########.
..........
.########.
..........
.########.
..........
.########.
..........
..........

Sample Output 4

0

Sample Input 5

1 1
.

Sample Output 5

0

Sample Input 6

3 5
...#.
.###.
...#.

Sample Output 6

1

Sample Input 7

3 3
...
...
...

Sample Output 7

0

Sample Input 8

5 7
.#.#.#.
.#.#.#.
.#.#.#.
.#.#.#.
.......

Sample Output 8

0

Sample Input 9

10 10
..........
.########.
..........
########.#
..........
.########.
..........
########.#
..........
..........

Sample Output 9

0

Sample Input 10

1 1
.

Sample Output 10

0
E - Stone Crossing Game

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 466

問題文

高橋君は川を渡るための石渡りゲームに挑戦しています。

川には N 個の石が一列に並んでおり、左から順に石 1、石 2、…、石 N と番号が付けられています。石 i1 \leq i \leq N)の上には A_i 枚のコインが置かれています。

高橋君は最初、石 1 の上に立っています。高橋君は石の上に立ったとき、その石の上にあるコインをすべて獲得します(最初に立っている石 1 のコインも獲得します)。立ち寄らなかった石のコインは獲得できません。コインの枚数は正とは限らず、負であることもあり、その場合も強制的に獲得します。

高橋君はまだ石 N に到達していない間、現在いる石から右方向に跳んで移動します。現在石 ii < N)にいるとき、1 以上 K 以下の整数 d を選んで、石 i+d に移動することができます。ただし、移動先の石の番号は N 以下でなければなりません。すなわち、石 i+1, 石 i+2, \ldots, 石 \min(i+K,\, N) のいずれかに跳ぶことができます。移動は常に右方向であるため、同じ石に二度立つことはありません。

高橋君は必ず石 N に到達しなければなりません。石 N に到達し、そのコインを獲得した時点でゲームは終了します。制約より K \geq 1 であるため、石 1 から 1 歩ずつ進むことで必ず石 N に到達できます。

1 から出発し石 N に到達するまでに獲得できるコインの枚数の合計の最大値を求めてください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq N - 1
  • -10^9 \leq A_i \leq 10^91 \leq i \leq N
  • 入力はすべて整数

入力

N K
A_1 A_2 \ldots A_N
  • 1 行目には、石の個数を表す整数 N と、一度のジャンプで進める石の番号の差の最大値を表す整数 K が、スペース区切りで与えられる。
  • 2 行目には、各石の上に置かれているコインの枚数を表す整数 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。

出力

1 から石 N まで移動するときに獲得できるコインの枚数の合計の最大値を 1 行で出力せよ。

Score : 466 pts

Problem Statement

Takahashi is attempting a stone jumping game to cross a river.

There are N stones lined up in a row in the river, numbered stone 1, stone 2, …, stone N from left to right. On stone i (1 \leq i \leq N), there are A_i coins placed.

Takahashi initially stands on stone 1. When Takahashi stands on a stone, he collects all the coins on that stone (including the coins on stone 1 where he starts). Coins on stones he does not visit cannot be collected. The number of coins is not necessarily positive; it can be negative, in which case he is forced to collect them as well.

While Takahashi has not yet reached stone N, he jumps to the right from his current stone. When he is currently on stone i (i < N), he can choose an integer d with 1 \leq d \leq K and move to stone i+d. However, the destination stone's number must be at most N. That is, he can jump to any one of stone i+1, stone i+2, \ldots, stone \min(i+K,\, N). Since movement is always to the right, he never stands on the same stone twice.

Takahashi must reach stone N. The game ends when he reaches stone N and collects its coins. Since K \geq 1 by the constraints, he can always reach stone N by advancing one step at a time from stone 1.

Find the maximum total number of coins Takahashi can collect starting from stone 1 and reaching stone N.

Constraints

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

Input

N K
A_1 A_2 \ldots A_N
  • The first line contains an integer N representing the number of stones and an integer K representing the maximum difference in stone numbers that can be covered in a single jump, separated by a space.
  • The second line contains integers A_1, A_2, \ldots, A_N representing the number of coins placed on each stone, separated by spaces.

Output

Print in one line the maximum total number of coins that can be collected when moving from stone 1 to stone N.