#include #define rep(i,a,b) for(int i=a;i=b;i--) #define fore(i,a) for(auto &i:a) #define all(x) (x).begin(),(x).end() //#pragma GCC optimize ("-O3") using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); } typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60; templatebool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; } templatebool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; } //--------------------------------------------------------------------------------------------------- // トポロジカルソートする O(n) // 返り値が true の時のみ意味を持つ(false の場合は閉路) struct TopologicalSort { vector> E; TopologicalSort(int N) { E.resize(N); } void add_edge(int a, int b) { E[a].push_back(b); } bool visit(int v, vector& order, vector& color) { color[v] = 1; for (int u : E[v]) { if (color[u] == 2) continue; if (color[u] == 1) return false; if (!visit(u, order, color)) return false; } order.push_back(v); color[v] = 2; return true; } bool sort(vector& order) { int n = E.size(); vector color(n); for (int u = 0; u < n; u++) if (!color[u] && !visit(u, order, color)) return false; reverse(order.begin(), order.end()); return true; } }; /*---------------------------------------------------------------------------------------------------             ∧_∧       ∧_∧  (´<_` )  Welcome to My Coding Space!      ( ´_ゝ`) /  ⌒i @hamayanhamayan     /   \    | |     /   / ̄ ̄ ̄ ̄/  |   __(__ニつ/  _/ .| .|____      \/____/ (u ⊃ ---------------------------------------------------------------------------------------------------*/ int H, W, A[404], B[404]; int ans[404][404]; //--------------------------------------------------------------------------------------------------- void _main() { cin >> H >> W; rep(y, 0, H) cin >> A[y]; rep(x, 0, W) cin >> B[x]; sort(A, A + H, greater()); sort(B, B + W, greater()); TopologicalSort ts(H * W); rep(y, 0, H) { rep(x, 0, A[y] - 1) ts.add_edge(y * W + x, y * W + x + 1); rep(x, A[y] - 1, W - 1) ts.add_edge(y * W + x + 1, y * W + x); } rep(x, 0, W) { rep(y, 0, B[x] - 1) ts.add_edge(y * W + x, (y + 1) * W + x); rep(y, B[x] - 1, H - 1) ts.add_edge((y + 1) * W + x, y * W + x); } vector ord; bool res = ts.sort(ord); assert(res); int num = 1; fore(i, ord) { int x = i % W; int y = i / W; ans[y][x] = num; num++; } rep(y, 0, H) { rep(x, 0, W) { if(x) printf(" "); printf("%d", ans[y][x]); } printf("\n"); } }