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 Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.AbstractQueue;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.AbstractCollection;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskJ solver = new TaskJ();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskJ {
        public static int[] dx = {-1, 1, 0, 0};
        public static int[] dy = {0, 0, -1, 1};

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            int[][] caps = new int[4][n];
            for (int i = 0; i < 4; i++) {
                caps[i] = in.readIntArray(n);
            }
            int N = n * n + 4 * n + 2;
            List<MinCostFlow.Edge>[] graph = new ArrayList[N];
            for (int i = 0; i < N; i++) graph[i] = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    MinCostFlow.addEdge(graph, n * n + 4 * j + 0, i * n + j, 1, i);
                    MinCostFlow.addEdge(graph, n * n + 4 * j + 1, i * n + j, 1, n - i);
                    MinCostFlow.addEdge(graph, n * n + 4 * i + 2, i * n + j, 1, j);
                    MinCostFlow.addEdge(graph, n * n + 4 * i + 3, i * n + j, 1, n - j);
                    MinCostFlow.addEdge(graph, i * n + j, N - 2, 1, 0);
                }
            }
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < n; j++) {
                    MinCostFlow.addEdge(graph, N - 1, n * n + 4 * j + i, caps[i][j], 0);
                }
            }

            int[] ans = MinCostFlow.minCostFlow(graph, N - 1, N - 2, n * n);
            if (ans[0] < n * n) {
                out.println("NO");
                return;
            }
            int[][] dir = new int[n][n];
            for (int i = 0; i < n * n; i++) {
                for (MinCostFlow.Edge ed : graph[i]) {
                    if (ed.to == N - 2) continue;
                    int flow = graph[ed.to].get(ed.rev).f;
                    if (flow == 1) {
                        dir[i / n][i % n] = (ed.to - n * n) % 4;
                    }
                }
            }
            int[] queue = new int[n * n];
            int front = 0, back = 0;
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    if (i == 0 && dir[i][j] == 0) queue[back++] = i * n + j;
                    if (j == 0 && dir[i][j] == 2) queue[back++] = i * n + j;
                    if (i == n - 1 && dir[i][j] == 1) queue[back++] = i * n + j;
                    if (j == n - 1 && dir[i][j] == 3) queue[back++] = i * n + j;
                }
            }
            boolean[][][] cleardir = new boolean[4][n][n];
            while (front < back) {
                int cx = queue[front] / n, cy = queue[front] % n;
                front++;
                for (int k = 0; k < 4; k++) {
                    int nx = cx + dx[k], ny = cy + dy[k];
                    cleardir[k][cx][cy] = (nx < 0 || ny < 0 || nx >= n || ny >= n || cleardir[k][nx][ny]);
                }
                for (int k = 0; k < 4; k++) {
                    int nx = cx + dx[k], ny = cy + dy[k];
                    if (nx < 0 || ny < 0 || nx >= n || ny >= n) continue;
                    if ((dir[nx][ny] == (k ^ 1)) && cleardir[k ^ 1][cx][cy]) {
                        queue[back++] = nx * n + ny;
                    }
                }

            }

            for (int i = 0; i < n * n; i++) {
                int cx = queue[i] / n, cy = queue[i] % n;
                out.printf("%c%d\n", "UDLR".charAt(dir[cx][cy]), (dir[cx][cy] <= 1 ? cy : cx) + 1);
            }
        }

    }

    static class MinCostFlow {
        public static void addEdge(List<MinCostFlow.Edge>[] graph, int s, int t, int cap, int cost) {
            graph[s].add(new MinCostFlow.Edge(t, cap, cost, graph[t].size()));
            graph[t].add(new MinCostFlow.Edge(s, 0, -cost, graph[s].size() - 1));
        }

        public static int[] minCostFlow(List<MinCostFlow.Edge>[] graph, int s, int t, int maxf) {
            int n = graph.length;
            int[] prio = new int[n];
            int[] curflow = new int[n];
            int[] prevedge = new int[n];
            int[] prevnode = new int[n];
            int[] pot = new int[n];

            //bellmanFord(graph, s, pot); // bellmanFord invocation can be skipped if edges costs are non-negative
            int flow = 0;
            int flowCost = 0;
            while (flow < maxf) {
                PriorityQueue<Long> q = new PriorityQueue<>();
                q.add((long) s);
                Arrays.fill(prio, Integer.MAX_VALUE);
                prio[s] = 0;
                boolean[] finished = new boolean[n];
                curflow[s] = Integer.MAX_VALUE;
                while (!finished[t] && !q.isEmpty()) {
                    long cur = q.remove();
                    int u = (int) (cur & 0xFFFF_FFFFL);
                    int priou = (int) (cur >>> 32);
                    if (priou != prio[u])
                        continue;
                    finished[u] = true;
                    for (int i = 0; i < graph[u].size(); i++) {
                        MinCostFlow.Edge e = graph[u].get(i);
                        if (e.f >= e.cap)
                            continue;
                        int v = e.to;
                        int nprio = prio[u] + e.cost + pot[u] - pot[v];
                        if (prio[v] > nprio) {
                            prio[v] = nprio;
                            q.add(((long) nprio << 32) + v);
                            prevnode[v] = u;
                            prevedge[v] = i;
                            curflow[v] = Math.min(curflow[u], e.cap - e.f);
                        }
                    }
                }
                if (prio[t] == Integer.MAX_VALUE)
                    break;
                for (int i = 0; i < n; i++)
                    if (finished[i])
                        pot[i] += prio[i] - prio[t];
                int df = Math.min(curflow[t], maxf - flow);
                flow += df;
                for (int v = t; v != s; v = prevnode[v]) {
                    MinCostFlow.Edge e = graph[prevnode[v]].get(prevedge[v]);
                    e.f += df;
                    graph[v].get(e.rev).f -= df;
                    flowCost += df * e.cost;
                }
            }
            return new int[]{flow, flowCost};
        }

        public static class Edge {
            public int to;
            public int f;
            public int cap;
            public int cost;
            public int rev;

            Edge(int to, int cap, int cost, int rev) {
                this.to = to;
                this.cap = cap;
                this.cost = cost;
                this.rev = rev;
            }

        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int[] readIntArray(int tokens) {
            int[] ret = new int[tokens];
            for (int i = 0; i < tokens; i++) {
                ret[i] = nextInt();
            }
            return ret;
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}