#include #define rep(i, l, n) for (int i = (l); i < (n); i++) #define all(x) x.begin(), x.end() #define last(v) v[v.size() - 1] using namespace std; template using V = vector; int N; V K; V T; V > A; V normal; V full; bool check(int mid); bool check_2(int t); int main(void) { cin >> N; K = V(N); rep(i, 0, N) { cin >> K[i]; } T = V(N); A = V >(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(st[mid])) { ok = mid; } else { ng = mid; } } int ans = st[ok]; cout << ans << endl; return 0; } bool check(int mid) { normal = {}; full = {}; for (V& va : A) { int s = 0; for (int a : va) { s += (a >= mid); } if (s == va.size()) { full.push_back(s); } else { normal.push_back(s); } } sort(all(normal)); sort(all(full), greater()); return check_2(N - 1); } bool check_2(int t) { if (t == -1) { return true; } int k = K[t]; int a = (normal.empty() == false) ? last(normal) : -1; int b = (full.empty() == false) ? full[0] : -1; if (max(a, b) >= k) { return true; } if (t > 0 and a == k - 1) { normal.pop_back(); return check_2(t - 1); } if (b >= 0) { full.pop_back(); return check_2(t - 1); } return false; }