/
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 466 点
問題文
高橋君は、東西に一直線に並んだ N 個の町を西から東へ順番にドライブしています。町には西から順に 1, 2, \ldots, N の番号が付けられています。高橋君は途中で引き返すことはできず、常に東方向(番号が増える方向)にのみ進みます。
高橋君は町 1 にガソリンが 0 リットルの状態でスタートし、町 N に到達することが目標です。高橋君の車のガソリンタンクの容量は T リットルであり、タンクに T リットルを超えるガソリンを入れることはできません。
隣接する町 i と町 i + 1 (1 \leq i \leq N-1) の間を移動するには、ちょうど D_i リットルのガソリンが消費されます。移動は一度開始すると途中で止まることはできず、タンクに残っているガソリンが D_i リットル未満の場合、町 i から町 i+1 へ移動することはできません。
各町 i (1 \leq i \leq N) にはガソリンスタンドがあり、1 リットルあたり C_i 円でガソリンを購入できます。高橋君は各町に滞在している間に、購入後のタンク内のガソリン量が T リットルを超えない範囲で、0 以上の任意の実数リットルのガソリンを購入できます。
高橋君の行動は以下の手順を繰り返すことで進みます:
- 町 1 にいる状態からスタートする(タンクのガソリンは 0 リットル)。
- 現在いる町のガソリンスタンドで、好きな量のガソリンを購入する(0 リットルでもよい。ただし購入後のタンク内のガソリン量が T リットルを超えてはならない)。
- 現在いる町が町 N であれば終了する。そうでなければ、次の町へ移動する(町 i にいるなら町 i+1 へ移動し、タンクから D_i リットルのガソリンが消費される)。移動後、手順 2 に戻る。
高橋君が町 1 から町 N まで到達するために必要なガソリン代の合計の最小値を求めてください。
すべての i (1 \leq i \leq N-1) について D_i \leq T であれば町 N に到達することが可能であり、そうでなければ(すなわち、ある i について D_i > T であれば)到達は不可能です。到達不可能な場合は -1 を出力してください。
なお、到達が可能な場合、最適な購入戦略においては各町で整数リットルのガソリンを購入すれば十分であることが示せるため、最小コストは必ず整数になります。
(注:C_N は入力形式の統一のため与えられますが、町 N に到着した後は移動がないため、町 N でガソリンを購入する必要はありません。また、制約より、到達可能な場合の答えは符号付き 64-bit 整数に収まります。)
制約
- 2 \leq N \leq 5 \times 10^5
- 1 \leq T \leq 10^9
- 1 \leq C_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq D_i \leq 10^9 (1 \leq i \leq N-1)
- \displaystyle \sum_{i=1}^{N-1} D_i \leq 10^9
- 入力はすべて整数である。
入力
N T
C_1 C_2 \ldots C_N
D_1 D_2 \ldots D_{N-1}
- 1 行目には、町の数を表す整数 N と、ガソリンタンクの容量を表す整数 T が、スペース区切りで与えられる。
- 2 行目には、各町 i でのガソリンの 1 リットルあたりの価格を表す整数 C_1, C_2, \ldots, C_N が、スペース区切りで与えられる。
- 3 行目には、隣接する町 i と町 i+1 の間の移動に必要なガソリン量を表す整数 D_1, D_2, \ldots, D_{N-1} が、スペース区切りで与えられる。
出力
高橋君が町 1 から町 N まで到達可能な場合、必要なガソリン代の合計の最小値を整数で 1 行に出力してください。到達が不可能な場合は -1 を出力してください。
入力例 1
4 10 5 2 4 1 4 6 3
出力例 1
38
入力例 2
3 5 3 1 4 4 6
出力例 2
-1
入力例 3
10 15 8 6 7 3 9 2 5 4 10 1 5 4 7 3 6 8 2 5 4
出力例 3
179
入力例 4
25 50 30 28 35 20 25 18 40 22 16 19 27 15 21 14 26 13 24 12 23 11 29 10 31 9 8 12 18 7 20 15 10 22 9 14 16 8 19 11 13 17 6 21 5 23 4 24 3 25 2
出力例 4
5269
入力例 5
2 1000000000 1000000000 1 1000000000
出力例 5
1000000000000000000
Score : 466 pts
Problem Statement
Takahashi is driving from west to east through N towns lined up in a straight line. The towns are numbered 1, 2, \ldots, N from west to east. Takahashi cannot turn back and always travels only in the eastward direction (the direction in which the town numbers increase).
Takahashi starts at Town 1 with 0 liters of fuel in his tank, and his goal is to reach Town N. The capacity of the fuel tank of Takahashi's car is T liters, and he cannot put more than T liters of fuel in the tank.
Moving between adjacent towns i and i + 1 (1 \leq i \leq N-1) consumes exactly D_i liters of fuel. Once a move starts, he cannot stop midway. If the fuel remaining in the tank is less than D_i liters, he cannot move from Town i to Town i+1.
Each town i (1 \leq i \leq N) has a gas station where fuel can be purchased at C_i yen per liter. While staying in each town, Takahashi can purchase any non-negative real number of liters of fuel, as long as the amount of fuel in the tank after purchase does not exceed T liters.
Takahashi's actions proceed by repeating the following steps:
- Start at Town 1 (with 0 liters of fuel in the tank).
- Purchase any amount of fuel at the gas station of the current town (it can be 0 liters, but the amount of fuel in the tank after purchase must not exceed T liters).
- If the current town is Town N, terminate. Otherwise, move to the next town (if currently in Town i, move to Town i+1, consuming D_i liters of fuel from the tank). After moving, return to Step 2.
Find the minimum total cost of fuel required for Takahashi to reach Town N from Town 1.
If D_i \leq T for all i (1 \leq i \leq N-1), it is possible to reach Town N; otherwise (i.e., if D_i > T for some i), it is impossible. If it is impossible to reach Town N, output -1.
Note that if it is possible to reach the destination, it can be shown that purchasing an integer number of liters of fuel at each town is sufficient in an optimal strategy, so the minimum cost is always an integer.
(Note: C_N is provided to unify the input format, but since there is no travel after arriving at Town N, there is no need to purchase fuel at Town N. Also, under the constraints, the answer fits within a signed 64-bit integer when reaching the destination is possible.)
Constraints
- 2 \leq N \leq 5 \times 10^5
- 1 \leq T \leq 10^9
- 1 \leq C_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq D_i \leq 10^9 (1 \leq i \leq N-1)
- \displaystyle \sum_{i=1}^{N-1} D_i \leq 10^9
- All input values are integers.
Input
N T
C_1 C_2 \ldots C_N
D_1 D_2 \ldots D_{N-1}
- The first line contains the integer N, representing the number of towns, and the integer T, representing the capacity of the fuel tank, separated by a space.
- The second line contains the integers C_1, C_2, \ldots, C_N, representing the price of fuel per liter at each town i, separated by spaces.
- The third line contains the integers D_1, D_2, \ldots, D_{N-1}, representing the amount of fuel required to move between adjacent towns i and i+1, separated by spaces.
Output
If Takahashi can reach Town N from Town 1, output the minimum total cost of fuel as an integer in a single line. If it is impossible to reach Town N, output -1.
Sample Input 1
4 10 5 2 4 1 4 6 3
Sample Output 1
38
Sample Input 2
3 5 3 1 4 4 6
Sample Output 2
-1
Sample Input 3
10 15 8 6 7 3 9 2 5 4 10 1 5 4 7 3 6 8 2 5 4
Sample Output 3
179
Sample Input 4
25 50 30 28 35 20 25 18 40 22 16 19 27 15 21 14 26 13 24 12 23 11 29 10 31 9 8 12 18 7 20 15 10 22 9 14 16 8 19 11 13 17 6 21 5 23 4 24 3 25 2
Sample Output 4
5269
Sample Input 5
2 1000000000 1000000000 1 1000000000
Sample Output 5
1000000000000000000