#include #include #include using pii = std::pair; using ll = long long; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); int n, m; std::cin >> n; std::vector a(n + 1); for (int i = 1; i <= n; ++i) std::cin >> a[i]; std::cin >> m; std::vector b(m + 1); for (int i = 1; i <= m; ++i) std::cin >> b[i]; std::vector> dp(n + 1, std::vector(m + 1, 0)); std::vector> path(n + 1, std::vector(m + 1, {0, 0})); for (int i = 1; i <= n; ++i) { int maxlen = 0; int pk = 0; for (int j = 1; j <= m; ++j) { dp[i][j] = dp[i - 1][j]; path[i][j] = {i - 1, j}; if (a[i] == b[j]) { if (maxlen + 1 > dp[i][j]) { dp[i][j] = maxlen + 1; path[i][j] = {i - 1, pk}; } } if (b[j] < a[i]) { if (dp[i - 1][j] > maxlen) { maxlen = dp[i - 1][j]; pk = j; } } } } int maxlen = 0; int endj = 0; for (int j = 1; j <= m; ++j) { if (dp[n][j] > maxlen) { maxlen = dp[n][j]; endj = j; } } std::cout << maxlen << "\n"; if (maxlen > 0) { std::stack res; int curri = n; int currj = endj; while (curri > 0 && currj > 0) { pii prev = path[curri][currj]; if (dp[curri][currj] > dp[prev.first][prev.second] && prev.second != currj) { res.push(a[curri]); } curri = prev.first; currj = prev.second; } bool first = true; while (!res.empty()) { if (!first) { std::cout << " "; } std::cout << res.top(); res.pop(); first = false; } std::cout << "\n"; } }