inputs: Solve in Python:
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 \le n, m \le 1000$) — 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, \dots, k_n$ ($0 \le k_i \le 1000$), 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 $1000$.

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 \le d_j \le 1000, 1 \le t_j \le n$) and means that microtransactions of the $t_j$-th type are on sale during the $d_j$-th day.


-----Output-----

Print one...
targets: import sys
import copy

DEBUG = False

if DEBUG:
    inf = open("input.txt")
else:
    inf = sys.stdin

N, M = list(map(int, inf.readline().split(' ')))
n_items = list(map(int, inf.readline().split(' ')))
sales = []
for _ in range(M):
    sale = list(map(int, inf.readline().split(' ')))
    sales.append(sale) # sale_day, sale_type

sales = sorted(sales, key=lambda x: x[0], reverse=True) # sort by day

def can_buy_in(dday):
    used = 0
    money_left = dday
    items = copy.deepcopy(n_items)
    for sale_day, sale_type in sales:
        if sale_day > dday:
            continue
        
        if money_left > sale_day:
            money_left = sale_day
        can_buy = min(items[sale_type-1], money_left)
        # buy it
        used += can_buy
        items[sale_type-1] -= can_buy
        money_left -= can_buy

        if money_left == 0:
            break
    
    need_money_for_rest = sum(items) * 2
    return need_money_for_rest + used <= dday

total_items = sum(n_items)
low = total_items
high = total_items * 2

# find minimum can_buy day
while low <= high:
    mid = (low + high) // 2
    if can_buy_in(mid):
        high = mid-1
    else:
        low = mid+1

print(low)