This page is still under construction.

Parts of this page are still being built. What you see may change.

Short on Fuel

Time limit1sMemory limit1024 MB

Summary
Given fuel depots on a grid, find the minimum starting fuel so a car moving only right or down can reach the goal, refueling at depots it passes.
Level

Medium6 of 10

Topics
Dynamic programming, Sorting, Greedy, Array
Solved
No attempts yet

Problem

Hyangbin, who had always dreamed of seeing the pyramids in person, signed up for a desert tour package. On the second day of the trip he arrived at the entrance of the desert and boarded the car used for the desert tour.

While he sat in the car waiting to depart, he found a desert tour guidebook. On the R×CR \times C map inside the guidebook, the locations of NN fuel depots and the amount of fuel stored at each depot were marked.

Hyangbin was a car enthusiast who had memorized the fuel efficiency of every car, and he knew that the car he was riding consumes 11 unit of fuel for every 11 unit of distance it travels. The car only drives in directions parallel to the xx-axis or the yy-axis of the map. For example, if the car moves from (0,0)\left(0,0\right) to (i,j)\left(i,j\right) along a shortest path, it consumes i+ji+j units of fuel.

The car currently has no fuel, so he plans to refuel at the gas station here before departing. Because the fuel sold at the gas station is very expensive, Hyangbin will put in as little fuel as possible here and refuel afterward at the fuel depots he visits along the way.

Hyangbin wants to see the pyramids as soon as possible, so he asked the driver not to drive in the direction away from the pyramids. In other words, the car moves only right or down.

There is no limit on the amount of fuel that can be put in at the gas station, and there is no fuel depot at the current location or at the location of the pyramids. Also, no location has two or more fuel depots.

Because each fuel depot has a different location and amount of stored fuel, the amount of fuel that must be put in at the start can differ depending on the order in which the fuel depots are visited. Find the minimum amount of fuel that must be put in at the gas station for Hyangbin to travel from the current location (1,1)\left(1,1\right) to the point (R,C)\left(R,C\right) where the pyramids are.

Input

The first line gives the integers RR and CC, the height and width of the map. (2≤R,C≤3 0002 \leq R, C \leq 3\ 000)

The second line gives the integer NN, the number of fuel depots marked on the map. (0≤N≤1 0000 \leq N \leq 1\ 000)

Each of the next NN lines gives the integer coordinates (r,c)\left(r,c\right) of a fuel depot and the integer ff, the amount of fuel stored there. (1≤r≤R1 \leq r \leq R, 1≤c≤C1 \leq c \leq C, 0≤f≤1000 \leq f \leq 100)

Output

Print the minimum amount of fuel that must be put in at the gas station for Hyangbin to travel from the current location (1,1)\left(1,1\right) to the point (R,C)\left(R,C\right) where the pyramids are.

Examples2

  1. Example 1

    Input
    3 3
    1
    2 2 1
    
    Expected output
    3
    
  2. Example 2

    Input
    5 5
    3
    2 2 1
    3 4 2
    4 3 4
    
    Expected output
    4