inlineintread() { ints(0), w(1); char ch = getchar(); while (ch < '0' || ch > '9'){if (ch == '-') w = -1; ch = getchar();} while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar(); return s * w; }
inlinevoidput(int x) { if (! x) putchar('0'); if (x < 0) putchar('-'), x = -x; intnum(0); char c[66]; while (x) c[++ num] = x % 10 + 48, x /= 10; while (num) putchar(c[num --]); return (void)(putchar('\n')); }
queue<int>q; int n, m, res; int num[N], num2[N], val[N];
inlineintpd(int x) { int i, lim(0); for (i = 1; i <= n - m; ++ i) if (num2[i] >= x) ++ lim; for (i = 1; i <= n; ++ i) q.push(num[i] ? (num[i] >= x ? 0 : inf) : 1); while (! q.empty()) { int a, b, c; a = q.front(); q.pop(); if (q.empty()) return a <= lim; b = q.front(); q.pop(); c = q.front(); q.pop(); q.push(min(inf, min(a + b, min(a + c, b + c)))); } }
signedmain() { int i, j, k;
n = read(), m = read();
for (i = 1; i <= m; ++ i) { j = read(), k = read(); num[k] = val[i] = j; } for (i = 1; i <= n - m; ++ i) val[i + m] = num2[i] = read(); sort(val + 1, val + n + 1);
int l = 1, r = n; while (l < r) { int mid = (l + r + 1) >> 1; if (pd(val[mid])) l = mid; else r = mid - 1; }