A - Chef's Break

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

高橋君は、レストランの厨房で働く料理人です。

今日、高橋君は N 個の食材を下ごしらえする必要があります。i 番目の食材を下ごしらえするのにかかる時間は T_i 秒です。高橋君は一度に 1 つの食材しか下ごしらえすることができません。

また、厨房のルールとして、高橋君は作業中にちょうど M 回の休憩を取らなければなりません。各休憩は R 秒かかります。休憩は任意の食材の下ごしらえを終えた直後にのみ取ることができます(最初の食材に取りかかる前や、最後の食材を終えた後には取れません)。

すべての食材を下ごしらえし、かつちょうど M 回の休憩を取るとき、作業開始から全作業完了までにかかる最小の合計時間を求めてください。なお、食材を下ごしらえする順番は自由に決めることができます。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq N - 1
  • 1 \leq R \leq 10^9
  • 1 \leq T_i \leq 10^9 (1 \leq i \leq N)
  • 入力はすべて整数である

入力

N M R
T_1 T_2 \ldots T_N
  • 1 行目には、食材の個数を表す N 、休憩の回数を表す M 、1 回の休憩にかかる時間を表す R が、スペース区切りで与えられる。
  • 2 行目には、各食材を下ごしらえするのにかかる時間 T_1, T_2, \ldots, T_N が、スペース区切りで与えられる。

出力

すべての食材を下ごしらえし、ちょうど M 回の休憩を取るときの、最小の合計時間を 1 行で出力せよ。


入力例 1

3 1 5
10 20 30

出力例 1

65

入力例 2

5 2 10
3 7 2 8 5

出力例 2

45

入力例 3

10 4 1000000000
100 200 300 400 500 600 700 800 900 1000

出力例 3

4000005500

Score : 200 pts

Problem Statement

Takahashi is a chef working in a restaurant kitchen.

Today, Takahashi needs to prepare N ingredients. The time required to prepare the i-th ingredient is T_i seconds. Takahashi can only prepare one ingredient at a time.

Additionally, according to the kitchen rules, Takahashi must take exactly M breaks during his work. Each break takes R seconds. A break can only be taken immediately after finishing the preparation of any ingredient (breaks cannot be taken before starting the first ingredient or after finishing the last ingredient).

Find the minimum total time from the start of work to the completion of all work, when preparing all ingredients and taking exactly M breaks. Note that the order in which ingredients are prepared can be freely chosen.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq N - 1
  • 1 \leq R \leq 10^9
  • 1 \leq T_i \leq 10^9 (1 \leq i \leq N)
  • All inputs are integers

Input

N M R
T_1 T_2 \ldots T_N
  • The first line contains N representing the number of ingredients, M representing the number of breaks, and R representing the time for one break, separated by spaces.
  • The second line contains the times T_1, T_2, \ldots, T_N required to prepare each ingredient, separated by spaces.

Output

Output in one line the minimum total time when preparing all ingredients and taking exactly M breaks.


Sample Input 1

3 1 5
10 20 30

Sample Output 1

65

Sample Input 2

5 2 10
3 7 2 8 5

Sample Output 2

45

Sample Input 3

10 4 1000000000
100 200 300 400 500 600 700 800 900 1000

Sample Output 3

4000005500
B - Bus Tour

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 266 点

問題文

高橋君は観光バスツアーの運営をしています。このツアーでは、バスが複数の停留所を順番に巡りながら乗客を乗せていきます。

ツアーには N 個の停留所があり、出発地点から順に停留所 1, 停留所 2, ..., 停留所 N と番号が付けられています。停留所 1 が最初の停留所で、停留所 N が最終目的地です。

バスは最初(停留所 1 で乗車が行われる前)には誰も乗っていません。

各停留所 i(1 \leq i \leq N)では、新たに A_i 人の乗客が乗車します。

さらに、各停留所 i(1 \leq i \leq N - 1)では、乗車の後、次の停留所 i + 1 への移動中に B_i 人の乗客が下車してツアーを離れます。ただし、そのときバスに乗っている人数が B_i 人未満の場合は、乗っている全員が下車します。

まとめると、各停留所 i で行われる処理は以下の通りです:

  1. A_i 人の乗客が新たに乗車する。
  2. i \leq N - 1 の場合、次の停留所への移動中に \min(\text{現在の乗客数}, B_i) 人が下車する。i = N の場合、下車は発生しない。

停留所 N での乗車が完了した時点で、バスに乗っている乗客の人数を求めてください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq A_i \leq 10^9(1 \leq i \leq N)
  • 0 \leq B_i \leq 10^9(1 \leq i \leq N - 1)
  • 入力はすべて整数

入力

N
A_1 B_1
A_2 B_2
\vdots
A_{N-1} B_{N-1}
A_N
  • 1 行目には、停留所の数を表す整数 N が与えられる。
  • i + 1 行目(1 \leq i \leq N - 1)には、停留所 i での乗車人数 A_i と下車人数 B_i がスペース区切りで与えられる。
  • N + 1 行目には、停留所 N での乗車人数 A_N のみが与えられる。

出力

停留所 N での乗車が完了した時点でバスに乗っている乗客の人数を 1 行で出力せよ。


入力例 1

3
5 2
3 1
4

出力例 1

9

入力例 2

3
2 10
1 5
3

出力例 2

3

入力例 3

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

出力例 3

13

入力例 4

10
1000000000 500000000
1000000000 200000000
500000000 1000000000
300000000 100000000
0 999999999
1000000000 1000000000
999999999 0
0 500000000
500000000 1000000000
123456789

出力例 4

123456789

入力例 5

2
0 0
0

出力例 5

0

Score : 266 pts

Problem Statement

Takahashi is operating a sightseeing bus tour. In this tour, a bus picks up passengers while visiting multiple stops in order.

The tour has N stops, numbered stop 1, stop 2, ..., stop N in order from the departure point. Stop 1 is the first stop, and stop N is the final destination.

The bus is initially empty (before any boarding takes place at stop 1).

At each stop i (1 \leq i \leq N), A_i new passengers board the bus.

Furthermore, at each stop i (1 \leq i \leq N - 1), after boarding, B_i passengers get off the bus and leave the tour during the trip to the next stop i + 1. However, if the number of passengers currently on the bus is less than B_i, all passengers on the bus get off.

In summary, the process at each stop i is as follows:

  1. A_i new passengers board the bus.
  2. If i \leq N - 1, during the trip to the next stop, \min(\text{current number of passengers}, B_i) passengers get off. If i = N, no passengers get off.

Find the number of passengers on the bus when boarding at stop N is completed.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq A_i \leq 10^9 (1 \leq i \leq N)
  • 0 \leq B_i \leq 10^9 (1 \leq i \leq N - 1)
  • All input values are integers.

Input

N
A_1 B_1
A_2 B_2
\vdots
A_{N-1} B_{N-1}
A_N
  • The first line contains an integer N, representing the number of stops.
  • The (i + 1)-th line (1 \leq i \leq N - 1) contains the number of passengers boarding at stop i, A_i, and the number of passengers getting off, B_i, separated by a space.
  • The (N + 1)-th line contains only A_N, the number of passengers boarding at stop N.

Output

Print on a single line the number of passengers on the bus when boarding at stop N is completed.


Sample Input 1

3
5 2
3 1
4

Sample Output 1

9

Sample Input 2

3
2 10
1 5
3

Sample Output 2

3

Sample Input 3

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

Sample Output 3

13

Sample Input 4

10
1000000000 500000000
1000000000 200000000
500000000 1000000000
300000000 100000000
0 999999999
1000000000 1000000000
999999999 0
0 500000000
500000000 1000000000
123456789

Sample Output 4

123456789

Sample Input 5

2
0 0
0

Sample Output 5

0
C - Factory Tour

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 333 点

問題文

高橋君は、大きな工場の見学ツアーのガイドとして働いています。

この工場には N 個の製造エリアがあり、それぞれのエリアには 1 から N までの番号が付けられています。各エリア i の見学にはちょうど T_i 分かかります。

今日は M 組の見学グループが工場を訪れます。各グループ j は、時刻 S_j 分に工場に到着し、到着後すぐにエリア番号 L_j から R_j までの範囲に含まれるすべてのエリアを見学します。グループは番号の小さいエリアから順に一つずつ見学し、一つのエリアの見学が終わると即座に次のエリアの見学を開始します(エリア間の移動時間はかかりません)。

各エリアは十分に広いため、複数のグループが同時に同じエリアを見学していても、互いに影響を受けることはありません。

高橋君は、各グループがすべての見学を終える時刻を事前に計算し、グループに伝えたいと考えています。

各グループについて、すべての指定されたエリアの見学が完了する時刻(分)を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 1 \leq T_i \leq 10^9
  • 0 \leq S_j \leq 10^9
  • 1 \leq L_j \leq R_j \leq N
  • 入力はすべて整数

入力

N M
T_1 T_2 \ldots T_N
S_1 L_1 R_1
S_2 L_2 R_2
\vdots
S_M L_M R_M
  • 1 行目には、エリアの個数 N とグループの数 M が、スペース区切りで与えられる。
  • 2 行目には、各エリアの見学時間 T_1, T_2, \ldots, T_N が、スペース区切りで与えられる。
  • 3 行目から M 行にわたり、各グループの情報が与えられる。
  • 2 + j 行目には、 j 番目のグループの到着時刻 S_j 、見学するエリア番号の範囲の始点 L_j 、終点 R_j が、スペース区切りで与えられる。

出力

M 行出力せよ。

j 行目には、 j 番目のグループがすべての見学を終える時刻を分単位で出力せよ。


入力例 1

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

出力例 1

60
95
150

入力例 2

4 5
15 25 10 30
0 1 4
10 1 1
20 2 3
0 3 4
50 1 2

出力例 2

80
25
55
40
90

入力例 3

10 6
100 200 300 150 250 50 400 100 350 200
0 1 10
1000 3 7
500 1 5
0 6 10
200 4 6
999999999 1 1

出力例 3

2100
2150
1500
1100
650
1000000099

Score : 333 pts

Problem Statement

Takahashi works as a guide for a tour of a large factory.

The factory has N manufacturing areas, each numbered from 1 to N. Visiting each area i takes exactly T_i minutes.

Today, M tour groups will visit the factory. Each group j arrives at the factory at time S_j minutes, and immediately after arrival, they visit all areas with numbers from L_j to R_j. The group visits the areas one by one in order of increasing area number, and as soon as they finish visiting one area, they immediately begin visiting the next area (there is no travel time between areas).

Each area is spacious enough that even if multiple groups are visiting the same area at the same time, they do not affect each other.

Takahashi wants to calculate in advance the time at which each group finishes all their visits, and inform the groups.

For each group, determine the time (in minutes) at which they complete visiting all of their designated areas.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 1 \leq T_i \leq 10^9
  • 0 \leq S_j \leq 10^9
  • 1 \leq L_j \leq R_j \leq N
  • All input values are integers.

Input

N M
T_1 T_2 \ldots T_N
S_1 L_1 R_1
S_2 L_2 R_2
\vdots
S_M L_M R_M
  • The first line contains the number of areas N and the number of groups M, separated by a space.
  • The second line contains the visiting times T_1, T_2, \ldots, T_N for each area, separated by spaces.
  • The following M lines contain the information for each group.
  • The (2 + j)-th line contains the arrival time S_j of the j-th group, and the start L_j and end R_j of the range of area numbers to visit, separated by spaces.

Output

Print M lines.

The j-th line should contain the time in minutes at which the j-th group finishes all their visits.


Sample Input 1

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

Sample Output 1

60
95
150

Sample Input 2

4 5
15 25 10 30
0 1 4
10 1 1
20 2 3
0 3 4
50 1 2

Sample Output 2

80
25
55
40
90

Sample Input 3

10 6
100 200 300 150 250 50 400 100 350 200
0 1 10
1000 3 7
500 1 5
0 6 10
200 4 6
999999999 1 1

Sample Output 3

2100
2150
1500
1100
650
1000000099
D - Package Delivery

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400 点

問題文

高橋君は配達員として働いています。今日は N 個の配達依頼が届いています。

i 番目 (1 \leq i \leq N) の依頼を受けると、報酬として P_i 円を受け取れますが、燃料代などの経費として C_i 円がかかります。したがって、i 番目の依頼を受けた場合の純利益は P_i - C_i 円です(純利益は負になることもあります)。また、i 番目の依頼の荷物の重量は W_i です。依頼を受けなければ、その依頼に関する報酬・経費は発生せず、荷物を運ぶ必要もありません。

高橋君は N 個の依頼の中から 0 個以上の依頼を選んで受けることにします。各依頼は受けるか受けないかのどちらかであり、同じ依頼を複数回受けることはできません。依頼を 1 つも選ばない場合、重量の合計・純利益の合計はともに 0 となります。

ただし、選んだ依頼の荷物はすべてトラックに積んで 1 回の配達で運ぶ必要があります。トラックの積載量の上限は S であるため、選んだ依頼の荷物の重量の合計が S 以下でなければなりません。

さらに、高橋君は今月の家賃を支払うために、選んだ依頼の純利益の合計が T 以上である必要があります。

以上をまとめると、選んだ依頼の集合を A(A \subseteq \{1, 2, \ldots, N\})として、次の 2 つの条件をともに満たす A の選び方を求めます。

  • 選んだ依頼の荷物の重量の合計が S 以下である。すなわち \displaystyle\sum_{i \in A} W_i \leq S
  • 選んだ依頼の純利益の合計が T 以上である。すなわち \displaystyle\sum_{i \in A} (P_i - C_i) \geq T

このような条件を満たす集合 A が存在するかどうかを判定してください。存在する場合は、条件を満たす A における要素数 |A| の最小値を出力してください。存在しない場合は -1 を出力してください。

制約

  • 1 \leq N \leq 50
  • 1 \leq S \leq 10^4
  • 1 \leq T \leq 10^4
  • 1 \leq P_i \leq 10^4 (1 \leq i \leq N)
  • 1 \leq C_i \leq 10^4 (1 \leq i \leq N)
  • 1 \leq W_i \leq 10^4 (1 \leq i \leq N)
  • 入力はすべて整数である

入力

N S T
P_1 C_1 W_1
P_2 C_2 W_2
\vdots
P_N C_N W_N

1 行目には、依頼の個数 N、トラックの積載量の上限 S、必要な純利益の合計の下限 T が、スペース区切りで与えられる。

続く N 行のうち i 行目 (1 \leq i \leq N) には、i 番目の依頼の報酬 P_i、経費 C_i、荷物の重量 W_i が、スペース区切りで与えられる。

出力

条件を満たす依頼の選び方が存在する場合は、選んだ依頼の個数の最小値を 1 行で出力してください。存在しない場合は -1 を 1 行で出力してください。


入力例 1

4 8 10
8 3 3
7 4 4
12 4 5
3 5 2

出力例 1

2

入力例 2

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

出力例 2

-1

入力例 3

10 30 50
60 5 25
30 2 12
25 3 11
20 5 10
18 4 9
15 6 8
12 3 7
10 4 6
8 5 5
5 3 4

出力例 3

1

Score : 400 pts

Problem Statement

Takahashi works as a delivery person. Today, he has received N delivery requests.

If he accepts the i-th request (1 \leq i \leq N), he receives a reward of P_i yen, but incurs expenses of C_i yen for fuel and other costs. Therefore, the net profit from accepting the i-th request is P_i - C_i yen (the net profit can be negative). Additionally, the weight of the package for the i-th request is W_i. If he does not accept a request, no reward or expense is incurred for that request, and there is no need to carry the package.

Takahashi will choose to accept 0 or more requests from the N requests. Each request is either accepted or not accepted; the same request cannot be accepted multiple times. If no requests are selected, both the total weight and the total net profit are 0.

However, all packages from the selected requests must be loaded onto a truck and delivered in a single trip. Since the truck has a maximum capacity of S, the total weight of the packages from the selected requests must be at most S.

Furthermore, in order to pay this month's rent, the total net profit from the selected requests must be at least T.

To summarize, let A (A \subseteq \{1, 2, \ldots, N\}) be the set of selected requests. We want to find a selection of A that satisfies both of the following conditions:

  • The total weight of the packages from the selected requests is at most S. That is, \displaystyle\sum_{i \in A} W_i \leq S
  • The total net profit from the selected requests is at least T. That is, \displaystyle\sum_{i \in A} (P_i - C_i) \geq T

Determine whether such a set A exists. If it exists, output the minimum value of |A| among all sets A satisfying the conditions. If it does not exist, output -1.

Constraints

  • 1 \leq N \leq 50
  • 1 \leq S \leq 10^4
  • 1 \leq T \leq 10^4
  • 1 \leq P_i \leq 10^4 (1 \leq i \leq N)
  • 1 \leq C_i \leq 10^4 (1 \leq i \leq N)
  • 1 \leq W_i \leq 10^4 (1 \leq i \leq N)
  • All inputs are integers

Input

N S T
P_1 C_1 W_1
P_2 C_2 W_2
\vdots
P_N C_N W_N

The first line contains the number of requests N, the maximum truck capacity S, and the minimum required total net profit T, separated by spaces.

In the following N lines, the i-th line (1 \leq i \leq N) contains the reward P_i, the expense C_i, and the package weight W_i for the i-th request, separated by spaces.

Output

If a selection of requests satisfying the conditions exists, output the minimum number of selected requests on a single line. If no such selection exists, output -1 on a single line.


Sample Input 1

4 8 10
8 3 3
7 4 4
12 4 5
3 5 2

Sample Output 1

2

Sample Input 2

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

Sample Output 2

-1

Sample Input 3

10 30 50
60 5 25
30 2 12
25 3 11
20 5 10
18 4 9
15 6 8
12 3 7
10 4 6
8 5 5
5 3 4

Sample Output 3

1
E - Elevator Transport

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 433 点

問題文

高橋君は引っ越し業者でアルバイトをしています。今日の仕事は、マンションの 1 階にある N 個の荷物を、エレベーターを使ってすべて同じ目的階まで運ぶことです。

各荷物 i(1 \leq i \leq N)には重さ W_i キログラムが設定されています。このマンションのエレベーターには、一度に積載できる荷物の重さの合計の上限 C キログラムがあります。高橋君はエレベーターに同乗して荷物を運びますが、高橋君自身の体重は積載上限には含まれません。

高橋君は効率よく作業を終わらせるため、N 個の荷物をいくつかのグループに分け、グループごとにまとめてエレベーターで運ぶことにしました。ここで、各荷物はちょうど 1 つのグループに属し、各グループには 1 個以上の荷物が含まれるものとします。また、各グループに含まれる荷物の重さの合計は C キログラム以下でなければなりません。

1 回の運搬では、1 つのグループに属する荷物をすべてエレベーターに積み、1 階から目的階まで運びます。次のグループを運ぶ際には、高橋君は空のエレベーターで 1 階に戻ります。運搬回数はグループの数に等しくなります。

すべての荷物を運び終えるために必要な最小の運搬回数を求めてください。

なお、すべての荷物の重さは積載上限 C 以下であることが保証されているため、どの荷物も単独で運ぶことができます。

制約

  • 1 \leq N \leq 15
  • 1 \leq C \leq 10^9
  • 1 \leq W_i \leq C(1 \leq i \leq N)
  • 入力は全て整数

入力

N C
W_1 W_2 \ldots W_N
  • 1 行目には、荷物の個数 N と、エレベーターの積載上限 C が、スペース区切りで与えられる。
  • 2 行目には、各荷物の重さ W_1, W_2, \ldots, W_N が、スペース区切りで与えられる。

出力

すべての荷物を運ぶために必要な最小の運搬回数を 1 行で出力せよ。


入力例 1

3 10
4 5 6

出力例 1

2

入力例 2

5 100
30 50 40 20 60

出力例 2

2

入力例 3

8 1000
200 300 150 400 250 100 350 450

出力例 3

3

Score : 433 pts

Problem Statement

Takahashi is working a part-time job at a moving company. Today's task is to transport N packages from the 1st floor of an apartment building to a single destination floor using an elevator.

Each package i (1 \leq i \leq N) has a weight of W_i kilograms. The elevator in this apartment building has a maximum total weight capacity of C kilograms for the packages loaded at one time. Takahashi rides the elevator alongside the packages to transport them, but his own body weight is not counted toward the weight capacity.

To finish the work efficiently, Takahashi decided to divide the N packages into several groups and transport each group together using the elevator. Here, each package belongs to exactly one group, and each group contains at least one package. Additionally, the total weight of the packages in each group must be at most C kilograms.

In one trip, Takahashi loads all packages belonging to one group onto the elevator and transports them from the 1st floor to the destination floor. To transport the next group, Takahashi returns to the 1st floor with the empty elevator. The number of trips equals the number of groups.

Find the minimum number of trips required to transport all the packages.

Note that it is guaranteed that the weight of every package is at most C, so any single package can be transported on its own.

Constraints

  • 1 \leq N \leq 15
  • 1 \leq C \leq 10^9
  • 1 \leq W_i \leq C (1 \leq i \leq N)
  • All inputs are integers

Input

N C
W_1 W_2 \ldots W_N
  • The first line contains the number of packages N and the elevator's weight capacity C, separated by a space.
  • The second line contains the weights of each package W_1, W_2, \ldots, W_N, separated by spaces.

Output

Print the minimum number of trips required to transport all the packages, on a single line.


Sample Input 1

3 10
4 5 6

Sample Output 1

2

Sample Input 2

5 100
30 50 40 20 60

Sample Output 2

2

Sample Input 3

8 1000
200 300 150 400 250 100 350 450

Sample Output 3

3