Pixels
Time limit2sMemory limit1024 MB
Color each pixel of an N by N grid black or white to maximize the color bonuses minus the costs of adjacent pixels with different colors.
- Level
Medium7 of 10
- Topics
- Graph
- Solved
- No attempts yet
Problem
You are given an matrix of pixels. Color each pixel black or white so that the end result is as pleasant as possible. You are given three kinds of information. For each pixel , is the bonus you get if you color it white. is the bonus you get if you color it black. For each pair of adjacent pixels (sharing an edge), is the cost you pay if the two pixels have different colors.
The cost is given for each pixel and each of its four directions. For pixel , is the cost when that pixel and differ, is the cost for , is the cost for , and is the cost for . If a neighbor is outside the matrix, its cost is still given as 0. and are always equal, and the same symmetry holds for the other directions.
Maximize the sum of over white pixels plus the sum of over black pixels, minus the costs of adjacent pairs with different colors. Each such pair is counted once.
Input
The first line contains . The next lines each contain values, where the -th value on line is . In the same format, the next lines contain . Finally, lines follow, each with four values , , , . The -th of these lines belongs to pixel .
Output
Print one line containing the maximum possible pleasantness.
Constraints
Hint
The only possible coloring for the optimal pleasantness is:
Black White
Black Black
The cost of coloring and differently is very high, so they should share a color. Black is the better choice for them. For and , the better option is clear.