n = int(input()) K = list(map(int, input().split())) A = [[] for _ in range(n)] for i in range(n): input() A[i] = list(map(int, input().split())) def ok(x): C = [] all_ = [] for row in A: c = sum(a >= x for a in row) if c == len(row): all_.append(c) else: C.append(c) if not C: return True C.sort() all_.sort(reverse=True) cp = len(C) - 1 cc = 0 for k in K[::-1]: if C and C[cp + min(cc, len(all_))] >= k: return True elif all_ and all_[0] >= k: return True if C[cp] == k - 1: cp -= 1 cc += 1 elif all_: all_.pop() else: return False if cp < 0 and all_: return True return False l = 1 r = 1 << 30 while r - l > 1: mid = (l + r) // 2 if ok(mid): l = mid else: r = mid print(l)