import itertools as iter import collections as coll import heapq as hq import bisect as bis from decimal import Decimal as dec from functools import cmp_to_key import math import sys #import pypyjit #pypyjit.set_param('max_unroll_recursion=-1') sys.setrecursionlimit(10 ** 6) inp = sys.stdin.readline input = lambda : inp().rstrip() getN = lambda : int(inp()) getNs = lambda : map(int, inp().split()) getList = lambda :list(map(int, inp().split())) getStrs = lambda n : [input() for _ in [0] * n] def yexit(): print("Yes"); exit(0) def nexit(): print("No"); exit(0) pi = 3.141592653589793 mod = 1000000007 MOD = 998244353 INF = 4611686018427387903 dx = [1, 0, -1, 0]; dy = [0, 1, 0, -1] #di = coll.defaultdict(int) """ Main Code """ N = getN() K = getList() T = [0]*N A = [[] for _ in [0]*N] for i in range(N): T[i] = getN() A[i] = getList() def check(m): s = [0] * N P_tmp = [] Q_tmp = [] for i in range(N): s[i] = sum((k >= m) for k in A[i]) if(s[i] != len(A[i])): P_tmp.append(s[i]) else: Q_tmp.append(s[i]) P = coll.deque(sorted(P_tmp)) Q = coll.deque(sorted(Q_tmp)) for i, k in enumerate(K[::-1]): p = P[-1] if(P) else -1 q = Q[-1] if(Q) else -1 if(max(p,q) >= k): return True elif(p == k-1 and i < N): P.pop() elif(q != -1): Q.popleft() else: return False return True ok = 0 ng = 10 ** 9 + 1 while(ng - ok > 1): mid = (ok + ng) >> 1 if(check(mid)): ok = mid else: ng = mid print(ok)