A - 経費の申請

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

配点 : 200 点

問題文

高橋君は今月、 N 件の経費を申請します。 i 件目の経費は A_i 円です。

会社の規則で、 1 か月に申請できる経費の合計は B 円までと決まっています。 N 件すべてを申請できるなら Yes を、合計が上限を超えてしまうなら No を出力してください。

制約

  • 1 \leq N \leq 100
  • 1 \leq A_i \leq 10^9
  • 1 \leq B \leq 10^{11}
  • 入力はすべて整数

入力

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

N B
A_1 A_2 \ldots A_N
  • 1 行目には、経費の件数 N と申請できる合計の上限 B がスペース区切りで与えられる。
  • 2 行目には、各経費の金額 A_1, A_2, \ldots, A_N がスペース区切りで与えられる。

出力

すべて申請できるなら Yes を、そうでないなら No を 1 行で出力せよ。


入力例 1

3 10000
3000 2500 4000

出力例 1

Yes

入力例 2

2 5000
3000 2500

出力例 2

No

入力例 3

1 100
1

出力例 3

Yes

入力例 4

1 1
1000000000

出力例 4

No

入力例 5

5 1000000000
1000000000 2 1 1 1

出力例 5

No

Score : 200 pts

Problem Statement

Takahashi will submit N expense claims this month. The i-th claim is for A_i yen.

Under company rules, the total amount of expenses that can be submitted in one month is at most B yen. Print Yes if all N claims can be submitted, and No if their total exceeds the limit.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq A_i \leq 10^9
  • 1 \leq B \leq 10^{11}
  • All input values are integers.

Input

The input is given from standard input in the following format.

N B
A_1 A_2 \ldots A_N
  • The first line contains the number of claims N and the maximum total amount B, separated by a space.
  • The second line contains the claim amounts A_1, A_2, \ldots, A_N, separated by spaces.

Output

Print Yes if all claims can be submitted, and No otherwise, on a single line.


Sample Input 1

3 10000
3000 2500 4000

Sample Output 1

Yes

Sample Input 2

2 5000
3000 2500

Sample Output 2

No

Sample Input 3

1 100
1

Sample Output 3

Yes

Sample Input 4

1 1
1000000000

Sample Output 4

No

Sample Input 5

5 1000000000
1000000000 2 1 1 1

Sample Output 5

No
B - 海図の航路

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

配点 : 233 点

問題文

高橋君は縦 H マス、横 W マスの海図を持っています。海図の様子は H 個の文字列 S_1, S_2, \ldots, S_H で表され、 S_i の j 文字目は上から i 行目、左から j 列目のマスの様子です。 . は海を、 # は島を表します。

高橋君の船は、ある列を選んで、その列を上から下までまっすぐ進みます。選んだ列のすべてのマスが海であるとき、その列は「航路にできる」といいます。

航路にできる列の数を求めてください。

制約

  • 1 \leq H \leq 10
  • 1 \leq W \leq 10
  • S_i は . と # のみからなる長さ W の文字列

入力

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

H W
S_1
S_2
\vdots
S_H
  • 1 行目には、海図の縦のマス数 H と横のマス数 W がスペース区切りで与えられる。
  • 2 行目から H 行にわたり、 1 + i 行目には上から i 行目の様子を表す文字列 S_i が与えられる。

出力

航路にできる列の数を 1 行で出力せよ。


入力例 1

3 4
.#..
...#
.#..

出力例 1

2

入力例 2

1 1
#

出力例 2

0

入力例 3

2 3
...
...

出力例 3

3

入力例 4

2 8
#....#..
#..##...

出力例 4

4

入力例 5

2 8
...#.#..
...#..#.

出力例 5

5

Score : 233 pts

Problem Statement

Takahashi has a sea chart with H rows and W columns of cells. The chart is described by H strings S_1, S_2, \ldots, S_H, where the j-th character of S_i represents the cell in the i-th row from the top and the j-th column from the left. . denotes sea and # denotes an island.

Takahashi's ship chooses one column and travels straight down that column from top to bottom. A column is said to be "usable as a route" if every cell in the chosen column is sea.

Find the number of columns that can be used as a route.

Constraints

  • 1 \leq H \leq 10
  • 1 \leq W \leq 10
  • S_i is a string of length W consisting only of . and #.

Input

The input is given from standard input in the following format:

H W
S_1
S_2
\vdots
S_H
  • The first line contains the number of rows H and the number of columns W of the sea chart, separated by a space.
  • The following H lines describe the chart: the (1 + i)-th line contains the string S_i representing the i-th row from the top.

Output

Print the number of columns that can be used as a route on a single line.


Sample Input 1

3 4
.#..
...#
.#..

Sample Output 1

2

Sample Input 2

1 1
#

Sample Output 2

0

Sample Input 3

2 3
...
...

Sample Output 3

3

Sample Input 4

2 8
#....#..
#..##...

Sample Output 4

4

Sample Input 5

2 8
...#.#..
...#..#.

Sample Output 5

5
C - 荷物の搬入

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

配点 : 333 点

問題文

高橋君は倉庫からビルの屋上まで N 個の荷物を運ぶ仕事を請け負いました。

倉庫から屋上までは一本の通路でつながっており、通路の途中には M 箇所の踊り場があります。踊り場 i(1 \leq i \leq M)は倉庫からの距離が D_i の位置にあり、屋上は倉庫からの距離が L の位置にあります。

高橋君は一度に最大 K 個の荷物をまとめて運ぶことができます。荷物を運ぶ際、倉庫から直接屋上まで運ぶこともできますし、途中の踊り場に荷物を一時的に置いておくこともできます。踊り場に置いた荷物は、後から再び持ち上げて運ぶことができます。

高橋君は常に倉庫またはいずれかの踊り場から荷物を持ち上げ、別の踊り場または屋上まで運びます。高橋君が移動する距離 1 あたりのコストは、そのとき運んでいる荷物の個数に等しいです(手ぶらで移動するときのコストは 0 です)。

すべての N 個の荷物を倉庫から屋上まで運ぶために必要なコストの合計の最小値を求めてください。なお、最終的にすべての荷物が屋上に届けばよく、高橋君自身がどこにいるかは問いません。

制約

  • 1 \leq N \leq 10^9
  • 1 \leq K \leq 10^9
  • 0 \leq M \leq 2 \times 10^5
  • 1 \leq L \leq 10^9
  • M \leq L - 1
  • 1 \leq D_i \leq L - 1(1 \leq i \leq M)
  • D_i はすべて異なる
  • 入力はすべて整数

入力

N K M L
D_1 D_2 \ldots D_M
  • 1 行目には、荷物の個数を表す N、一度に運べる最大個数を表す K、踊り場の数を表す M、屋上までの距離を表す L が、スペース区切りで与えられる。
  • 2 行目には、各踊り場の倉庫からの距離を表す D_1, D_2, \ldots, D_M が、スペース区切りで与えられる。ただし M = 0 の場合、2 行目は与えられない。

出力

すべての荷物を倉庫から屋上まで運ぶために必要なコストの合計の最小値を 1 行で出力せよ。


入力例 1

3 2 2 10
4 7

出力例 1

30

入力例 2

5 3 0 8

出力例 2

40

入力例 3

12345 67 8 1000
50 120 250 400 600 750 900 990

出力例 3

12345000

入力例 4

987654321 123456789 20 1000000000
1 1000 50000 1234567 5000000 12345678 50000000 100000000 200000000 300000000 400000000 500000000 600000000 700000000 800000000 900000000 950000000 990000000 999000000 999999999

出力例 4

987654321000000000

入力例 5

1 1 0 1

出力例 5

1

Score : 333 pts

Problem Statement

Takahashi has taken a job to carry N goods from a warehouse to the rooftop of a building.

The warehouse and the rooftop are connected by a single pathway, and there are M landings along the pathway. Landing i (1 \leq i \leq M) is located at a distance of D_i from the warehouse, and the rooftop is located at a distance of L from the warehouse.

Takahashi can carry up to K goods at a time. When carrying goods, he can carry them directly from the warehouse to the rooftop, or temporarily leave them at a landing along the way. Goods left at a landing can be picked up and carried again later.

Takahashi always picks up goods from either the warehouse or one of the landings, and carries them to another landing or the rooftop. The cost per unit distance Takahashi travels is equal to the number of goods he is carrying at that moment (the cost when moving empty-handed is 0).

Find the minimum total cost required to carry all N goods from the warehouse to the rooftop. Note that it is only required that all goods eventually reach the rooftop, and Takahashi's final location does not matter.

Constraints

  • 1 \leq N \leq 10^9
  • 1 \leq K \leq 10^9
  • 0 \leq M \leq 2 \times 10^5
  • 1 \leq L \leq 10^9
  • M \leq L - 1
  • 1 \leq D_i \leq L - 1 (1 \leq i \leq M)
  • D_i are all distinct.
  • All input values are integers.

Input

N K M L
D_1 D_2 \ldots D_M
  • The first line contains N, the number of goods; K, the maximum number of goods that can be carried at once; M, the number of landings; and L, the distance to the rooftop, separated by spaces.
  • The second line contains D_1, D_2, \ldots, D_M, representing the distance of each landing from the warehouse, separated by spaces. If M = 0, the second line is not provided.

Output

Print the minimum total cost required to carry all goods from the warehouse to the rooftop in a single line.


Sample Input 1

3 2 2 10
4 7

Sample Output 1

30

Sample Input 2

5 3 0 8

Sample Output 2

40

Sample Input 3

12345 67 8 1000
50 120 250 400 600 750 900 990

Sample Output 3

12345000

Sample Input 4

987654321 123456789 20 1000000000
1 1000 50000 1234567 5000000 12345678 50000000 100000000 200000000 300000000 400000000 500000000 600000000 700000000 800000000 900000000 950000000 990000000 999000000 999999999

Sample Output 4

987654321000000000

Sample Input 5

1 1 0 1

Sample Output 5

1
D - モンスター討伐

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

配点 : 366 点

問題文

高橋君はゲームの中で M 匹のモンスターと戦っています。

モンスターには 1 から M までの番号が付けられており、番号順に一列に並んでいます。モンスター j (1 \leq j \leq M) の初期の体力は H_j です。

高橋君は N 回の範囲攻撃を行いました。

i 回目 (1 \leq i \leq N) の攻撃では、モンスター L_i からモンスター R_i までのすべてのモンスターにそれぞれ A_i のダメージを与えます。

すべての攻撃が終わった時点で、受けたダメージの合計が初期の体力 H_j 以上になったモンスターは倒されます。

なお、途中の攻撃で倒されたモンスターも列から取り除かれず、その後の攻撃の対象に含まれます。

すべての攻撃が終わった時点で、倒されたモンスターの匹数を求めてください。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq M \leq 10^6
  • 1 \leq H_j \leq 10^9 (1 \leq j \leq M)
  • 1 \leq L_i \leq R_i \leq M (1 \leq i \leq N)
  • 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
  • 入力はすべて整数

入力

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

N M
H_1 H_2 \dots H_M
L_1 R_1 A_1
L_2 R_2 A_2
\vdots
L_N R_N A_N
  • 1 行目には、攻撃の回数を表す N と、モンスターの数を表す M がスペース区切りで与えられる。
  • 2 行目には、各モンスターの初期の体力を表す H_1, H_2, \dots, H_M がスペース区切りで与えられる。
  • 続く N 行のうち i 行目(全体で 2+i 行目)には、i 回目の攻撃の範囲を表す L_i, R_i と、与えるダメージを表す A_i がスペース区切りで与えられる。

出力

すべての攻撃が終わった時点で倒されたモンスターの匹数を 1 行に出力してください。


入力例 1

3 5
10 5 8 12 3
1 3 4
2 4 5
3 5 3

出力例 1

3

入力例 2

2 4
100 200 150 120
1 3 10
2 4 20

出力例 2

0

入力例 3

6 10
15 20 8 30 12 5 25 18 10 22
1 5 10
3 8 7
2 4 5
6 10 12
1 10 3
5 7 8

出力例 3

6

入力例 4

8 15
1000000000 500000000 800000000 300000000 900000000 150000000 600000000 450000000 700000000 200000000 850000000 400000000 950000000 350000000 650000000
1 15 200000000
2 8 300000000
4 12 250000000
1 5 150000000
7 14 180000000
10 15 400000000
3 10 120000000
1 1 500000000

出力例 4

11

入力例 5

1 1
1000000000
1 1 1000000000

出力例 5

1

Score : 366 pts

Problem Statement

Takahashi is fighting M monsters in a game.

The monsters are numbered 1 through M and are lined up in a row in increasing order of their numbers. The initial health of monster j (1 \leq j \leq M) is H_j.

Takahashi performed N range attacks.

In the i-th attack (1 \leq i \leq N), he dealt A_i damage to each monster from monster L_i through monster R_i.

After all attacks are completed, any monster whose total damage received is greater than or equal to its initial health H_j is defeated.

Note that even if a monster is defeated partway through, it is not removed from the line and can still be targeted by subsequent attacks.

Find the number of monsters that are defeated after all attacks are completed.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq M \leq 10^6
  • 1 \leq H_j \leq 10^9 (1 \leq j \leq M)
  • 1 \leq L_i \leq R_i \leq M (1 \leq i \leq N)
  • 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N M
H_1 H_2 \dots H_M
L_1 R_1 A_1
L_2 R_2 A_2
\vdots
L_N R_N A_N
  • The first line contains N, representing the number of attacks, and M, representing the number of monsters, separated by a space.
  • The second line contains H_1, H_2, \dots, H_M, representing the initial health of each monster, separated by spaces.
  • The i-th of the following N lines (the (2+i)-th line in total) contains L_i, R_i, representing the range of the i-th attack, and A_i, representing the damage dealt, separated by spaces.

Output

Print the number of monsters defeated after all attacks are completed in a single line.


Sample Input 1

3 5
10 5 8 12 3
1 3 4
2 4 5
3 5 3

Sample Output 1

3

Sample Input 2

2 4
100 200 150 120
1 3 10
2 4 20

Sample Output 2

0

Sample Input 3

6 10
15 20 8 30 12 5 25 18 10 22
1 5 10
3 8 7
2 4 5
6 10 12
1 10 3
5 7 8

Sample Output 3

6

Sample Input 4

8 15
1000000000 500000000 800000000 300000000 900000000 150000000 600000000 450000000 700000000 200000000 850000000 400000000 950000000 350000000 650000000
1 15 200000000
2 8 300000000
4 12 250000000
1 5 150000000
7 14 180000000
10 15 400000000
3 10 120000000
1 1 500000000

Sample Output 4

11

Sample Input 5

1 1
1000000000
1 1 1000000000

Sample Output 5

1
E - 粘土細工コンテスト

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

配点 : 433 点

問題文

高橋君は粘土細工のコンテストに参加しています。テーブルの上には N 個の粘土が一列に並んでおり、左から順に 1 から N までの番号が付けられています。各粘土 i の重さは A_i です。

高橋君は以下の操作を、粘土が 1 つになるまで繰り返します。すなわち、操作はちょうど N - 1 回行われます。

  1. 現在一列に並んでいる粘土の中から、隣り合う 2 つの粘土を 1 組選ぶ。選んだ 2 つの粘土を取り除き、それらの重さの和を重さとする 1 つの新しい粘土を作る。新しい粘土は、取り除いた 2 つの粘土がもともと並んでいた位置に挿入される。すなわち、列の中で取り除いた 2 つの粘土が 1 つの粘土に置き換わり、他の粘土の相対的な並び順は変わらない。これにより、列の粘土の個数が 1 つ減る。
  2. くっつけた直後、高橋君はその新しい粘土に「特殊なふくらし粉」を振りかけるかどうかを選べる。ふくらし粉を振りかけると、その粘土の重さが 2 倍になる。1 回の操作で振りかけられるふくらし粉は高々 1 回分である。ふくらし粉を使える回数は、N - 1 回の操作全体を通じて合計 K 回までである(操作回数は N - 1 回なので、K \geq N - 1 の場合は毎回の操作で使うことができる)。なお、過去の操作でふくらし粉を振りかけた粘土を含む合体であっても、改めてふくらし粉を振りかけることができる。

高橋君は、最終的に残った 1 つの粘土の重さを最大化したいと考えています。最適に操作を行ったときの、最終的な粘土の重さの最大値を求めてください。

制約

  • 2 \leq N \leq 50
  • 0 \leq K \leq 50
  • 1 \leq A_i \leq 10^9
  • 入力はすべて整数である

入力

N K
A_1 A_2 \ldots A_N
  • 1 行目には、粘土の個数を表す整数 N と、ふくらし粉を使える回数の上限を表す整数 K が、スペース区切りで与えられる。
  • 2 行目には、各粘土の重さを表す整数 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。

出力

最終的に残った 1 つの粘土の重さの最大値を整数として 1 行で出力せよ。なお、答えは 64 ビット整数の範囲に収まらないことがあるため、多倍長整数などを用いて正確に出力すること。


入力例 1

3 1
1 2 3

出力例 1

12

入力例 2

4 0
5 1 4 2

出力例 2

12

入力例 3

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

出力例 3

388

入力例 4

20 25
1000000000 123456789 987654321 555555555 1 999999999 314159265 271828182 42 424242424 777777777 888888888 222222222 333333333 444444444 666666666 135792468 246813579 99999999 500000000

出力例 4

1064579502196308

入力例 5

2 50
1000000000 1000000000

出力例 5

4000000000

Score : 433 pts

Problem Statement

Takahashi is participating in a clay sculpting contest. On the table, N pieces of clay are lined up in a row, numbered 1 through N from left to right. The weight of each piece of clay i is A_i.

Takahashi repeats the following operation until only one piece of clay remains. That is, the operation is performed exactly N - 1 times.

  1. From the pieces of clay currently lined up in a row, choose one pair of adjacent pieces of clay. Remove the two chosen pieces and create one new piece of clay whose weight is the sum of their weights. The new piece of clay is inserted at the position where the two removed pieces were originally located. In other words, the two removed pieces in the row are replaced by one piece of clay, and the relative order of the other pieces remains unchanged. This reduces the number of pieces of clay in the row by one.
  2. Immediately after combining, Takahashi can choose whether or not to sprinkle "special baking powder" on the new piece of clay. If baking powder is sprinkled, the weight of that piece of clay doubles. At most one application of baking powder can be used per operation. The total number of times baking powder can be used across all N - 1 operations is at most K (since there are N - 1 operations, if K \geq N - 1, it can be used in every operation). Note that even when combining clay that had baking powder sprinkled on it in a previous operation, baking powder can be sprinkled again.

Takahashi wants to maximize the weight of the final remaining piece of clay. Find the maximum possible weight of the final piece of clay when operations are performed optimally.

Constraints

  • 2 \leq N \leq 50
  • 0 \leq K \leq 50
  • 1 \leq A_i \leq 10^9
  • 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 pieces of clay and an integer K representing the maximum number of times baking powder can be used, separated by a space.
  • The second line contains integers A_1, A_2, \ldots, A_N representing the weight of each piece of clay, separated by spaces.

Output

Output the maximum weight of the final remaining piece of clay as an integer on one line. Note that the answer may not fit in a 64-bit integer, so use arbitrary-precision integers or similar to output the exact value.


Sample Input 1

3 1
1 2 3

Sample Output 1

12

Sample Input 2

4 0
5 1 4 2

Sample Output 2

12

Sample Input 3

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

Sample Output 3

388

Sample Input 4

20 25
1000000000 123456789 987654321 555555555 1 999999999 314159265 271828182 42 424242424 777777777 888888888 222222222 333333333 444444444 666666666 135792468 246813579 99999999 500000000

Sample Output 4

1064579502196308

Sample Input 5

2 50
1000000000 1000000000

Sample Output 5

4000000000