#include #include typedef struct { int key, id; } data; typedef struct { data obj[300003]; int size; } max_heap; void push(max_heap* h, data x) { int i = ++(h->size), j = i >> 1; data tmp; h->obj[i] = x; while (j > 0) { if (h->obj[i].key > h->obj[j].key) { tmp = h->obj[j]; h->obj[j] = h->obj[i]; h->obj[i] = tmp; i = j; j >>= 1; } else break; } } data pop(max_heap* h) { int i = 1, j = 2; data output = h->obj[1], tmp; h->obj[1] = h->obj[(h->size)--]; while (j <= h->size) { if (j < h->size && h->obj[j^1].key > h->obj[j].key) j ^= 1; if (h->obj[j].key > h->obj[i].key) { tmp = h->obj[j]; h->obj[j] = h->obj[i]; h->obj[i] = tmp; i = j; j <<= 1; } else break; } return output; } int main() { int i, j, N, K[300003], T[300003], *A[300003]; unsigned int l = 2000000003, r = 0, m; scanf("%d", &N); for (i = 1; i <= N; i++) scanf("%d", &(K[i])); for (i = 1; i <= N; i++) { scanf("%d", &(T[i])); A[i] = (int*)malloc(sizeof(int) * (T[i] + 1)); for (j = 1; j <= T[i]; j++) { scanf("%d", &(A[i][j])); if (l > A[i][j]) l = A[i][j]; if (r < A[i][j]) r = A[i][j]; } } data d; max_heap h[2]; int k, max[2]; while (l < r) { m = (l + r + 1) / 2; h[0].size = 0; h[1].size = 0; for (i = 1; i <= N; i++) { for (j = 1, k = 0; j <= T[i]; j++) if (A[i][j] >= m) k++; d.key = k; if (k == T[i]) push(&(h[0]), d); else push(&(h[1]), d); } if (h[0].size > 0) max[0] = h[0].obj[1].key; else max[0] = -1; for (i = N; i >= 1; i--) { if (h[0].size == 0) max[0] = -1; if (h[1].size > 0) max[1] = h[1].obj[1].key; else max[1] = -1; if (max[0] >= K[i] || max[1] >= K[i]) break; if (max[1] == K[i] - 1) pop(&(h[1])); else if (max[0] >= 0) pop(&(h[0])); else { i = 0; break; } } if (i >= 1) l = m; else r = m - 1; } printf("%u\n", l); fflush(stdout); return 0; }