Buggy python submission (verdict: Time Limit Exceeded):
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n, m = na()
als = na()
ls = []
for i in range(m):
    b, c = na()
    ls.append([b, c])
ls.sort(key=lambda x: x[1], reverse=True)
als.sort()
while len(ls) > 0:
    m = als[0]
    if ls[0][0] == 0:
        ls.pop(0)
        if len(ls) == 0:
            break
    if m < ls[0][1]:
        als.remove(m)
        als.append(ls[0][1])
        ls[0][0] -= 1
    else:
        break
print(sum(als))

Fixed submission:
import sys
from operator import itemgetter

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces


n, m = na()
a = na()
bcs = []
for i in range(m):
    bcs.append(na())
for i in range(n):
    bcs.append([1, a[i]])
bcs.sort(key=itemgetter(1))

ans = 0
rem = n
for i in range(m+n-1, -1, -1):
    use = min(rem, bcs[i][0])
    ans += use*bcs[i][1]
    rem -= use

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n, m = na()
als = na()
ls = []
for i in range(m):
    b, c = na()
    ls.append([b, c])
ls.sort(key=lambda x: x[1], reverse=True)
als.sort()
while len(ls) > 0:
    m = als[0]
    if ls[0][0] == 0:
        ls.pop(0)
        if len(ls) == 0:
            break
    if m < ls[0][1]:
        als.remove(m)
        als.append(ls[0][1])
        ls[0][0] -= 1
    else:
        break
print(sum(als))

Fixed submission:
import sys
from operator import itemgetter

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces


n, m = na()
a = na()
bcs = []
for i in range(m):
    bcs.append(na())
for i in range(n):
    bcs.append([1, a[i]])
bcs.sort(key=itemgetter(1))

ans = 0
rem = n
for i in range(m+n-1, -1, -1):
    use = min(rem, bcs[i][0])
    ans += use*bcs[i][1]
    rem -= use

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n, m = na()
als = na()
ls = []
for i in range(m):
    b, c = na()
    ls.append([b, c])
ls.sort(key=lambda x: x[1], reverse=True)
while len(ls) > 0:
    m = min(als)
    if ls[0][0] == 0:
        ls.pop(0)
        if len(ls) == 0:
            break
    if m < ls[0][1]:
        als.remove(m)
        als.append(ls[0][1])
        ls[0][0] -= 1
    else:
        break
print(sum(als))


Fixed submission:
import sys
from operator import itemgetter

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces


n, m = na()
a = na()
bcs = []
for i in range(m):
    bcs.append(na())
for i in range(n):
    bcs.append([1, a[i]])
bcs.sort(key=itemgetter(1))

ans = 0
rem = n
for i in range(m+n-1, -1, -1):
    use = min(rem, bcs[i][0])
    ans += use*bcs[i][1]
    rem -= use

print(ans)