#include int ri() { int n; scanf("%d", &n); return n; } std::vector > hen[400][400]; int ord[400][400]; bool used[400][400]; int head = 0; void dfs(int x, int y) { for (auto &j : hen[x][y]) { if (used[j.first][j.second]) continue; used[j.first][j.second] = true; dfs(j.first, j.second); } ord[x][y] = head++; } int main() { int h = ri(), w = ri(); int a[h], b[w]; for (int i = 0; i < h; i++) a[i] = ri() - 1; for (int i = 0; i < w; i++) b[i] = ri() - 1; std::sort(a, a + h, std::greater<>()); std::sort(b, b + w, std::greater<>()); for (int i = 0; i < h; i++) { for (int j = 0; j < a[i]; j++) hen[i][j].push_back({i, j + 1}); for (int j = a[i]; j + 1 < w; j++) hen[i][j + 1].push_back({i, j}); } for (int i = 0; i < w; i++) { for (int j = 0; j < b[i]; j++) hen[j][i].push_back({j + 1, i}); for (int j = b[i]; j + 1 < h; j++) hen[j + 1][i].push_back({j, i}); } for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (!used[i][j]) { used[i][j] = true; dfs(i, j); } std::cerr << h * w - ord[i][j] << " "; } std::cerr << std::endl; } return 0; }