#include #include #include #include #define rep(i, l, n) for (int i = (l); i < (n); i++) #define all(x) x.begin(), x.end() using namespace std; template using V = vector; bool check(int& N, V&K, V >&A, int mid) { V vec = {}; V wild = {}; rep(i, 0, N) { int s = 0; for (int a : A[i]) { s += (a >= mid); } if (s == A[i].size()) { wild.push_back(s); } else { vec.push_back(s); } } sort(all(vec)); sort(all(wild)); reverse(all(wild)); rep(i, 1, N + 1) { int k = K[N - i]; int s = -1, w = -1; if (vec.empty() == false) { s = vec[vec.size() - 1]; } if (wild.empty() == false) { w = wild[0]; } if (max(s, w) >= k) { return true; } else if (i < N - 1 and s == k - 1) { vec.pop_back(); } else if (wild.empty() == false) { wild.pop_back(); } else { return false; } } return true; } int main(void) { int N; cin >> N; V K(N); rep(i, 0, N) { cin >> K[i]; } V T(N); V > A(N, V({})); V st = {}; rep(i, 0, N) { cin >> T[i]; rep(j, 0, T[i]) { int a; cin >> a; A[i].push_back(a); st.push_back(a); } } st.erase(unique(all(st)), st.end()); sort(all(st)); int ok = 0, ng = st.size(); while (ng - ok > 1) { int mid = (ok + ng) >> 1; if (check(N, K, A, st[mid])) { ok = mid; } else { ng = mid; } } int ans = st[ok]; cout << ans << endl; return 0; }