3358 {
3361 int K;
3363 unordered_set<int>
ms;
3364 auto *
const m =
new int[
M];
3365 auto *
const n =
new int[
N + 1];
3366 memset(n, -1, (N + 1) * sizeof(int));
3367 for(
int i = 0; i <
M; i++) {
3368 cin >> m[i];
3370 }
3371 auto cp = unordered_map<int, int>();
3372 for(int i = 0; i < K; i++) {
3373 int c;
3374 int p;
3375 cin >> c >> p;
3376 n[p] = c;
3377 cp.insert(make_pair(c, p));
3378 }
3379 if(cp.contains(1)) {
3380
3381 cout << cp[1];
3382 delete[] n;
3383 delete[] m;
3384 return 0;
3385 }
3386 if(
ms.contains(1)) {
3387
3388 int current = 1;
3389 for(
int i = 0; i <
M; i++) {
3390
3391 if(cp.contains(m[i])) {
3392
3393 current = cp[m[i]];
3394 continue;
3395 }
3396
3397 while(n[current] != -1) {
3398 current++;
3399 }
3400 n[current] = m[i];
3401 if(m[i] == 1) {
3402 cout << current;
3403 delete[] n;
3404 delete[] m;
3405 return 0;
3406 }
3407 }
3408 }
3409
3411 for(int i = M - 1; i >= 0; i--) {
3412 if(cp.contains(m[i])) {
3413
3414 current = cp[m[i]];
3415 continue;
3416 }
3417
3418 while(n[current] != -1) {
3419 current--;
3420 }
3421 n[current] = m[i];
3422 }
3423 for(
int i = 1; i <=
N; i++) {
3424 if(n[i] == -1) {
3425 cout << i;
3426 break;
3427 }
3428 }
3429 delete[] n;
3430 delete[] m;
3431 return 0;
3432 }
void ms(vector< int > &arr, int l, int r, int *ans)