Language: C++

// In the name of God

#include <iostream>
#include <algorithm>
#include <fstream>
#include <vector>
#include <deque>
#include <assert.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <stdio.h>
#include <string.h>
#include <utility>
#include <math.h>
#include <bitset>
#include <iomanip>

using namespace std;
#define prev tavas
const int N = (int) 1e5 + 5, mod = (int) 0;
int mark[N], head[N], prev[N], cap[N], to[N], en;
void nein() {
    cout << "NO\n";
    exit(0);
}
void add_edge(int u, int v, int uv, int vu) {
    to[en] = v, cap[en] = uv, prev[en] = head[u], head[u] = en++;
    to[en] = u, cap[en] = vu, prev[en] = head[v], head[v] = en++;
}
int dfs(int v, int t, int f = 1e9) {
    if (v == t) return f;
    if (mark[v]++) return 0;
    for (int e = head[v]; ~e; e = prev[e]) {
        int u = to[e];
        if (cap[e]) {
            int x = dfs(u, t, min(f, cap[e]));
            if (x > 0) {
                cap[e] -= x;
                cap[e ^ 1] += x;
                return x;
            }
        }
    }
    return 0;
}
int max_flow(int s, int t) {
    int res = 0;
    while (true) {
        memset(mark, 0, sizeof mark);
        int x = dfs(s, t);
        if (!x) break;
        res += x;
    }
    return res;
}
string make(char p, int x) {
    x++;
    string s = "";
    while (x) s += x % 10 + '0', x /= 10;
    reverse(s.begin(), s.end());
    string res = "";
    res += p;
    res += s;
    return res;
}
int mat[1000][1000], u[N], d[N], l[N], r[N], vis[N][N], up[N], dp[N], lp[N], rp[N];
int32_t main() {
    memset(head, -1, sizeof head);
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) cin >> u[i];
    for (int i = 0; i < n; ++i) cin >> d[i];
    for (int i = 0; i < n; ++i) cin >> l[i];
    for (int i = 0; i < n; ++i) cin >> r[i];
    int sum = 0;
    for (int i = 0; i < n; ++i) {
        lp[i] = 0;
        rp[i] = n - 1;
        up[i] = 0;
        dp[i] = n - 1;
        if (u[i] + d[i] > n) {
            nein();
        }
        if (l[i] + r[i] > n) {
            nein();
        }
        sum += u[i] + d[i] + l[i] + r[i];
    }
    if (sum != n * n) {
        nein();
    }
    int src = 2 * n, snk = 2 * n + 1;
    for (int i = 0; i < n; ++i) {
        add_edge(src, i << 1 | 0, d[i] + u[i], 0);
        add_edge(i << 1 | 1, snk, n - l[i] - r[i], 0);
    }
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < n; ++j) {
            mat[i][j] = en;
            add_edge(i << 1 | 0, i << 1 | 1, 1, 0);
        }
    vector<string> op;
    for (int step = 0; step < n * n; ++step) {
        for (int i = 0; i < n; ++i) {
            while (up[i] <= dp[i] && vis[up[i]][i]) up[i]++;
            while (lp[i] <= rp[i] && vis[i][lp[i]]) lp[i]++;
            while (dp[i] >= up[i] && vis[dp[i]][i]) dp[i]--;
            while (rp[i] >= lp[i] && vis[i][rp[i]]) rp[i]--;
        }
        for (int i = 0; i < n; ++i) if (up[i] <= dp[i]) {
            if (i == lp[up[i]]) {
                if (mat[i << 1 | 0][up[i] << 1 | 1]) {
                    if (l[up[i]]) vis[up[i]][i] = 1, op.push_back(make('L', up[i]));
                } else {
                    if (u[i]) vis[up[i]][i] = 1, op.push_back(make('U', i));
                }
            }
            if (i == rp[up[i]]) {
                if (mat[i << 1 | 0][up[i] << 1 | 1]) {
                    if (r[up[i]]) vis[up[i]][i] = 1, op.push_back(make('R', up[i]));
                } else {
                    if (u[i]) vis[up[i]][i] = 1, op.push_back(make('U', i));
                }
            }
            if (i == lp[dp[i]]) {
                if (mat[i << 1 | 0][dp[i] << 1 | 1]) {
                    if (l[dp[i]]) vis[dp[i]][i] = 1, op.push_back(make('L', dp[i]));
                } else {
                    if (d[i]) vis[dp[i]][i] = 1, op.push_back(make('D', i));
                }
            }
            if (i == rp[dp[i]]) {
                 if (mat[i << 1 | 0][dp[i] << 1 | 1]) {
                    if (r[dp[i]]) vis[dp[i]][i] = 1, op.push_back(make('R', dp[i]));
                } else {
                    if (d[i]) vis[dp[i]][i] = 1, op.push_back(make('D', i));
                }
            }
        }
    }
    for (auto x : op)
        cout << x << '\n';
}





Compiler output:
/tmp/cc0sv8Fz.o: in function `main':
a.cc:(.text+0x689): relocation truncated to fit: R_X86_64_PC32 against symbol `lp' defined in .bss section in /tmp/cc0sv8Fz.o
a.cc:(.text+0x6ad): relocation truncated to fit: R_X86_64_PC32 against symbol `rp' defined in .bss section in /tmp/cc0sv8Fz.o
a.cc:(.text+0x6c4): relocation truncated to fit: R_X86_64_PC32 against symbol `up' defined in .bss section in /tmp/cc0sv8Fz.o
a.cc:(.text+0x6e8): relocation truncated to fit: R_X86_64_PC32 against symbol `dp' defined in .bss section in /tmp/cc0sv8Fz.o
a.cc:(.text+0x986): relocation truncated to fit: R_X86_64_PC32 against symbol `up' defined in .bss section in /tmp/cc0sv8Fz.o
a.cc:(.text+0x9a0): relocation truncated to fit: R_X86_64_PC32 against symbol `up' defined in .bss section in /tmp/cc0sv8Fz.o
a.cc:(.text+0x9b7): relocation truncated to fit: R_X86_64_PC32 against symbol `up' defined in .bss section in /tmp/cc0sv8Fz.o
a.cc:(.text+0x9ce): relocation truncated to fit: R_X86_64_PC32 against symbol `dp' defined in .bss section in /tmp/cc0sv8Fz.o
a.cc:(.text+0x9e9): relocation truncated to fit: R_X86_64_PC32 against symbol `up' defined in .bss section in /tmp/cc0sv8Fz.o
a.cc:(.text+0xa2e): relocation truncated to fit: R_X86_64_PC32 against symbol `lp' defined in .bss section in /tmp/cc0sv8Fz.o
a.cc:(.text+0xa48): additional relocation overflows omitted from the output
collect2: error: ld returned 1 exit status