slasticarnica (COCI CONTEST_#5)
Time limit: 2 seconds | Memory limit: 512 MB | Task type: batch
Algorithms: segment tree, range queries, binary search, dynamic programming

# Task Slastičarnica

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

The bakers have prepared *n* cakes, the *i*-th of which has sweetness *a<sub>i</sub>*. They are displayed on a long table in the given order. There are *q* 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 *d<sub>i</sub>* cakes whose sweetnesses are at least *s<sub>i</sub>*".

Dorijan serves customers in a peculiar way. He will give *d<sub>i</sub>* 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 edge 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 edges 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

The first line contains two integers *n* and *q* (1 ≤ *n* ≤ 5000, 1 ≤ *q* ≤ 2 ⋅ 10<sup>5</sup>), the number of cakes and the number of people in the line.

The second line contains *n* numbers *a<sub>i</sub>* (1 ≤ *a<sub>i</sub>* ≤ 10<sup>9</sup>), where *a<sub>i</sub>* is the sweetness of the *i*-th cake.

In each of the next *q* lines, there are two integers *d<sub>i</sub>* and *s<sub>i</sub>* (1 ≤ *d<sub>i</sub>* ≤ *n*, 1 ≤ *s<sub>i</sub>* ≤ 10<sup>9</sup>), the order of the *i*-th customer in line.

## Output

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

## Scoring

| Subtask | Points | Constraints                     |
| :------ | :----- | :------------------------------ |
| 1       | 21     | *n*, *q* ≤ 100                 |
| 2       | 31     | *d*<sub>1</sub> = *d*<sub>2</sub> = ... = *d<sub>q</sub>* = 1 |
| 3       | 58     | No additional constraints.      |

## Examples

**Example 1:**

```
input
5 6
1 2 3 4 5
1 1
1 2
1 3
1 4
1 6
1 5
output
4
```

**Example 2:**

```
input
5 3
1 3 2 2 1
3 1
1 3
2 2
output
2
```

**Example 3:**

```
input
9 5
1 3 2 5 1 4 6 2 1
3 2
2 3
1 1
1 2
1 1
output
4
```

#### Clarification of the third example:

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

Starter code:

Grader code:

Setup script:

Evaluation script: