NOI Contest Archive

Archive / 2020 / Final Round — Day 2

Asteroid Belt

Medium · National Olympiad in Informatics Sri Lanka 2020 - Day 2

There are a bunch of asteroids in a belt. For simplicity let’s assume the asteroids are in a straight-line from left to right. You want to explore these asteroids. You can start at any asteroid and finish at any asteroid. However you can only travel towards the right. You want to visit and explore as many asteroids as possible.

There is another problem. Your tiny ship can only jump from an asteroid to another if the second asteroid has a larger diameter than the first. However, from some exceptional asteroids you can jump to either an asteroid with a larger diameter or to one or more specified asteroids.

You are given the diameters of the asteroids and the exceptions. You need to compute the maximum number of asteroids you can visit.

Input Format

First line contains N and E. N is the number of asteroids and E is the number of exceptions.
The second line contains N positive integers representing the diameters of the N asteroids from left to right order.
The following E lines contain two pairs of integers (Xi, Yi) each, giving the exception.
This means that you can jump from the asteroid numbered Xi to asteroid Yi regardless of their diameters. The asteroids are numbered from 0 to N-1 from left to right.
Xi will always be less than Yi (Xi < Yi).

Output Format

Output a single number indicating the maximum number of asteroids you can visit.

Constraints

Limits

Sample Input 0

10 2
1 2 3 6 4 7 1 4 5 6
2 5
4 6

Sample Output 0

8