#include #define rep(i, l, n) for (int i = (l); i < (n); i++) #define all(x) x.begin(), x.end() #define last(x) x[x.size() - 1] using namespace std; template using V = vector; bool check(int& N, V&K, V >&A, int m) { deque P = {}; deque Q = {}; rep(i, 0, N) { int s = 0; for (int a : A[i]) { s += (a >= m); } if (s < A[i].size()) { P.push_back(s); } else { Q.push_back(s); } } sort(all(P)); sort(all(Q)); rep(i, 1, N + 1) { int k = K[N - i]; int p = P.empty() ? -1 : last(P); int q = Q.empty() ? -1 : last(Q); if (max(p, q) >= k) { return true; } else if (i < N and p == k - 1) { P.pop_back(); } else if (q != -1) { Q.pop_front(); } 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 t = (ok + ng) >> 1; if (check(N, K, A, st[t])) { ok = t; } else { ng = t; } } int ans = st[ok]; cout << ans << endl; return 0; }