#P3227. [HNOI2013] 切糕
[HNOI2013] 切糕
Description
After much effort, Xiao A obtained a piece of qiegao. The qiegao is a rectangular cuboid, and Xiao A plans to cut it in half horizontally to share it with Xiao B. For aesthetic reasons, Xiao A hopes the cutting surface can be as smooth and harmonious as possible. So she asks you to find the best cutting plan.
For simplicity, we view the qiegao as a cuboid grid with length , width , and height . We call the point at layer , row , and column the point , and it has a nonnegative disharmony value . A valid cutting surface satisfies the following two conditions:
-
It has exactly one intersection with each vertical axis (there are vertical axes). That is, the cutting surface is a function , and for all (, ), we specify a cutting point with .
-
The cutting surface needs to meet a smoothness requirement: cutting points on adjacent vertical axes cannot be too far apart. For all and , if , then , where is a given nonnegative integer.
There may be many cutting surfaces satisfying the above conditions. Xiao A wants the one that minimizes the total disharmony value at the cutting points.
Input Format
The first line contains three positive integers , representing the length, width, and height of the qiegao.
The second line contains a nonnegative integer , representing the smoothness requirement.
Then follow matrices of size rows and columns. In the -th matrix, the entry at row and column is (, , ).
Output Format
Output a single integer, the minimal possible total disharmony value under the legality constraints.
2 2 2
1
6 1
6 1
2 6
2 6
6
Hint
Explanation for Sample 1
The optimal cutting surface is , .
Constraints
For of the testdata, , , and all given disharmony values do not exceed .
Translated by ChatGPT 5
京公网安备 11011102002149号