#include using namespace std; using ll = long long; using Graph = vector< vector >; bool dfs(int cur, vector& memo, Graph& g, vector& order) { memo[cur] = 1; for (int nex : g[cur]) { if (memo[nex] == 2) continue; if (memo[nex] == 1) return false; if (!dfs(nex, memo, g, order)) return false; } memo[cur] = 2; order.push_back(cur); return true; } bool tsort(Graph& g, vector& order) { vector memo(g.size(), 0); for (int i = 0; i < g.size(); i++) { if (memo[i] != 0) continue; if (!dfs(i, memo, g, order)) return false; } reverse(order.begin(), order.end()); return true; } int main() { cin.tie(0); ios::sync_with_stdio(false); int H, W; cin >> H >> W; vector a(H), b(W); for (int i = 0; i < H; i++) cin >> a[i]; sort(a.begin(), a.end()); for (int j = 0; j < W; j++) cin >> b[j]; sort(b.begin(), b.end()); Graph g(H * W); for (int i = 0; i < H; i++) { for (int j = 0; j < a[i] - 1; j++) { g[i * W + j].push_back(i * W + j + 1); } for (int j = W - 1; j >= a[i]; j--) { g[i * W + j].push_back(i * W + j - 1); } } for (int j = 0; j < W; j++) { for (int i = 0; i < b[j] - 1; i++) { g[i * W + j].push_back((i + 1) * W + j); } for (int i = H - 1; i >= b[j]; i--) { g[i * W + j].push_back((i - 1) * W + j); } } vector order; assert(tsort(g, order)); vector< vector > ans(H, vector(W)); for (int i = 0; i < H * W; i++) { int h = order[i] / W; int w = order[i] % W; ans[h][w] = i + 1; } for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cout << ans[i][j] << " \n"[j + 1 == W]; } } return 0; }