## COCI '22 Contest 5 #4 Slastičarnica

View as PDF

Points: 15 (partial)
Time limit: 2.0s
Memory limit: 512M

Problem type

There is a new bakery in town! Come and try delicious cakes from Dodo's bakery!

The bakers have prepared cakes, the -th of which has sweetness . They are displayed on a long table in the given order. There are people waiting in line to order the best cakes in town. Each of them has an order of the form: "I would like to buy cakes whose sweetnesses are at least ".

Dorijan serves customers in a peculiar way. He will give continuous cakes from the table. To keep the table looking as nice as possible, he will only give cakes from the left or the right end of the table. He noticed that he cannot serve a lot of customers that way, so before serving a customer, he will eat some cakes (possibly none) from the ends of the table.

If Dorijan cannot satisfy a customer, he will close the bakery for the day. What is the largest number of customers he can serve before closing?

#### Input Specification

The first line contains two integers , , the number of cakes and the number of people in the line.

The second line contains integers , where is the sweetness of the -th cake.

In each of the next lines there are two integers , , the order of the -th customer in the line.

#### Output Specification

In the first and only line, output the number of customers Dorijan can serve.

1 21
2 31

#### Sample Input 1

5 6
1 2 3 4 5
1 1
1 2
1 3
1 4
1 6
1 5

#### Sample Output 1

4

#### Sample Input 2

5 3
1 3 2 2 1
3 1
1 3
2 2

#### Sample Output 2

2

#### Sample Input 3

9 5
1 3 2 5 1 4 6 2 1
3 2
2 3
1 1
1 2
1 1

#### Sample Output 3

4

#### Explanation for Sample 3

Dorijan first eats one cake from the left, then gives the next three cakes with sweetnesses , , and to the first customer. He then eats another cake from the left and gives the next two cakes with sweetnesses and to the second customer. The third customer gets a cake from the right with sweetness and the fourth gets the last cake with sweetness . Dorijan unfortunately does not have more cakes, so the last customer goes home empty-handed.