Under construction

Still being built. Things may change

Slastičarnica

Time limit2sMemory limit1024 MB

Summary
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 nn cakes, the ii-th of which has sweetness aia_i. They are displayed on a long table in the given order. There are qq 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 did_i cakes whose sweetnesses are at least sis_i".

Dorijan serves customers in a peculiar way. He gives did_i 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 nn, qq (1≤n≤50001 \le n \le 5000, 1≤q≤2⋅1051 \le q \le 2 \cdot 10^5), the number of cakes and the number of people in the line.

The second line contains nn numbers aia_i (1≤ai≤1091 \le a_i \le 10^9), where aia_i is the sweetness of the ii-th cake.

In each of the next qq lines there are two integers did_i, sis_i (1≤di≤n1 \le d_i \le n, 1≤si≤1091 \le s_i \le 10^9), the order of the ii-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 33, 22, and 55 to the first customer. He then eats another cake from the left and gives the next two cakes with sweetnesses 44 and 66 to the second customer. The third customer gets a cake from the right with sweetness 11, and the fourth gets the last cake with sweetness 22. Dorijan has no more cakes, so the last customer goes home empty-handed.

Examples3

  1. Example 1

    Input
    5 6
    1 2 3 4 5
    1 1
    1 2
    1 3
    1 4
    1 6
    1 5
    
    Expected output
    4
    
  2. Example 2

    Input
    5 3
    1 3 2 2 1
    3 1
    1 3
    2 2
    
    Expected output
    2
    
  3. Example 3

    Input
    9 5
    1 3 2 5 1 4 6 2 1
    3 2
    2 3
    1 1
    1 2
    1 1
    
    
    Expected output
    4