Bob has a sequence consisting of
integers, denoted as:
.
Bob doesn't like sequences that stay the same, so he's planning to perform update operations. Each update is described by a pair of integers
, meaning Bob will change
to
during that operation.
However, all updates are hypothetical - they are applied independently and always refer to the original sequence .
Bob is fascinated by non-decreasing subsequences. He wants to find a subsequence of the original sequence that satisfies the following condition. The subsequence must remain non-decreasing in the original sequence and after each of the updates.
Write a program to compute the maximum possible length of such a persistent non-decreasing subsequence.
Input Specification
The first line of input contains two integers and
(
), indicating the length of the sequence and the number of update operations, respectively.
The second line of input contains integers,
(
), the initial sequence.
Each of the following lines contains two integers
and
(
and
), indicating an update operation to change
to
.
Output Specification
Output one integer, the length of the longest subsequence which is non-decreasing in the initial sequence and also non-decreasing after each update operation.
Constraints
Subtask | Points | Additional constraints |
---|---|---|
No additional constraints. |
Sample Input
4 5
1 2 3 5
1 2
2 3
2 1
3 4
4 1
Sample Output
3
Explanation for Sample
The original sequence and the sequence after each update are listed as follows:
Bob can choose the first three integers in his subsequence, which is non-decreasing for the original sequence and also for the sequence after each update.
Comments