#define _USE_MATH_DEFINES #include <cstdio> #include <iostream> #include <sstream> #include <fstream> #include <iomanip> #include <algorithm> #include <cmath> #include <complex> #include <string> #include <vector> #include <array> #include <list> #include <queue> #include <stack> #include <set> #include <map> #include <bitset> #include <numeric> #include <limits> #include <climits> #include <cfloat> #include <functional> #include <iterator> #include <memory> #include <regex> using namespace std; bool topologicalSort(const vector<vector<int> >& edges, vector<int>& node) { int n = edges.size(); node.resize(n); vector<int> restEdges(n, 0); for(int i=0; i<n; ++i){ for(unsigned j=0; j<edges[i].size(); ++j){ ++ restEdges[edges[i][j]]; } } int restNodes = n; queue<int> q; for(int i=0; i<n; ++i){ if(restEdges[i] == 0){ node[n-restNodes] = i; q.push(i); -- restNodes; } } while(restNodes > 0){ if(q.empty()){ node.clear(); return false; } int i = q.front(); q.pop(); for(unsigned j=0; j<edges[i].size(); ++j){ int k = edges[i][j]; -- restEdges[k]; if(restEdges[k] == 0){ node[n-restNodes] = k; q.push(k); -- restNodes; } } } return true; } int main() { int h, w; cin >> h >> w; vector<int> a(h), b(w); for(int i=0; i<h; ++i) cin >> a[i]; for(int i=0; i<w; ++i) cin >> b[i]; sort(a.begin(), a.end()); sort(b.begin(), b.end()); vector<vector<int> > edges(h*w); for(int y=0; y<h; ++y){ for(int x=0; x<w-1; ++x){ int i = y * w + x; int j = y * w + (x + 1); if(x < a[y] - 1) edges[i].push_back(j); else edges[j].push_back(i); } } for(int x=0; x<w; ++x){ for(int y=0; y<h-1; ++y){ int i = y * w + x; int j = (y + 1) * w + x; if(y < b[x] - 1) edges[i].push_back(j); else edges[j].push_back(i); } } vector<int> node; topologicalSort(edges, node); vector<vector<int> > ans(h, vector<int>(w)); for(int i=0; i<h*w; ++i){ int y = node[i] / w; int x = node[i] % w; ans[y][x] = i + 1; } for(int y=0; y<h; ++y){ cout << ans[y][0]; for(int x=1; x<w; ++x) cout << ' ' << ans[y][x]; cout << endl; } return 0; }