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# (Mono 4.6.2.0) (judge verdict: Time Limit Exceeded)

using System;
using System.Text;
using System.Collections.Generic;
class Solve{
    int N;
    int[] count;
    int[] state;
    public Solve(){}
    StringBuilder sb;
    public static int Main(){
        new Solve().Run();
        return 0;
    }
    void Run(){
        sb = new StringBuilder();
        Calc();
        Console.Write(sb.ToString());
    }
    void Calc(){
        N = int.Parse(Console.ReadLine());
        int[] U = new int[N];
        int[] D = new int[N];
        int[] L = new int[N];
        int[] R = new int[N];
        string[] str = Console.ReadLine().Split(' ');
        for(int i=0;i<N;i++){
            U[i] = int.Parse(str[i]);
        }
        str = Console.ReadLine().Split(' ');
        for(int i=0;i<N;i++){
            D[i] = int.Parse(str[i]);
        }
        str = Console.ReadLine().Split(' ');
        for(int i=0;i<N;i++){
            L[i] = int.Parse(str[i]);
        }
        str = Console.ReadLine().Split(' ');
        for(int i=0;i<N;i++){
            R[i] = int.Parse(str[i]);
        }
        {
            bool b = true;
            for(int i=0;i<N;i++){
                b &= U[i] + D[i] <= N;
                b &= L[i] + R[i] <= N;
            }
            if(!b){
                sb.Append("NO\n");
                return;
            }
        }
        int l = N*N;
        int r = N*N + N;
        int u = N*N + 2*N;
        int d = N*N + 3*N;
        int s = N*N + 4*N;
        int g = N*N + 4*N + 1;
        List<int> f = new List<int>();
        List<int> t = new List<int>();
        List<long> capa = new List<long>();
        List<long> cost = new List<long>();
        for(int i=0;i<N;i++){
            f.Add(s);
            t.Add(l+i);
            capa.Add(L[i]);
            cost.Add(0);
            f.Add(s);
            t.Add(r+i);
            capa.Add(R[i]);
            cost.Add(0);
            f.Add(s);
            t.Add(u+i);
            capa.Add(U[i]);
            cost.Add(0);
            f.Add(s);
            t.Add(d+i);
            capa.Add(D[i]);
            cost.Add(0);
        }
        for(int i=0;i<N;i++){
            for(int j=0;j<N;j++){
                f.Add(l+i);
                t.Add(i*N+j);
                capa.Add(1);
                cost.Add(j);
                f.Add(r+i);
                t.Add(i*N+j);
                capa.Add(1);
                cost.Add(N-1-j);
                f.Add(u+j);
                t.Add(i*N+j);
                capa.Add(1);
                cost.Add(i);
                f.Add(d+j);
                t.Add(i*N+j);
                capa.Add(1);
                cost.Add(N-1-i);
                f.Add(i*N+j);
                t.Add(g);
                capa.Add(1);
                cost.Add(0);
            }
        }
        MinCostFlow F = new MinCostFlow(f.ToArray(),t.ToArray(),capa.ToArray(),cost.ToArray(),g+1,s,g,N*N);
        state = new int[N*N];
        for(int i=0;i<N;i++){
            Vertex V = F.point[l+i];
            for(int j=1;j<=N;j++){
                if(V.edges[j].capacity == 0){
                    state[i*N+j-1] = 2;
                }
            }
            V = F.point[r+i];
            for(int j=1;j<=N;j++){
                if(V.edges[j].capacity == 0){
                    state[i*N+j-1] = 3;
                }
            }
            V = F.point[d+i];
            for(int j=1;j<=N;j++){
                if(V.edges[j].capacity == 0){
                    state[(j-1)*N+i] = 1;
                }
            }
        }
        count = new int[N*N];
        for(int i=0;i<N*N;i++){
            if(state[i] == 0){
                count[i] = i/N;
            }
            if(state[i] == 1){
                count[i] = N - 1 - i/N;
            }
            if(state[i] == 2){
                count[i] = i%N;
            }
            if(state[i] == 3){
                count[i] = N - 1 - i%N;
            }
        }
        
        for(int i=0;i<N*N;i++){
            if(count[i] == 0){
                Calc(i/N,i%N);
            }
        }
    }
    void Calc(int h,int w){
        count[h*N+w]--;
        if(state[h*N+w] == 0){
            sb.Append("U"+(w+1)+"\n");
        }
        if(state[h*N+w] == 1){
            sb.Append("D"+(w+1)+"\n");
        }
        if(state[h*N+w] == 2){
            sb.Append("L"+(h+1)+"\n");
        }
        if(state[h*N+w] == 3){
            sb.Append("R"+(h+1)+"\n");
        }
        for(int i=0;i<h;i++){
            if(state[i*N+w] == 1){
                count[i*N+w]--;
                if(count[i*N+w] == 0){
                    Calc(i,w);
                }
            }
        }
        for(int i=h+1;i<N;i++){
            if(state[i*N+w] == 0){
                count[i*N+w]--;
                if(count[i*N+w] == 0){
                    Calc(i,w);
                }
            }
        }
        for(int i=0;i<w;i++){
            if(state[h*N+i] == 3){
                count[h*N+i]--;
                if(count[h*N+i] == 0){
                    Calc(h,i);
                }
            }
        }
        for(int i=w+1;i<N;i++){
            if(state[h*N+i] == 2){
                count[h*N+i]--;
                if(count[h*N+i] == 0){
                    Calc(h,i);
                }
            }
        }
    }
}
class Edge{
    public Vertex to;
    public long capacity;
    public long cost;
    public Edge rev;
    public Edge(Vertex v,long ca,long co){
        to = v;
        capacity = ca;
        cost = co;
    }
}
class Vertex{
    public bool finished;
    public long cost;
    public long h;
    public Vertex fromv;
    public Edge frome;
    public List<Edge> edges;
    public void AddEdge(Edge e){
        edges.Add(e);
    }
    public Vertex(){
        edges = new List<Edge>();
        finished = false;
        cost = -1;
    }
}
struct Data{
    public Vertex v;
    public long cost;
    public Data(Vertex v0,long c){
        v = v0;
        cost = c;
    }
}
class MinCostFlow{
    public Vertex[] point;
    Vertex S;
    Vertex G;
    public long res;
    int E;
    const long INF = 1000000000000000;
    public MinCostFlow(int[] f,int[] t,long[] capacity,long[] cost,int n,int s,int g,long flow){
        point = new Vertex[n];
        for(int i=0;i<n;i++){
            point[i] = new Vertex();
        }
        E = f.Length;
        for(int i=0;i<E;i++){
            point[f[i]].AddEdge(new Edge(point[t[i]],capacity[i],cost[i]));
            point[t[i]].AddEdge(new Edge(point[f[i]],0,-cost[i]));
            point[f[i]].edges[point[f[i]].edges.Count-1].rev = point[t[i]].edges[point[t[i]].edges.Count-1];
            point[t[i]].edges[point[t[i]].edges.Count-1].rev = point[f[i]].edges[point[f[i]].edges.Count-1];
        }
        S = point[s];
        G = point[g];
        res = 0;
        while(true){
            Heap H = new Heap(2*E+1);
            S.cost = 0;
            H.push(new Data(S,0));
            while(!H.Empty()){
                Data D = H.pop();
                Vertex V = D.v;
                if(V.finished){
                    continue;
                }
                V.finished = true;
                for(int i=0;i<V.edges.Count;i++){
                    if(V.edges[i].capacity == 0){
                        continue;
                    }
                    Vertex T = V.edges[i].to;
                    if(T.cost == -1 || T.cost > V.cost + V.edges[i].cost + V.h - T.h){
                        T.cost = V.cost + V.edges[i].cost + V.h - T.h;
                        T.fromv = V;
                        T.frome = V.edges[i];
                        H.push(new Data(T,T.cost));
                    }
                }
            }
            if(G.cost == -1){
                res = -1;
                break;
            }
            for(int i=0;i<n;i++){
                point[i].h += point[i].cost;
                point[i].cost = -1;
                point[i].finished = false;
            }
            long d = flow;
            for(Vertex V = G;V != S;V = V.fromv){
                d = Math.Min(d,V.frome.capacity);
            }
            res += d*G.h;
            for(Vertex V = G;V != S;V = V.fromv){
                V.frome.capacity -= d;
                V.frome.rev.capacity += d;
            }
            flow -= d;
            if(flow == 0){
                break;
            }
        }
    }
}
class Heap{
    public int size;
    Data[] obj;
    public Heap(int maxsize){
        size = 0;
        obj = new Data[maxsize];
    }
    public void push(Data a){
        int i = size;
        size++;
        while(i > 0){
            int p = (i - 1)/2;
            if(obj[p].cost <= a.cost){
                obj[i] = a;
                break;
            }
            obj[i] = obj[p];
            i = p;
        }
        if(i == 0){
            obj[0] = a;
        }
    }
    public bool Empty(){
        return size == 0;
    }
    public Data pop(){
        Data t = obj[0];
        int i = 0;
        size--;
        while(2*i+1 < size){
            int p = 2*i+1;
            if(p+1 < size && obj[p+1].cost < obj[p].cost){
                p++;
            }
            if(obj[p].cost < obj[size].cost){
                obj[i] = obj[p];
                i = p;
            }
            else{
                obj[i] = obj[size];
                break;
            }
        }
        if(2*i+1 >= size){
            obj[i] = obj[size];
        }
        return t;
    }
}