Slastičarnica
Time limit2sMemory limit1024 MB
Customers arrive in order, each taking d cakes from one end of a row with all sweetness at least s. Find the maximum number of customers that can be served.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Array, Binary search
- Solved
- No attempts yet
Problem
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 gives continuous cakes from the table. To keep the table looking as nice as possible, he only gives cakes from the left or the right edge of the table. He noticed that he cannot serve many customers this way, so before serving a customer he eats some cakes (possibly none) from the edges of the table.
If Dorijan cannot satisfy a customer, he closes the bakery for the day. What is the largest number of customers he can serve before closing?
Input
The first line contains two integers , (, ), the number of cakes and the number of people in the line.
The second line contains numbers (), 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 line.
Output
In the first and only line, output the number of customers Dorijan can serve.
Hint
Clarification of the third example: 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 has no more cakes, so the last customer goes home empty-handed.