Design Tutorial: Make It Nondeterministic

A way to make a new task is to make it nondeterministic or probabilistic. For example, the hard task of Topcoder SRM 595, Constellation, is the probabilistic version of a convex hull.

Let’s try to make a new task. Firstly we will use the following task. There are n people, sort them by their name. It is just an ordinary sorting problem, but we can make it more interesting by adding nondeterministic element. There are n people, each person will use either his/her first name or last name as a handle. Can the lexicographical order of the handles be exactly equal to the given permutation p?

More formally, if we denote the handle of the i-th person as h i, then the following condition must hold: .

Input

The first line contains an integer n (1 ≤ n ≤ 105) — the number of people.

The next n lines each contains two strings. The i-th line contains strings f i and s i (1 ≤ |f i|, |s i| ≤ 50) — the first name and last name of the i-th person. Each string consists only of lowercase English letters. All of the given 2n strings will be distinct.

The next line contains n distinct integers: p 1, p 2, …, p n (1 ≤ p i ≤ n).

Output

If it is possible, output “YES”, otherwise output “NO”.

Examples

input

3
gennady korotkevich
petr mitrichev
gaoyuan chen
1 2 3

output

NO

input

3
gennady korotkevich
petr mitrichev
gaoyuan chen
3 1 2

output

YES

input

2
galileo galilei
nicolaus copernicus
2 1

output

YES

input

10
rean schwarzer
fei claussell
alisa reinford
eliot craig
laura arseid
jusis albarea
machias regnitz
sara valestin
emma millstein
gaius worzel
1 2 3 4 5 6 7 8 9 10

output

NO

input

10
rean schwarzer
fei claussell
alisa reinford
eliot craig
laura arseid
jusis albarea
machias regnitz
sara valestin
emma millstein
gaius worzel
2 4 9 6 5 7 1 3 8 10

output

YES

Note

In example 1 and 2, we have 3 people: tourist, Petr and me (cgy4ever). You can see that whatever handle is chosen, I must be the first, then tourist and Petr must be the last.

In example 3, if Copernicus uses “copernicus” as his handle, everything will be alright.

Solution:

#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <memory.h>
#include <cassert>

using namespace std;

const int N = 200010;

char a[N][55], b[N][55];
int p[N];

int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
    scanf("%s %s", a[i], b[i]);
  }
  for (int i = 0; i < n; i++) {
    scanf("%d", p + i);
    p[i]--;
  }
  string res = "";
  for (int id = 0; id < n; id++) {
    int i = p[id];
    string x = "", y = "";
    for (int j = 0; a[i][j]; j++) x += a[i][j];
    for (int j = 0; b[i][j]; j++) y += b[i][j];
    if (x > y) {
swap(x, y);
}
if (x > res) {
res = x;
continue;
}
if (y > res) {
res = y;
continue;
}
puts("NO");
return 0;
}
puts("YES");
return 0;
}