#abc320f. F - Fuel Round Trip
F - Fuel Round Trip
Score : points
问题描述
你计划从坐标 出发,前往坐标 ,然后掉头返回坐标 。在此过程中,你只能在出程时沿正方向移动,在回程时沿负方向移动。
你将驾车出行。汽车每行驶一个单位距离就会消耗一升燃料。你可以携带最多 升燃料,并且在没有燃料的情况下无法移动。
对于每个 ,在坐标 处有一个加油站,你可以在那里以 日元的价格获得 升燃料。但是,你不能携带超过 升的燃料。更准确地说,如果你当前有 升燃料并在坐标 的加油站加油,你需要支付 日元,并且你的燃料量会变成 升。每个加油站在整个往返行程中最多只能使用一次。
确定当你最初拥有 升燃料时,是否可以完成这个计划。如果可能,找出所需的最小金额。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
You are planning to travel from coordinate to coordinate on a number line, then turn around and return to coordinate . Here, you can only move in the positive direction on the outbound trip and in the negative direction on the return trip.
You will travel by car. The car consumes one liter of fuel for every unit distance it travels. You can carry up to liters of fuel and cannot move without fuel.
For each , there is a gas station at coordinate , where you can get liters of fuel for yen. However, you cannot carry more than liters of fuel. More precisely, if you have liters of fuel and use the gas station at coordinate , you must pay yen, and your amount of fuel becomes liters. Each gas station can be used at most once in total during the round trip.
Determine if you can achieve this plan when you initially have liters of fuel, and if it is possible, find the minimum amount of money required.
Constraints
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Output
If the plan can be achieved, print the minimum amount of money required; otherwise, print -1
.
Sample Input 1
4 10
2 5 9 11
8 10
5 8
4 9
Sample Output 1
9
You can achieve the plan by using the gas station at coordinate on the outbound trip and the one at coordinate on the return trip, paying a total of yen.
It is impossible to achieve the plan by paying yen or less. Note that you cannot use the same gas station on both the outbound and return trips.
Sample Input 2
1 1
100000
Sample Output 2
-1
Sample Input 3
5 20
4 13 16 18 23
1 16
2 8
4 11
8 13
Sample Output 3
13
update @ 2024/3/10 01:39:13