Mr. Problem

Author: Abu Rayhan Ahmad

Problem Setter: Md. Nafiul Adnan, Dept. of CSE

Metropolitan University, Sylhet.

Mr. Problem has a bunch of solutions stored in his Solution cottage. But there is a problem in Mr. Problems Solution cottage, some of the contestants aren’t honest. They try all the time searching the solution instead of thinking. Which is a very bad habit. So, Mr. Problem bought some CC cameras to check the cheaters face.

He represents his area of solution cottage as a 2D grid. He can set cameras at any of the 4 sides [Up, down, left or right]. Security level of a cell is total number of cameras that cover the cell.

Here you can see that if we set a camera at“L3”, it can cover the cells [(3,1),(3,2), …. (3,8)] , [ ( 2,1), (1,2)] , [ (4,1),(5,2)]. And security level of (3,3) is 3 as it is covered by 3 cameras, also security level of (1,1) is 1,  security level of (5,8) is 2.


Mr. Problem has N problems along there rating. He wants to place the problems in the cells where higher rated problems will be placed into a cell that has higher security level.

Suppose Mr. Problem has 3 problems. With the rating 1, 5, 2. As the second problem has the max rating he will place the problem in any of the cells that has max security level = 3. So, problem no.2 (rating 5) can be putted in cells [ ( 3,3 ), (3,6) ], Problem no. 3 ( rating 2 ) can be placed in any cell that has the second max security level, and problem no. 1 (rating 1) can be placed in any cell that has 3rd max security level.

You will be given the problems rating and CC cameras position, you have to print the expected position of the problems in the grid. See the sample input output below for better understanding. 

Input Format

N (0 <=N<=15),the number of problems. In the next line there will be an array [ 1<= arr[i] <= 100 ] consisting the distinct rating of ‘i’th problem. Next Line will contain R (rows),C (columns) and P (number of cameras), next P lines will contain the position of cameras, the First character will define the side of it, and the next integer will define the position of it. [R, C will be between 1 and 50, the cameras position will be valid for sure.] 

Output Format

Output the matrix consisting of problem id in cells. If a cell contains a problem, output the index of the problem, separate all the cells by a white space [index starts from 1], else print ‘*’. If it is not possible to place all the problems in the grid just print “No Solution Mr. Problem. Sorry!” without the quotes.

Samples

Input
3 1 5 2 5 8 4 U 3 L 3 R 4 D 6
Output
1 3 1 1 * 3 * * 3 1 1 * 1 1 1 * 1 1 2 1 1 2 1 3 3 1 3 3 1 3 3 3 * 1 1 * 1 1 1 3
Limits
Language Time Memory
GNU C 11 1s 512MB
GNU C++ 14 1s 512MB
GNU C++ 11 1s 512MB
PHP 7 1s 1024MB
Java (OpenJDK 8) 1s 4096MB
Statistics
Login To Submit