Under construction

Still being built. Things may change

Estimating the Flood Risk

Time limit2sMemory limit512 MB

Summary
Given measured altitudes on some grid cells, find the minimum total altitude over all integer assignments where edge-adjacent cells differ by at most 1, or report impossibility.
Level

Medium7 of 10

Topics
Graph, Shortest path, Greedy, Math
Solved
No attempts yet

Problem

Mr. Boat owns a vast extent of land. As many typhoons have struck Japan this year, he became concerned about the flood risk of his estate and wants to know the average altitude of his land. The land is too vast to measure the altitude at many spots. As there are no steep slopes in the estate, he thought that it would be enough to measure the altitudes at only a limited number of sites and then approximate the altitudes of the rest based on them.

Multiple approximations might be possible based on the same measurement results, in which case he wants to know the worst case, that is, the one giving the lowest average altitude.

Mr. Boat's estate, which has a rectangular shape, is divided into grid-aligned rectangular areas of the same size. Altitude measurements have been carried out in some of these areas, and the measurement results are now at hand. The altitudes of the remaining areas are to be approximated on the assumption that the altitudes of two adjoining areas sharing an edge differ by at most 1.

In the first sample given below, the land is divided into 5×4 areas. The altitudes of the areas at (1, 1) and (5, 4) are measured to be 10 and 3, respectively. In this case, the altitudes of all the areas are uniquely determined on the assumption that the altitudes of adjoining areas differ by at most 1.

In the second sample, there are multiple possibilities, among which the one giving the lowest average altitude should be considered.

In the third sample, no altitude assignment satisfies the assumption on altitude differences.

Your job is to write a program that approximates the average altitude of his estate. To be precise, the program should compute the total of the approximated and measured altitudes of all the mesh-divided areas. If two or more different approximations are possible, the program should compute the total with the severest approximation, that is, the one giving the lowest total of the altitudes.

Input

The input consists of a single test case of the following format.

w d n
x1 y1 z1
.
.
.
xn yn zn

Here, w, d, and n are integers between 1 and 50, inclusive. w and d are the numbers of areas in the two sides of the land. n is the number of areas where altitudes are measured. The i-th line of the following n lines contains three integers, xi, yi, and zi, satisfying 1 ≤ xi ≤ w, 1 ≤ yi ≤ d, and −100 ≤ zi ≤ 100. They mean that the altitude of the area at (xi, yi) was measured to be zi. At most one measurement result is given for the same area, i.e., for i ≠ j, (xi, yi) ≠ (xj, yj).

Output

If all the unmeasured areas can be assigned their altitudes without any conflicts with the measured altitudes assuming that two adjoining areas have an altitude difference of at most 1, output an integer that is the total of the measured or approximated altitudes of all the areas. If more than one such altitude assignment is possible, output the minimum altitude total among the possible assignments.

If no altitude assignments satisfy the altitude difference assumption, output No.

Examples4

  1. Example 1

    Input
    5 4 2
    1 1 10
    5 4 3
    
    Expected output
    130
    
  2. Example 2

    Input
    5 4 3
    2 2 0
    4 3 0
    5 1 2
    
    Expected output
    -14
    
  3. Example 3

    Input
    3 3 2
    1 1 8
    3 3 3
    
    Expected output
    No
    
  4. Example 4

    Input
    2 2 1
    1 1 -100
    
    Expected output
    -404