/** * author: ivanzuki * created: Wed May 12 2021 **/ #include using namespace std; string to_string(string s) { return '"' + s + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(bool b) { return (b ? "true" : "false"); } template string to_string(pair p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } void debug_out() { cerr << endl; } template void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } template void dout(string name, int idx, T arg) { cerr << name << " = " << to_string(arg); } template void dout(string names, int idx, T1 arg, T2... args) { cerr << names.substr(0, names.find(',')) << " = " << to_string(arg) << ", "; dout(names.substr(names.find(',') + 2), idx + 1, args...); } #ifdef LOCAL #define debug(...) cerr << "[", dout(#__VA_ARGS__, 0, __VA_ARGS__), cerr << "]" << endl; #else #define debug(...) 42 #endif int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; vector a(n), b(m); for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < m; i++) { cin >> b[i]; } sort(a.begin(), a.end()); sort(b.begin(), b.end()); vector oa = a, ob = b; vector> ans(n, vector(m)); set unused; for (int k = 0; k < n * m; k++) { unused.insert(k); } int l = 0, r = n * m - 1; for (int i = 0, j = 0; i < n && j < m; ) { debug(i, b); if (b[j] > 1) { int pref = a[i] - 1; vector to_reverse; for (int k = j; k < m; k++) { assert(b[k] > 0); if (b[k] == 1) { ans[i][k] = r; --r; } else { ans[i][k] = l; ++l; if (pref > 0) { --pref; } else { to_reverse.push_back(k); } --b[k]; } } vector nums; for (int k : to_reverse) { nums.push_back(ans[i][k]); } reverse(nums.begin(), nums.end()); for (int k = 0; k < (int) to_reverse.size(); k++) { ans[i][to_reverse[k]] = nums[k]; } ++i; } else if (b[j] == 1) { vector to_reverse; for (int k = i; k < n; k++) { if (a[k] == 1) { ans[k][j] = r; --r; } else { ans[k][j] = l; ++l; --a[k]; to_reverse.push_back(k); } } vector nums; for (int k : to_reverse) { nums.push_back(ans[k][j]); } reverse(nums.begin(), nums.end()); for (int k = 0; k < (int) to_reverse.size(); k++) { ans[to_reverse[k]][j] = nums[k]; } ++j; } else { assert(0); } } vector seen(n * m); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { ++seen[ans[i][j]]; cout << ans[i][j] + 1 << ' '; } cout << '\n'; } a = oa, b = ob; for (int row = 0; row < n; row++) { vector dp(m, 1); for (int j = 1; j < m; j++) { for (int i = 0; i < j; i++) { if (ans[row][i] < ans[row][j]) { dp[j] = max(dp[j], dp[i] + 1); } } } assert(a[row] == *max_element(dp.begin(), dp.end())); } for (int col = 0; col < m; col++) { vector dp(n, 1); for (int j = 1; j < n; j++) { for (int i = 0; i < j; i++) { if (ans[i][col] < ans[j][col]) { dp[j] = max(dp[j], dp[i] + 1); } } } debug(dp); // assert(b[col] == *max_element(dp.begin(), dp.end())); } assert(all_of(seen.begin(), seen.end(), [&](int x) { return x == 1; })); return 0; }