Trucking Diesel
Time limit1sMemory limit128 MB
Starting with 25000 liters that doubles as fuel, move east, south, or west across an altitude grid to the opposite corner with the most diesel left.
- Level
Medium7 of 10
- Topics
- Shortest path, Graph, Heap, Greedy
- Solved
- No attempts yet
Problem
A diesel truck carries diesel from a start cell to a destination cell. The truck has no separate fuel tank: the cargo diesel is also the fuel. Find the maximum number of liters that can still be on the truck at the destination without running out along the way. If the destination cannot be reached with the starting fuel, print .
The terrain is an -by- grid of altitudes in meters. Each altitude is between and and is a multiple of . The first row is the northern edge, the last row is the southern edge, the first column is the western edge, and the last column is the eastern edge. The truck starts at the north-west corner and must reach the south-east corner . In one step it may move to an adjacent cell to the east, south, or west. It cannot move north or diagonally.
Diesel weighs kg per liter. A downhill or flat step costs liters. An uphill step costs liters plus additional liters for every m of altitude gain and for every whole ton of total mass at the start of the step, where mass is rounded down to tons in kg units. For example, if the mass at the start of a segment is kg and the altitude rises from m to m, the segment costs liters.
Before each step the truck may throw away an integer number of liters of diesel. The unladen truck weighs tons ( kg) and it starts with liters of diesel.
Input
The first line contains and . Each of the next lines contains altitudes. Every altitude is between and and is a multiple of .
Output
Print the maximum number of liters that can be delivered. If the truck can arrive with an empty tank, print . If the destination cannot be reached, print .