import sys input = lambda: sys.stdin.readline().rstrip() ii = lambda: int(input()) mi = lambda: map(int, input().split()) li = lambda: list(mi()) inf = 2 ** 63 - 1 mod = 998244353 n = ii() k = [i - 1 for i in li()][::-1] a = [] for _ in range(n): _ = ii() a.append(list(sorted(li(), reverse=True))) def check(x): ind = [-1] * n s = set(range(n)) nowkind = 0 nowk = k[0] while s: for v in list(s): if nowk <= len(a[v]) - 1 and a[v][min(len(a[v]) - 1, nowk)] >= x: return True f = False p = -1 for v in list(s): if nowk >= len(a[v]) and a[v][-1] >= x: nowk = len(a[v]) - 1 s.discard(v) p = v f = True break for v in list(s): if a[v][min(len(a[v]) - 1, nowk)] >= x: return True if f: s.add(p) nowk = k[nowkind] mind = -1 mini = x for v in list(s): if a[v][min(len(a[v]) - 1, nowk - 1)] >= x and a[v][min(len(a[v]) - 1, nowk - 1)] < mini: mind = v mini = a[v][min(len(a[v]) - 1, nowk - 1)] if mind == -1: return False x = mini s.discard(mind) nowkind += 1 if nowkind >= len(k) or nowk <= k[nowkind]: return False nowk = k[nowkind] return False def check2(x): ind = [-1] * n s = set(range(n)) nowkind = 0 while s: nowk = k[nowkind] for v in list(s): if len(a[v]) != nowk and a[v][min(len(a[v]) - 1, nowk)] >= x: return True mind = -1 mini = inf for v in list(s): if a[v][min(len(a[v]) - 1, nowk - 1)] >= x and a[v][min(len(a[v]) - 1, nowk - 1)] < mini: mind = v mini = a[v][min(len(a[v]) - 1, nowk - 1)] if mind == -1: return False x = mini s.discard(mind) nowkind += 1 if nowkind >= len(k) or nowk <= k[nowkind]: return False return False ok = 0 ng = 10 ** 9 + 1 while abs(ok - ng) > 1: mid = (ok + ng) // 2 if check(mid) or check2(mid): ok = mid else: ng = mid print(ok)