Making a Beautiful Puzzle
Time limit3sMemory limit128 MB
Fill each square of an N by M board with one of four colors so that orthogonal neighbors differ, maximizing total beauty and counting optimal placements modulo 1e9+7.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Backtracking, Combinatorics, Implementation
- Solved
- No attempts yet
Problem
For his birthday Seongyong got an puzzle board and puzzle pieces in red, blue, green and yellow. He has infinitely many pieces of each color.
Seongyong puts exactly one piece on every square of the board. He wants the board to look colorful, so two squares that touch horizontally or vertically always hold pieces of different colors.
Putting a piece of a given color on a given square earns the beauty fixed in advance for that square and that color. The beauty of the whole puzzle is the sum of the beauty earned on every square.
Write a program that finds the largest possible beauty of the whole puzzle and the number of placements that reach it.
Input
The first line contains and , separated by a space. (, )
The next lines contain the beauty earned by putting a red piece on each square, values per line.
The following lines give the values for blue pieces, the next lines give the values for green pieces, and the last lines give the values for yellow pieces, in the same format.
Every beauty is an integer between and . The largest possible beauty of the whole puzzle does not exceed 2,100,000,000.
Output
On the first line print the largest possible beauty of the whole puzzle and the number of placements that reach it, separated by a space. The number of placements can be very large, so print it modulo .
Hint
Write the four colors as R, B, G and Y.
In the first example the board has a single square, and every color gives a total beauty of 1, so 1 is the maximum. There are four placements.
In the second example, putting R and B on the top row and G and Y on the bottom row gives , which is the maximum. Only one placement reaches it.