Buggy python submission (verdict: Time Limit Exceeded):
import bisect
from collections import deque
n, m = map(int, input().split())
A = deque(sorted(map(int, input().split())))
BC = []
for j in range(m):
    b, c = map(int, input().split())
    BC.append((b, c))
BC = sorted(BC, key=lambda x: x[1], reverse=True)
for b, c in BC:
    change = 0
    for i, a in enumerate(A):
        if i == b:
            break
        if a < c:
            change += 1
    for _ in range(change):
        A.popleft()
        A.append(c)
print(sum(A))

Fixed submission:
n, m = map(int, input().split())
A = sorted(map(int, input().split()))
BC = []
for j in range(m):
    b, c = map(int, input().split())
    BC.append((b, c))
BC = sorted(BC, key=lambda x: x[1], reverse=True)
D = []
for b, c in BC:
    D.extend([c for i in range(b)])
    if len(D) >= n:
        D = D[:n]
        break
for x in range(len(D)):
    if A[x] > D[x]:
        break
else:
    x += 1
print(sum(A[x:]) + sum(D[:x]))

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
from collections import deque
n, m = map(int, input().split())
A = deque(sorted(map(int, input().split())))
BC = []
for j in range(m):
    b, c = map(int, input().split())
    BC.append((b, c))
BC = sorted(BC, key=lambda x: x[1], reverse=True)
for b, c in BC:
    i = bisect.bisect_left(A, c)
    if i == 0:
        break
    else:
        start = min(i, b)
        for _ in range(start):
            A.popleft()
            A.append(c)
print(sum(A))

Fixed submission:
n, m = map(int, input().split())
A = sorted(map(int, input().split()))
BC = []
for j in range(m):
    b, c = map(int, input().split())
    BC.append((b, c))
BC = sorted(BC, key=lambda x: x[1], reverse=True)
D = []
for b, c in BC:
    D.extend([c for i in range(b)])
    if len(D) >= n:
        D = D[:n]
        break
for x in range(len(D)):
    if A[x] > D[x]:
        break
else:
    x += 1
print(sum(A[x:]) + sum(D[:x]))

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
from collections import deque
n, m = map(int, input().split())
A = deque(sorted(map(int, input().split())))
BC = []
for j in range(m):
    b, c = map(int, input().split())
    BC.append((b, c))
BC = sorted(BC, key=lambda x: x[1], reverse=True)
for b, c in BC:
    i = bisect.bisect_left(A, c)
    if i == 0:
        break
    else:
        start = min(i, b)
        A.extend([c for j in range(start)])
        for _ in range(start):
            A.popleft()
print(sum(A))

Fixed submission:
n, m = map(int, input().split())
A = sorted(map(int, input().split()))
BC = []
for j in range(m):
    b, c = map(int, input().split())
    BC.append((b, c))
BC = sorted(BC, key=lambda x: x[1], reverse=True)
D = []
for b, c in BC:
    D.extend([c for i in range(b)])
    if len(D) >= n:
        D = D[:n]
        break
for x in range(len(D)):
    if A[x] > D[x]:
        break
else:
    x += 1
print(sum(A[x:]) + sum(D[:x]))

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
n, m = map(int, input().split())
A = sorted(map(int, input().split()), reverse = True)
BC = []
for j in range(m):
    b, c = map(int, input().split())
    BC.append((b, c))
BC = sorted(BC, key=lambda x: x[1], reverse=True)
for b, c in BC:
    i = bisect.bisect_left(A[::-1], c)
    if i == 0:
        break
    else:
        start = min(i, b)
        conect = [c for j in range(start)]
        conect.extend(A)
        for _ in range(start):
            conect.pop()
        A = conect
print(sum(A))

Fixed submission:
n, m = map(int, input().split())
A = sorted(map(int, input().split()))
BC = []
for j in range(m):
    b, c = map(int, input().split())
    BC.append((b, c))
BC = sorted(BC, key=lambda x: x[1], reverse=True)
D = []
for b, c in BC:
    D.extend([c for i in range(b)])
    if len(D) >= n:
        D = D[:n]
        break
for x in range(len(D)):
    if A[x] > D[x]:
        break
else:
    x += 1
print(sum(A[x:]) + sum(D[:x]))

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
n, m = map(int, input().split())
A = sorted(map(int, input().split()))
BC = []
for j in range(m):
    b, c = map(int, input().split())
    BC.append((b, c))
BC = sorted(BC, key=lambda x: x[1], reverse=True)
ans = 0
for b, c in BC:
    i = bisect.bisect_left(A, c)
    if i == 0:
        break
    else:
        change = min(i, b)
        ans += c * change
        A = A[change:]
ans += sum(A)
print(ans)

Fixed submission:
n, m = map(int, input().split())
A = sorted(map(int, input().split()))
BC = []
for j in range(m):
    b, c = map(int, input().split())
    BC.append((b, c))
BC = sorted(BC, key=lambda x: x[1], reverse=True)
D = []
for b, c in BC:
    D.extend([c for i in range(b)])
    if len(D) >= n:
        D = D[:n]
        break
for x in range(len(D)):
    if A[x] > D[x]:
        break
else:
    x += 1
print(sum(A[x:]) + sum(D[:x]))

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
n, m = map(int, input().split())
A = sorted(map(int, input().split()))
for j in range(m):
    b, c = map(int, input().split())
    i = bisect.bisect_left(A, c)
    start = min(i, b)
    A = A[:i] + [c for j in range(start)] + A[i:]
    A = A[start:]
print(sum(A))

Fixed submission:
n, m = map(int, input().split())
A = sorted(map(int, input().split()))
BC = []
for j in range(m):
    b, c = map(int, input().split())
    BC.append((b, c))
BC = sorted(BC, key=lambda x: x[1], reverse=True)
D = []
for b, c in BC:
    D.extend([c for i in range(b)])
    if len(D) >= n:
        D = D[:n]
        break
for x in range(len(D)):
    if A[x] > D[x]:
        break
else:
    x += 1
print(sum(A[x:]) + sum(D[:x]))

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
n, m = map(int, input().split())
A = sorted(map(int, input().split()))
BC = []
for j in range(m):
    b, c = map(int, input().split())
    BC.append((b, c))
BC = sorted(BC, key=lambda x: x[1], reverse=True)
for b, c in BC:
    i = bisect.bisect_left(A, c)
    if i == 0:
        break
    else:
        start = min(i, b)
        conect = [c for j in range(start)]
        A.extend(conect)
        A = A[start:]
print(sum(A))

Fixed submission:
n, m = map(int, input().split())
A = sorted(map(int, input().split()))
BC = []
for j in range(m):
    b, c = map(int, input().split())
    BC.append((b, c))
BC = sorted(BC, key=lambda x: x[1], reverse=True)
D = []
for b, c in BC:
    D.extend([c for i in range(b)])
    if len(D) >= n:
        D = D[:n]
        break
for x in range(len(D)):
    if A[x] > D[x]:
        break
else:
    x += 1
print(sum(A[x:]) + sum(D[:x]))

Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
A = sorted(map(int, input().split()))
BC = []
for j in range(m):
    b, c = map(int, input().split())
    BC.append((b, c))
BC = sorted(BC, key=lambda x: x[1], reverse=True)
for b, c in BC:
    i = bisect.bisect_left(A, c)
    if i == 0:
        break
    else:
        start = min(i, b)
        conect = [c for j in range(start)]
        A.extend(conect)
        A = A[start:]
print(sum(A))

Fixed submission:
n, m = map(int, input().split())
A = sorted(map(int, input().split()))
BC = []
for j in range(m):
    b, c = map(int, input().split())
    BC.append((b, c))
BC = sorted(BC, key=lambda x: x[1], reverse=True)
D = []
for b, c in BC:
    D.extend([c for i in range(b)])
    if len(D) >= n:
        D = D[:n]
        break
for x in range(len(D)):
    if A[x] > D[x]:
        break
else:
    x += 1
print(sum(A[x:]) + sum(D[:x]))

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
n, m = map(int, input().split())
A = sorted(map(int, input().split()))
BC = []
for j in range(m):
    b, c = map(int, input().split())
    BC.append((b, c))
BC = sorted(BC, key=lambda x : x[1], reverse=True)
for b, c in BC:
    i = bisect.bisect_left(A,c)
    if i == 0:
        break
    else:
        start = min(i, b)
        A = A[start:]
        for j in range(start):
            bisect.insort_right(A,c)
print(sum(A))

Fixed submission:
n, m = map(int, input().split())
A = sorted(map(int, input().split()))
BC = []
for j in range(m):
    b, c = map(int, input().split())
    BC.append((b, c))
BC = sorted(BC, key=lambda x: x[1], reverse=True)
D = []
for b, c in BC:
    D.extend([c for i in range(b)])
    if len(D) >= n:
        D = D[:n]
        break
for x in range(len(D)):
    if A[x] > D[x]:
        break
else:
    x += 1
print(sum(A[x:]) + sum(D[:x]))