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;
const int N = (int) 1e5 + 5, mod = (int) 0;
int mark[N], head[N], pre[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, pre[en] = head[u], head[u] = en++;
    to[en] = u, cap[en] = vu, pre[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 = pre[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[1000][1000], 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;
    int sum = 0;
    for (int i = 0; i < n; ++i) {
        add_edge(src, i << 1 | 0, d[i] + u[i], 0);
        sum += d[i] + u[i];
        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);
        }
    if (max_flow(src, snk) != sum) {
        nein();
    }
    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));
                }
            }
        }
    }
    assert(op.size() == n * n);
    for (auto x : op)
        cout << x << '\n';
}



Compiler output:
a.cc: In function 'int32_t main()':
a.cc:95:9: error: redeclaration of 'int sum'
   95 |     int sum = 0;
      |         ^~~
a.cc:77:9: note: 'int sum' previously declared here
   77 |     int sum = 0;
      |         ^~~