//愚直解 #include #define rep(i, l, n) for (int i = (l); i < (n); i++) #define itrep(itr, st) for(auto itr = st.begin(); itr != st.end(); itr++) #define all(x) x.begin(), x.end() using namespace std; template using V = vector; int main(void) { int N; cin >> N; V K(N); rep(i, 0, N) { cin >> K[i]; } V T(N); V > A(N, V({})); rep(i, 0, N) { cin >> T[i]; rep(j, 0, T[i]) { int a; cin >> a; A[i].push_back(a); } } V perm(N); rep(i, 0, N) { perm[i] = i; } int ans = 0; do { int last = 1; rep(i, 0, N) { multiset st = {}; if (last != 1) { st.insert(last); } last = 1; for (int a : A[perm[i]]) { st.insert(-a); } int k = 0, bk = 0; itrep(itr, st) { k++; bk = *itr; if (k == K[i]) { last = bk; } } if (last == 1) { last = bk; } } ans = max(ans, -last); } while (next_permutation(all(perm))); cout << ans << endl; return 0; }