#include #include #include #include #include int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int n; std::cin >> n; std::vector a(n); for (auto &e : a) std::cin >> e; std::vector> b(n); for (int i = 0; i < n; ++i) { int k; std::cin >> k; b[i].resize(k); for (int j = 0; j < k; ++j) { std::cin >> b[i][j]; } } auto check = [&](int x) -> bool { std::map p, q; for (int i = 0; i < n; ++i) { std::array cnt{ 0, 0 }; for (int v : b[i]) { ++cnt[v >= x]; } ++(cnt[0] ? p : q)[cnt[1]]; } int maxp = -1; for (int i = n - 1; i >= 0; --i) { if (p.lower_bound(a[i]) != p.end() or q.lower_bound(a[i]) != q.end()) { return true; } if (maxp >= a[i] and not q.empty()) return true; if (auto itp = p.find(a[i] - 1); itp != p.end()) { maxp = std::max(maxp, itp->first); if (--(itp->second) == 0) { p.erase(itp); } } else if (auto itq = q.begin(); itq != q.end()) { if (--(itq->second) == 0) { q.erase(itq); } if (i == 0) return true; } else { return false; } } return false; }; int l = 0, r = 1000000010; while (r - l > 1) { int x = (l + r) >> 1; (check(x) ? l : r) = x; } std::cout << l << std::endl; }