1165_F2. Microtransactions (hard version)
The only difference between easy and hard versions is constraints.

Ivan plays a computer game that contains some microtransactions to make characters look cooler. Since Ivan wants his character to be really cool, he wants to use some of these microtransactions — and he won't start playing until he gets all of them.

Each day (during the morning) Ivan earns exactly one burle.

There are n types of microtransactions in the game. Each microtransaction costs 2 burles usually and 1 burle if it is on sale. Ivan has to order exactly k_i microtransactions of the i-th type (he orders microtransactions during the evening).

Ivan can order any (possibly zero) number of microtransactions of any types during any day (of course, if he has enough money to do it). If the microtransaction he wants to order is on sale then he can buy it for 1 burle and otherwise he can buy it for 2 burles.

There are also m special offers in the game shop. The j-th offer (d_j, t_j) means that microtransactions of the t_j-th type are on sale during the d_j-th day.

Ivan wants to order all microtransactions as soon as possible. Your task is to calculate the minimum day when he can buy all microtransactions he want and actually start playing.

Input

The first line of the input contains two integers n and m (1 ≤ n, m ≤ 2 ⋅ 10^5) — the number of types of microtransactions and the number of special offers in the game shop.

The second line of the input contains n integers k_1, k_2, ..., k_n (0 ≤ k_i ≤ 2 ⋅ 10^5), where k_i is the number of copies of microtransaction of the i-th type Ivan has to order. It is guaranteed that sum of all k_i is not less than 1 and not greater than 2 ⋅ 10^5.

The next m lines contain special offers. The j-th of these lines contains the j-th special offer. It is given as a pair of integers (d_j, t_j) (1 ≤ d_j ≤ 2 ⋅ 10^5, 1 ≤ t_j ≤ n) and means that microtransactions of the t_j-th type are on sale during the d_j-th day.

Output

Print one integer — the minimum day when Ivan can order all microtransactions he wants and actually start playing.

Examples

Input


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


Output


8


Input


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


Output


20
```python
import math
from collections import defaultdict
import sys
#input = sys.stdin.readline


def main():
    n, m = map(int, input().split())
    k = list(map(int, input().split()))
    sales = [(0, 0)] * m
    for i in range(m):
        a, b = map(int, input().split())
        sales[i] = (b, a)

    def check(days):
        last_sale = {}
        for sale in sales:
            if sale[1] <= days:
                if sale[0] not in last_sale or sale[1] > last_sale[sale[0]]:
                    last_sale[sale[0]] = sale[1]

        date_last_sales = {}
        for t, d in last_sale.items():
            if d not in date_last_sales:
                date_last_sales[d] = [t]
            else:
                date_last_sales[d].append(t)

        balance = 0
        required = [0] + k.copy()

        end = 0
        for d in range(1, days+1):
            balance += 1
            if d in date_last_sales:
                for t in date_last_sales[d]:
                    if required[t] > 0:
                        if required[t] > balance:
                            end += required[t] - balance
                        balance -= min(required[t], balance)
                        required[t] = 0
            if d == days:  # last day
                for r in required:
                    if r > 0:
                        end += r

        return 2*end <= balance

    total = sum(k)
    hi = 2*total
    lo = 1

    while lo + 1 < hi:
        mid = (lo + hi) // 2
        if check(mid):
            hi = mid
        else:
            lo = mid

    if check(lo):
        print(lo)
    else:
        print(hi)


if __name__ == '__main__':
    main()

```