Colored Tiles 4
Time limit1sMemory limit512 MB
Place N tiles of given colors and sizes (1x1 or 1x2) on an HxW board without overlap to maximize the sum of edge scores between adjacent tiles.
- Level
Hard9 of 10
- Topics
- Graph, Matrix, Implementation
- Solved
- No attempts yet
Problem
In 2018, the International Olympiad in Informatics will be held in Japan. To celebrate it, the IOI committee plans to make an artwork of "slightly odd design" and decorate the competition venue with it. The committee asked JOI (Just Odd Inventions) Co. Ltd. to design it. JOI proposed the following.
- The artwork is made on a rectangular board of cells. The board is paved with tiles, and the tiles do not overlap.
- Tile () is or , and its color is .
- A tile may be rotated and used as a tile.
JOI fixed the kinds of tiles but did not say how to lay them out. The committee must keep JOI's tile kinds and find a layout that maximizes the beauty of the design.
When two tiles share an edge of cells and their colors are and , that edge scores . The beauty is the sum of the scores of all such edges. If two tiles share the edges of two cells, both edges count separately. Find a layout whose beauty is as large as possible.
Input
The first line contains four space-separated integers , , , and . The board has cells, there are colors, and tiles are used.
The -th line () contains two integers and . Tile has size and color .
The -th line () contains integers . When a tile of color and a tile of color share an edge, the score is .
Output
Print lines. The -th line describes how to place tile .
- If , print two integers and . The tile covers the cell in row (counted from the top) and column (counted from the left).
- If , print four integers , , , and . The tile covers the cell in row and column , and the cell in row and column (rows counted from the top, columns from the left).
Constraints
All input data satisfy the following conditions.
- ()
- ()
- (, )
- (, )
Hint
In this sample, the board is and 4 tiles are used. The tiles are as follows.
The figure below shows the layout. The numbers in the figure are tile numbers.

The beauty of this design is 26.
- Tile 2 (color 2) and tile 4 (color 1) share an edge, so they score 7.
- Tile 2 (color 2) and tile 1 (color 1) share an edge, so they score 7.
- Tile 4 (color 1) and tile 1 (color 1) share an edge, so they score 2.
- Tile 4 (color 1) and tile 3 (color 3) share an edge, so they score 5.
- Tile 1 (color 1) and tile 3 (color 3) share an edge, so they score 5.