alexjercan/bugnet huggingface dataset:
Problem p03033 (Python) — buggy submission, verdict: Time Limit Exceeded

from operator import itemgetter
import sys

input = sys.stdin.readline

inf = float("inf")

N, Q = map(int, input().split())

events = []

for _ in range(N):
    s, t, x = map(int, input().split())
    add_event = (s - x, 1, x)
    remove_event = (t - x, -1, x)
    events.append(add_event)
    events.append(remove_event)

events.sort(key=itemgetter(0))  # イベント処理時刻昇順ソート

under_construction = set()

min_stop = inf
chg_flg = False

cur = 0
for _ in range(Q):
    d = int(input())
    while cur < N * 2 and events[cur][0] <= d:
        exec_time, event_type, under_construction_x = events[cur]
        if event_type == 1:  # add_event
            under_construction.add(under_construction_x)
            if not chg_flg:
                min_stop = min(min_stop, under_construction_x)
        else:  # remove_event
            under_construction.discard(under_construction_x)
            if not chg_flg and min_stop == under_construction_x:
                chg_flg = True
        cur += 1

    if chg_flg:
        min_stop = min(under_construction) if under_construction else inf
        chg_flg = False
    print(min_stop if min_stop != inf else -1)


Error: TLE

Fixed submission:

from operator import itemgetter
import sys

input = sys.stdin.readline

N, Q = map(int, input().split())

events = []

for _ in range(N):
    s, t, x = map(int, input().split())
    add_event = (s - x, 1, x)
    remove_event = (t - x, -1, x)
    events.append(add_event)
    events.append(remove_event)

events.sort(key=itemgetter(0))  # イベント処理時刻昇順ソート

under_construction = set()

cur = 0
for _ in range(Q):
    d = int(input())
    # print('d', d)
    while cur < N * 2 and events[cur][0] <= d:
        exec_time, event_type, under_construction_x = events[cur]
        # print(cur, exec_time, event_type, under_construction_x)
        if event_type == 1:
            # add_event
            under_construction.add(under_construction_x)
        else:
            # remove_event
            under_construction.discard(under_construction_x)
        cur += 1
    # print('cur', cur, 'events', events)
    # print('uc', under_construction)
    print(min(under_construction) if under_construction else -1)

    # print()
Problem p03034 (Python) — buggy submission, verdict: Runtime Error

n = int(input())
sss = list(map(int, input().split()))

ans = 0
for c in range(1, n // 2):
    a = n - 1 - c
    k = c
    tmp = 0
    while a > c and a != k and a + c != k:
        tmp += sss[a] + sss[k]
        ans = max(ans, tmp)
        a -= c
        k += c
print(ans)


Error: TLE

Fixed submission:

import numpy as np


def score(cache, sss, a, ab, d):
    mat = cache[ab]
    if mat is None:
        mat = cache[ab] = np.cumsum(np.resize(sss, ((n - 1) // ab + 1, ab)).T, axis=1)
    mat_a = mat[a % ab]
    return mat[0][d] + mat_a[-1] - mat_a[-d - 2]


n = int(input())
sss = list(map(int, input().split()))
sss.extend([0] * n)
sss = np.array(sss, dtype=np.int64)

ans = 0
cache = [None] * n
for ab in range(1, int((n - 2) ** 0.5) + 1):
    for remain in range(ab, n - 3, ab):
        a = n - remain - 1
        d = remain // ab
        if remain < a:
            ans = max(ans, score(cache, sss, a, ab, d))
            if ab != d:
                ans = max(ans, score(cache, sss, a, d, ab))
        else:
            if a % ab > 0:
                ans = max(ans, score(cache, sss, a, ab, d))
            if a > d and ab != d and a % d > 0:
                ans = max(ans, score(cache, sss, a, d, ab))
print(ans)
Problem p03035 (Python) — buggy submission, verdict: Runtime Error

A, B = map(int, input().split())

if A >= 13:
    print(B)
elif A >= 6:
    print(B // 2)
else:
    print(0)


Error: ValueError: invalid literal for int() with base 10: ' '
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03035/Python/s602871516.py", line 1, in <module>
    A, B = map(int, input())
ValueError: invalid literal for int() with base 10: ' '


Fixed submission:

A, B = map(int, input())

if A >= 13:
    print(B)
elif A >= 6:
    print(B // 2)
else:
    print(0)
Problem p03037 (Python) — buggy submission, verdict: Time Limit Exceeded

def main():
    N, M = map(int, input().split())
    L = 0
    R = N
    for _ in range(M):
        l, r = map(int, input().split())
        if L < l:
            L = l
        if R > r:
            R = r
    ans = 0
    if L <= R:
        ans = R - L + 1
    print(ans)


if __name__ == "__main__":
    main()


Error: TLE

Fixed submission:

import itertools
from collections import Counter


def main():
    N, M = map(int, input().split())
    LR = []
    for _ in range(M):
        L, R = map(int, input().split())
        LR.append(list(range(L, R + 1)))
    LR = list(itertools.chain.from_iterable(LR))
    C = Counter(LR)

    ans = 0
    for c in C.values():
        if c == M:
            ans += 1
    print(ans)


if __name__ == "__main__":
    main()
Problem p03037 (Python) — buggy submission, verdict: Time Limit Exceeded

N, M = map(int, input().split())
LR = [list(map(int, input().split())) for _ in range(M)]

A, B = 1, N
for lr in LR:
    L, R = lr
    if R < A or B < L:
        print(0)
        exit()

    if A <= L <= R <= B:
        A, B = L, R
    elif L <= A <= R <= B:
        B = R
    elif A <= L <= B <= R:
        A = L
print(B - A + 1)


Error: TLE

Fixed submission:

N, M = map(int, input().split())
LR = [list(map(int, input().split())) for _ in range(M)]

ans = set(list(range(1, N + 1)))
for lr in LR:
    l, r = lr
    ans = ans & set(list(range(l, r + 1)))
print(len(list(ans)))
Problem p03037 (Python) — buggy submission, verdict: Time Limit Exceeded

# C - Prison

N, M = map(int, input().split())
left = 0
right = 10**5
for _ in range(M):
    l, r = map(int, input().split())
    left, right = max(left, l), min(right, r)

print(len([i for i in range(left, right + 1)]) if left <= right else 0)


Error: TLE

Fixed submission:

# C - Prison

N, M = map(int, input().split())
ls = [i for i in range(1, N + 1)]
for _ in range(M):
    l, r = map(int, input().split())
    ls = set(ls) & set([i for i in range(l, r + 1)])

print(len(ls))
Problem p03037 (Python) — buggy submission, verdict: Time Limit Exceeded

from sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    n, m = map(int, input().split())
    l_max = 0
    r_min = float("inf")
    for _ in range(m):
        l, r = map(int, input().split())
        l_max = max(l_max, l)
        r_min = min(r_min, r)
    print(max(r_min + 1 - l_max, 0))


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()


Error: TLE

Fixed submission:

from sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    n, m = map(int, input().split())
    counts = [0] * n
    for _ in range(m):
        l, r = map(int, input().split())
        for i in range(l - 1, r):
            counts[i] += 1
    print(counts.count(m))


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()
Problem p03037 (Python) — buggy submission, verdict: Time Limit Exceeded

N, M = list(map(int, input().split()))

maxvL = 0
minvR = 1000000
for i in range(M):
    L, R = list(map(int, input().split()))
    if maxvL < L:
        maxvL = L
    if minvR > R:
        minvR = R

count = 0
for i in range(N):
    if maxvL <= i + 1 <= minvR:
        count += 1
print(count)


Error: TLE

Fixed submission:

N, M = list(map(int, input().split()))

Ls = [0] * M
Rs = [0] * M

for i in range(M):
    Ls[i], Rs[i] = list(map(int, input().split()))

count = 0
for i in range(N):
    judge = True
    for k in range(M):
        L = Ls[k]
        R = Rs[k]
        if not (L <= i + 1 <= R):
            judge = False
    if judge:
        count += 1
print(count)
Problem p03037 (Python) — buggy submission, verdict: Time Limit Exceeded

n, m = map(int, input().split())

li_w = list()

for _ in range(m):
    li_w.append(tuple(map(int, input().split())))

w_l = 0
w_r = 999999

for w in li_w:
    w_l = max(w_l, w[0])
    w_r = min(w_r, w[1])


if w_r >= w_l:
    print(w_r - w_l + 1)
else:
    print(0)


Error: TLE

Fixed submission:

n, m = map(int, input().split())

li_w = list()

for _ in range(m):
    li_w.append(tuple(map(int, input().split())))

answer = set()

for idx, w in enumerate(li_w):
    if idx == 0:
        answer = set(range(w[0], w[1] + 1))
    else:
        answer = answer.intersection(range(w[0], w[1] + 1))

print(len(answer))
Problem p03037 (Python) — buggy submission, verdict: Time Limit Exceeded

def main():
    N, M = map(int, input().split())
    A = [0] * (N + 1)
    for _ in range(M):
        L, R = map(int, input().split())
        A[L - 1] += 1
        A[R] -= 1
    c = 0
    ans = 0
    for i, a in enumerate(A):
        c += a
        if c == M:
            ans += 1
    print(ans)


main()


Error: TLE

Fixed submission:

def main():
    N, M = map(int, input().split())
    A = [0] * N
    for _ in range(M):
        L, R = map(int, input().split())
        for i in range(L - 1, R):
            A[i] += 1
    print(A.count(M))


main()
Problem p03037 (Python) — buggy submission, verdict: Time Limit Exceeded

n, m = map(int, input().split())
Authority = [list(map(int, input().split())) for _ in range(m)]
cards = list(range(1, n + 2))
l_list = []
r_list = []
for i in range(m):
    l_list.append(Authority[i][0])
    r_list.append(Authority[i][1])

print(len(cards[max(l_list) : min(r_list) + 1]))


Error: TLE

Fixed submission:

n, m = map(int, input().split())
Authority = [list(map(int, input().split())) for _ in range(m)]
cards = set(range(1, n + 1))

for i in range(m):
    gate = set(range(Authority[i][0], Authority[i][1] + 1))
    cards &= gate
print(len(cards))
Problem p03037 (Python) — buggy submission, verdict: Runtime Error

from sys import exit

N, M = map(int, input().split())

L = []
R = []

for i in range(M):
    l, r = map(int, input().split())
    L.append(l)
    R.append(r)

l_max = max(L)
r_min = min(R)

if (r_min - l_max) < 0:
    print(0)
else:
    print(r_min - l_max + 1)


Error: 0

Fixed submission:

from sys import exit

N, M = map(int, input().split())

ans = []

for i in range(M):
    L, R = map(int, input().split())

    if i == 0:
        for card in range(L, R + 1):
            ans.append(card)
        continue

    if ans[0] < L:
        for card in range(ans[0], L):
            ans.remove(card)

    if R < ans[-1]:
        for card in range(R + 1, ans[-1] + 1):
            ans.remove(card)

    if not ans:
        print(0)
        exit()

print(len(ans))
Problem p03037 (Python) — buggy submission, verdict: Time Limit Exceeded

(n, m) = map(int, input().split())


def intersect(lhs, rhs):
    if lhs[1] < rhs[0] or rhs[1] < lhs[0]:
        return [0, 0]
    min_value = lhs[0] if lhs[0] > rhs[0] else rhs[0]
    max_value = lhs[1] if lhs[1] < rhs[1] else rhs[1]
    return [min_value, max_value]


ans = [-1, 2 * 10**5]
for _ in range(m):
    ans = intersect(ans, list(map(int, input().split())))
    if 0 == ans[0] and 0 == ans[1]:
        print(0)
        exit(0)
print(ans[1] - ans[0] + 1)


Error: TLE

Fixed submission:

(n, m) = map(int, input().split())
counts = [0 for _ in range(n)]
for _ in range(m):
    gate_i = list(map(int, input().split()))
    for i in range(gate_i[0] - 1, gate_i[1]):
        counts[i] += 1
counts.sort()
ans = 0
for count in counts:
    ans += 1 if m == count else 0
print(ans)
Problem p03037 (Python) — buggy submission, verdict: Time Limit Exceeded

def main():
    n, m = map(int, input().split())
    lr_lst = [list(map(int, input().split())) for _ in range(m)]
    left = 0
    right = n + 1

    for i in range(m):
        if left < lr_lst[i][0]:
            left = lr_lst[i][0]
        if lr_lst[i][1] < right:
            right = lr_lst[i][1]

    if right - left < 0:
        print(0)
    else:
        print(right - left + 1)


if __name__ == "__main__":
    main()


Error: TLE

Fixed submission:

def main():
    n, m = map(int, input().split())
    lr_lst = [list(map(int, input().split())) for _ in range(m)]

    ans = 0

    for i in range(1, n + 1):
        is_able = True
        for j in range(m):
            if not lr_lst[j][0] <= i <= lr_lst[j][1]:
                is_able = False
                break
        if is_able:
            ans += 1

    print(ans)


if __name__ == "__main__":
    main()
Problem p03037 (Python) — buggy submission, verdict: Time Limit Exceeded

N, M = map(int, input().split())
L = [0] * M
R = [0] * M
count = 0
for i in range(M):
    L[i], R[i] = map(int, input().split())
Lmax = max(L)
Rmin = min(R)
if Lmax <= Rmin:
    print(Rmin - Lmax + 1)
else:
    print(0)


Error: TLE

Fixed submission:

N, M = map(int, input().split())
L = [0] * M
R = [0] * M
count = 0
for i in range(M):
    L[i], R[i] = map(int, input().split())
for i in range(N):
    clear = 0
    for j in range(M):
        if L[j] <= i + 1 <= R[j]:
            clear += 1
        else:
            break
    if clear == M:
        count += 1
print(count)
Problem p03037 (Python) — buggy submission, verdict: Time Limit Exceeded

import sys

input = sys.stdin.readline

n, m = map(int, input().split())

l_list = []
r_list = []

for _ in range(m):
    l, r = map(int, input().split())
    l_list.append(l)
    r_list.append(r)

l_maybe_answer = max(l_list)
r_maybe_answer = min(r_list)

if l_maybe_answer <= r_maybe_answer:
    print(min(r_list) - max(l_list) + 1)
else:
    print(0)


Error: TLE

Fixed submission:

import sys

input = sys.stdin.readline

n, m = map(int, input().split())

l_first, r_first = map(int, input().split())

answer_set = {value for value in range(l_first, r_first + 1)}

for _ in range(m - 1):
    l_current, r_current = map(int, input().split())
    current_set = {value for value in range(l_current, r_current + 1)}
    answer_set.intersection_update(current_set)

print(len(answer_set))
Problem p03037 (Python) — buggy submission, verdict: Time Limit Exceeded

def solve2():
    n, m = map(int, input().split())
    L = 1
    R = n
    for _ in range(m):
        l, r = map(int, input().split())
        L = max(L, l)
        R = min(R, r)
    if L <= R:
        print(R - L + 1)
    else:
        print(0)


solve2()


Error: TLE

Fixed submission:

n, m = map(int, input().split())
ans = set(range(1, n + 1))
for _ in range(m):
    l, r = map(int, input().split())
    st = set(range(l, r + 1))
    ans &= st
print(len(ans))
Problem p03037 (Python) — buggy submission, verdict: Time Limit Exceeded

n, m = map(int, input().split())

left = []
right = []
for i in range(m):
    a, b = map(int, input().split())
    left.append(a)
    right.append(b)

if min(right) >= max(left):
    print(min(right) - max(left) + 1)
else:
    print(0)


Error: TLE

Fixed submission:

n, m = map(int, input().split())
a = [list(map(int, input().split())) for i in range(m)]

if len(a) == 1:
    print(a[0][1] - a[0][0] + 1)
else:
    ans = n
    for i in range(1, n + 1):
        for j in range(m):
            if a[j][0] <= i <= a[j][1]:
                continue
            else:
                ans -= 1
                break
    print(ans)
Problem p03037 (Python) — buggy submission, verdict: Runtime Error

N, M = map(int, input().split())
table = [[int(i) for i in input().split()] for _ in range(M)]

ans = [table[0][0], table[0][1]]
cnt = 0

for i in table[1:]:
    if ans[0] <= i[0] <= ans[1]:
        ans[0] = i[0]
    if ans[0] <= i[1] <= ans[1]:
        ans[1] = i[1]
    if ans[1] < i[0] or i[1] < ans[0]:
        cnt += 1
        break

if cnt != 0:
    print(0)
else:
    print(ans[1] - ans[0] + 1)


Error: TypeError: '>=' not supported between instances of 'list' and 'int'
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03037/Python/s777390978.py", line 7, in <module>
    if ans[0] >= i[0] <= ans[1]:
TypeError: '>=' not supported between instances of 'list' and 'int'


Fixed submission:

N, M = map(int, input().split())
table = [[int(i) for i in input().split()] for _ in range(M)]

ans = [table[0], table[1]]

for i in table:
    if ans[0] >= i[0] <= ans[1]:
        ans[0] = i[0]
    if ans[0] >= i[1] <= ans[1]:
        ans[1] = i[1]

print(ans[1] - ans[0] + 1)
Problem p03037 (Python) — buggy submission, verdict: Time Limit Exceeded

#!/usr/bin/env python3
import sys

N, M = map(int, input().rstrip().split())

L = []
R = []
for _ in range(M):
    l, r = map(int, input().rstrip().split())
    L.append(l)
    R.append(r)

Ans = min(R) - max(L) + 1
if Ans < 0:
    Ans = 0
print(Ans)


Error: TLE

Fixed submission:

#!/usr/bin/env python3
import sys

N, M = map(int, input().rstrip().split())

L = []
R = []
for _ in range(M):
    l, r = map(int, input().rstrip().split())
    L.append(l)
    R.append(r)

Ans = set(range(L[0], R[0] + 1))
for i in range(1, len(L)):
    Ans = set(range(L[i], R[i] + 1)) & Ans
    if not Ans:
        print(0)
        sys.exit(0)

print(len(Ans))
Problem p03037 (Python) — buggy submission, verdict: Time Limit Exceeded

N, M = map(int, input().split())

L, R = 1, N
for _ in range(M):
    Li, Ri = map(int, input().split())
    if L <= Li:
        L = Li
    if Ri <= R:
        R = Ri

print(R - L + 1 if (R >= L) else 0)


Error: TLE

Fixed submission:

N, M = map(int, input().split())

id_list = [0] * N

for _ in range(M):
    L, R = map(int, input().split())
    for i in range(L, R + 1):
        id_list[i - 1] += 1

print(sorted(id_list, reverse=True).count(M))
Problem p03037 (Python) — buggy submission, verdict: Time Limit Exceeded

n, m = map(int, input().split())
mi, ma = 1, n
for _ in range(m):
    l, r = map(int, input().split())
    mi = max(l, mi)
    ma = min(r, ma)
print(max(0, ma - mi + 1))


Error: TLE

Fixed submission:

n, m = map(int, input().split())
lr = [list(map(int, input().split())) for _ in range(m)]
ans = []
for i in range(m):
    res = []
    for j in range(lr[i][0], lr[i][1] + 1):
        res.append(j)
    if i == 0:
        ans = set(res)
    else:
        ans = set(ans) & set(res)
print(len(ans))
Problem p03037 (Python) — buggy submission, verdict: Time Limit Exceeded

N, M = map(int, input().split())
LR = [list(map(int, input().split())) for _ in range(M)]

left = 1
right = N

for l, r in LR:
    left = max(left, l)
    right = min(right, r)

ans = right - left

print(ans + 1 if ans >= 0 else 0)


Error: TLE

Fixed submission:

N, M = map(int, input().split())
LR = [list(map(int, input().split())) for _ in range(M)]

c = [0 for _ in range(N)]
cnt = 0
for i, (l, r) in enumerate(LR):
    for j in range(l - 1, r):
        c[j] += i + 1
    cnt += i + 1

s = [1 if k == cnt else 0 for k in c]

print(sum(s))
Problem p03037 (Python) — buggy submission, verdict: Time Limit Exceeded

N, M = map(int, input().split())
A = []
for i in range(M):
    L, R = map(int, input().split())
    if i == 0:
        Lmax = L
        Rmin = R
    else:
        Lmax = max(L, Lmax)
        Rmin = min(R, Rmin)

# L = set(A[0])
# for i in range(1, M):
#     L = L & set(A[i])

if Rmin - Lmax < 0:
    print(0)
else:
    print(Rmin - Lmax + 1)


Error: TLE

Fixed submission:

N, M = map(int, input().split())
A = []
for i in range(M):
    L, R = map(int, input().split())
    if i == 0:
        S = set(list(range(L, R + 1)))
    else:
        S = S & set(list(range(L, R + 1)))
        if len(S) == 0:
            break

# L = set(A[0])
# for i in range(1, M):
#     L = L & set(A[i])

print(len(S))
Problem p03038 (Python) — buggy submission, verdict: Runtime Error

N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [list(map(int, input().split())) for i in range(M)]
BC = sorted(BC, key=lambda x: x[1])
A.sort()
i = 0
for b, c in BC[::-1]:
    for _ in range(b):
        if i == N:
            break
        if c > A[i]:
            A[i] = c
            i += 1
        else:
            break

print(sum(A))


Error: 0

Fixed submission:

N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [list(map(int, input().split())) for i in range(M)]
BC = sorted(BC, key=lambda x: x[1])
A.sort()
i = 0
for b, c in BC[::-1]:
    if i == N:
        break
    for _ in range(b):
        if c > A[i]:
            A[i] = c
            i += 1
        else:
            break

print(sum(A))
Problem p03038 (Python) — buggy submission, verdict: Time Limit Exceeded

n, m = map(int, input().split())
A = list(map(int, input().split()))
d = {}
for a in A:
    d[a] = 1 if a not in d else d[a] + 1
for _ in range(m):
    b, c = map(int, input().split())
    d[c] = b if c not in d else d[c] + b
ans = 0
cnt = 0
L = sorted(d, reverse=True)
for k in L:
    if d[k] + cnt < n:
        ans += k * d[k]
        cnt += d[k]
    else:
        ans += k * (n - cnt)
        break
print(ans)


Error: TLE

Fixed submission:

import heapq

n, m = map(int, input().split())
a = list(map(int, input().split()))
heapq.heapify(a)
for _ in range(m):
    b, c = map(int, input().split())
    for _ in range(b):
        tmp = heapq.heappop(a)
        if tmp >= c:
            heapq.heappush(a, tmp)
            break
        else:
            heapq.heappush(a, c)
print(sum(a))
Problem p03038 (Python) — buggy submission, verdict: Time Limit Exceeded

import sys

input = sys.stdin.readline

N, M = map(int, input().split())
A = list(map(int, input().split()))
op = [list(map(int, input().split())) for _ in range(M)]

cards = sorted(A)
op.sort(key=lambda x: -x[1])

i = 0

for m in range(M):
    b, c = op[m]
    for _ in range(b):
        if cards[i] < c:
            cards[i] = c
            i += 1
            if i >= N:
                break
        else:
            break
    else:
        continue
    break

print(sum(cards))


Error: TLE

Fixed submission:

import sys

input = sys.stdin.readline

N, M = map(int, input().split())
A = list(map(int, input().split()))
op = [list(map(int, input().split())) for _ in range(M)]

cards = sorted(A)
op.sort(key=lambda x: -x[1])

i = 0

for m in range(M):
    b, c = op[m]
    for _ in range(b):
        if cards[i] < c:
            cards[i] = c
            i += 1
        if i >= N:
            break
    else:
        continue
    break

print(sum(cards))
Problem p03038 (Python) — buggy submission, verdict: Runtime Error

N, M = map(int, input().split())
A = sorted(list(map(int, input().split())))
BC = [list(map(int, input().split())) for _ in range(M)]

# n番目の要素でソートsort
BC = sorted(BC, reverse=True, key=lambda x: x[1])

m = A[0]
D = []
for b, c in BC:
    if c > m:
        _ = [D.append(c) for i in range(b)]
    else:
        continue
    if len(D) > N:
        break

D.append(0)

for i, d in enumerate(D):
    if i >= N:
        break
    if A[i] < d:
        A[i] = d
    else:
        break

print(sum(A))


Error: 0

Fixed submission:

N, M = map(int, input().split())
A = sorted(list(map(int, input().split())))
BC = [list(map(int, input().split())) for _ in range(M)]

# n番目の要素でソートsort
BC = sorted(BC, reverse=True, key=lambda x: x[1])

m = A[0]
D = []
p = 0
for b, c in BC:
    if c > m:
        _ = [D.append(c) for i in range(b)]
        p += 1
        if p > N:
            break
    else:
        continue

D.append(0)

for i, d in enumerate(D):
    if i > N:
        break
    if A[i] < d:
        A[i] = d
    else:
        break

print(sum(A))