#P10123. Earliest Milking Position

    ID: 12110 Type: Default 1000ms 256MiB

Earliest Milking Position

Earliest Milking Position

Farmer John's N cows have developed a complex social structure based on two key features:

  • Social Hierarchy: Certain cows must be milked before others in a fixed order. For example, if cow 3 has the highest status, cow 1 is next, and cow 4 is last, then they must be milked in the order 3, then 1, then 4.
  • Fixed Position Requirements: Some cows insist on being milked at a specific position in the order. For example, cow 4 may demand to be milked second.

Farmer John is always able to find an order that satisfies these constraints. Unfortunately, cow 1 has recently fallen ill so he wants to milk her as early as possible so she can get some rest. Given the social hierarchy and fixed position requirements, determine the earliest position in the milking order at which cow 1 can appear.

The input guarantees that a valid milking order exists. Note that if cow 1 already has a fixed position, that position is the answer.

inputFormat

The first line contains three integers N, M, and K where:

  • N (2 ≤ N ≤ 100) is the total number of cows.
  • M (1 ≤ M ≤ N) is the number of cows in the social hierarchy list.
  • K (0 ≤ K ≤ N) is the number of cows with fixed milking positions.

The second line contains M distinct integers representing the social hierarchy order. This means that the cows must appear in the final order in the same relative order as they appear in this list (i.e. a cow earlier in the list must be milked before a cow later in the list).

Each of the following K lines contains two integers: cow and position, meaning that this cow must be milked exactly in that position. The positions are numbered 1 to N and are all distinct.

outputFormat

Output a single integer representing the earliest possible position at which cow 1 can be milked while satisfying all the constraints.

sample

5 3 2
3 1 4
4 5
3 1
2

</p>