Problem:
Score : 2100 points

Problem Statement

Snuke has a board with an N \times N grid, and N \times N tiles.

Each side of a square that is part of the perimeter of the grid is attached with a socket.
That is, each side of the grid is attached with N sockets, for the total of 4 \times N sockets.
These sockets are labeled as follows:

The sockets on the top side of the grid: U1, U2, ..., UN from left to right

The sockets on the bottom side of the grid: D1, D2, ..., DN from left to right

The sockets on the left side of the grid: L1, L2, ..., LN from top to bottom

The sockets on the right side of the grid: R1, R2, ..., RN from top to bottom

Figure: The labels of the sockets

Snuke can insert a tile from each socket into the square on which the socket is attached.
When the square is already occupied by a tile, the occupying tile will be pushed into the next square, and when the next square is also occupied by another tile, that another occupying tile will be pushed as well, and so forth.
Snuke cannot insert a tile if it would result in a tile pushed out of the grid.
The behavior of tiles when a tile is inserted is demonstrated in detail at Sample Input/Output 1.

Snuke is trying to insert the N \times N tiles one by one from the sockets, to reach the state where every square contains a tile.
Here, he must insert exactly U_i tiles from socket Ui, D_i tiles from socket Di, L_i tiles from socket Li and R_i tiles from socket Ri.
Determine whether it is possible to insert the tiles under the restriction. If it is possible, in what order the tiles should be inserted from the sockets?

Constraints

1≦N≦300

U_i,D_i,L_i and R_i are non-negative integers.

The sum of all values U_i,D_i,L_i and R_i is equal to N \times N.

Partial Scores

2000 points will be awarded for passing the test set satisfying N≦40.

Additional 100 points will be awarded for passing the test set without additional constraints.

Input

The input is given from Standard Input in the following format:

N
U_1 U_2 ... U_N
D_1 D_2 ... D_N
L_1 L_2 ... L_N
R_1 R_2 ... R_N

Output

If it is possible to insert the tiles so that every square will contain a tile, print the labels of the sockets in the order the tiles should be inserted from them, one per line. If it is impossible, print NO instead. If there exists more than one solution, print any of those.

Sample Input 1

3
0 0 1
1 1 0
3 0 1
0 1 1

Sample Output 1

L1
L1
L1
L3
D1
R2
U3
R3
D2

Snuke can insert the tiles as shown in the figure below. An arrow indicates where a tile is inserted from, a circle represents a tile, and a number written in a circle indicates how many tiles are inserted before and including the tile.

Sample Input 2

2
2 0
2 0
0 0
0 0

Sample Output 2

NO

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
bool isok(vector<int>a, vector<int>b, vector<int>c, int alp)
{
	for (int i = 0; i < a.size(); i++)if (a[i] < 0)return false;
	for (int i = 0; i < b.size(); i++)if (b[i] < 0)return false;
	for (int i = 0; i < c.size(); i++)if (c[i] < 0)return false;
	int gen = alp;
	for (int i = 0; i < a.size(); i++)gen += a[i];
	sort(a.begin(), a.end());
	sort(b.begin(), b.end());
	sort(c.begin(), c.end());
	int sum = 0;
	int pb = 0, pc = 0;
	for (int i = 0; i <= a.size(); i++)
	{
		int t = a.size() - i;
		for (;;)
		{
			if (pb != b.size() && b[pb] < t + 1)sum += b[pb], pb++;
			else break;
		}
		for (;;)
		{
			if (pc != c.size() && c[pc] < t)sum += c[pc], pc++;
			else break;
		}
		if (sum + (t + 1)*(b.size() - pb) + t*(c.size() - pc) < gen)return false;
		if (i != a.size())sum += a[i];
	}
	sum = alp, pb = pc = 0;
	for (int i = 0; i <= a.size(); i++)
	{
		int t = a.size() - i;
		for (;;)
		{
			if (pb != b.size() && b[pb] < t)sum += b[pb], pb++;
			else break;
		}
		for (;;)
		{
			if (pc != c.size() && c[pc] < t)sum += c[pc], pc++;
			else break;
		}
		if (sum + t*(b.size() - pb) + t*(c.size() - pc) < gen)return false;
		if (i != a.size())sum += a[i];
	}
	return true;
}
int ans[333][333];
int u[333], d[333], l[333], r[333];
int ver[333];
bool flag[333][333];
typedef pair<int, int>pii;
vector<pii>topo;
void dfs(int x, int y, int num)
{
	if (flag[x][y])return;
	flag[x][y] = true;
	if (ans[x][y] == 1)for (int i = 0; i <= y - 1; i++)dfs(x, i, num);
	else if (ans[x][y] == 2)for (int i = y + 1; i < num; i++)dfs(x, i, num);
	else if (ans[x][y] == 3)for (int i = 0; i <= x - 1; i++)dfs(i, y, num);
	else if (ans[x][y] == 4)for (int i = x + 1; i < num; i++)dfs(i, y, num);
	topo.push_back(make_pair(x, y));
}
int main()
{
	int num;
	scanf("%d", &num);
	for (int i = 0; i < num; i++)scanf("%d", &u[i]);
	for (int i = 0; i < num; i++)scanf("%d", &d[i]);
	for (int i = 0; i < num; i++)scanf("%d", &l[i]);
	for (int i = 0; i < num; i++)scanf("%d", &r[i]);
	for (int i = 0; i < num; i++)ver[i] = num - u[i] - d[i];
	vector<int>za, zb, zc;
	for (int i = 0; i < num; i++)za.push_back(l[i] + r[i]), zc.push_back(num - u[i] - d[i]);
	//if (!isok(za, zb, zc, 0))
	{
		printf("NO\n");
		return 0;
	}
	for (int i = 0; i < num; i++)
	{
		int p1 = 0;
		for (;;)
		{
			if (l[i] == 0)break;
			for (;;)
			{
				if (p1 == num)return 0;
				l[i]--, ver[p1]--;
				vector<int>a, b, c;
				for (int j = i + 1; j < num; j++)a.push_back(l[j] + r[j]);
				for (int j = 0; j < num; j++)
				{
					if (p1 < j)b.push_back(ver[j]);
					else c.push_back(ver[j]);
				}
				if (isok(a, b, c, l[i] + r[i]))
				{
					ans[i][p1] = 1;
					p1++;
					break;
				}
				else l[i]++, ver[p1]++, p1++;
			}
		}
		int p2 = num - 1;
		for (;;)
		{
			if (r[i] == 0)break;
			for (;;)
			{
				if (p2 == -1)return 0;
				r[i]--, ver[p2]--;
				vector<int>a, b, c;
				for (int j = i + 1; j < num; j++)a.push_back(l[j] + r[j]);
				for (int j = 0; j < num; j++)
				{
					if (p1 <= j&&j < p2)b.push_back(ver[j]);
					else c.push_back(ver[j]);
				}
				if (isok(a, b, c, l[i] + r[i]))
				{
					ans[i][p2] = 2;
					p2--;
					break;
				}
				else r[i]++, ver[p2]++, p2--;
			}
		}
	}
	for (int i = 0; i < num; i++)
	{
		for (int j = 0; j < num; j++)
		{
			if (ans[j][i] == 0)
			{
				if (u[i] != 0)u[i]--, ans[j][i] = 3;
				else d[i]--, ans[j][i] = 4;
			}
		}
	}
	for (int i = 0; i < num; i++)
	{
		for (int j = 0; j < num; j++)
		{
			if (!flag[i][j])dfs(i, j, num);
		}
	}
	for (int i = 0; i < topo.size(); i++)
	{
		int x = topo[i].first, y = topo[i].second;
		if (ans[x][y] == 1)printf("L%d\n", x + 1);
		else if (ans[x][y] == 2)printf("R%d\n", x + 1);
		else if (ans[x][y] == 3)printf("U%d\n", y + 1);
		else if (ans[x][y] == 4)printf("D%d\n", y + 1);
	}
}