Buggy python submission (verdict: Runtime Error):
n, m = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
bc = [list(map(int,input().split())) for _ in range(m)]
bc.sort(key=lambda x:x[1], reverse=True)

ad = 0
idx = 0
for b, c in bc:
    for i in range(b):
        if a[idx+i] < c:
            continue
        else:
            ad += c*i
            idx += i
            print(ad + sum(a[idx:]))
            exit()
    ad += c*b
    idx += b
print(ad + sum(a[idx:]))

Fixed submission:
n, m = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
bc = [list(map(int,input().split())) for _ in range(m)]
bc.sort(key=lambda x:x[1], reverse=True)

ad = 0
idx = 0
for b, c in bc:
    for i in range(min(b, n-idx)):
        if a[idx+i] < c:
            continue
        else:
            ad += c*i
            idx += i
            print(ad + sum(a[idx:]))
            exit()
    ad += c*min(b, n-idx)
    idx += min(b, n-idx)
print(ad + sum(a[idx:]))

Buggy python submission (verdict: Runtime Error):
n, m = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
bc = [list(map(int,input().split())) for _ in range(m)]
bc.sort(key=lambda x:x[1], reverse=True)

ad = 0
idx = 0
for b, c in bc:
    for i in range(b):
        if a[idx+i] < c:
            ad += c
        else:
            idx += i
            print(ad + sum(a[idx:]))
            exit()
    else:
        idx += i+1
print(ad + sum(a[idx:]))

Fixed submission:
n, m = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
bc = [list(map(int,input().split())) for _ in range(m)]
bc.sort(key=lambda x:x[1], reverse=True)

ad = 0
idx = 0
for b, c in bc:
    for i in range(min(b, n-idx)):
        if a[idx+i] < c:
            continue
        else:
            ad += c*i
            idx += i
            print(ad + sum(a[idx:]))
            exit()
    ad += c*min(b, n-idx)
    idx += min(b, n-idx)
print(ad + sum(a[idx:]))

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import accumulate
n, m = map(int,input().split())
a = list(map(int,input().split()))
for i in range(m):
    b, c = map(int,input().split())
    a += [c]*b
a.sort(reverse=True)
ac = list(accumulate(a))
print(ac[n-1])

Fixed submission:
n, m = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
bc = [list(map(int,input().split())) for _ in range(m)]
bc.sort(key=lambda x:x[1], reverse=True)

ad = 0
idx = 0
for b, c in bc:
    for i in range(min(b, n-idx)):
        if a[idx+i] < c:
            continue
        else:
            ad += c*i
            idx += i
            print(ad + sum(a[idx:]))
            exit()
    ad += c*min(b, n-idx)
    idx += min(b, n-idx)
print(ad + sum(a[idx:]))

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import accumulate
n, m = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
cnt = 0
for i in range(m):
    b, c = map(int,input().split())
    a += [c]*b
    cnt += b
a.sort()
ac = list(accumulate(a))
if cnt == 0:
    print(ac[-1])
else:
    print(ac[-1] - ac[cnt-1])

Fixed submission:
n, m = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
bc = [list(map(int,input().split())) for _ in range(m)]
bc.sort(key=lambda x:x[1], reverse=True)

ad = 0
idx = 0
for b, c in bc:
    for i in range(min(b, n-idx)):
        if a[idx+i] < c:
            continue
        else:
            ad += c*i
            idx += i
            print(ad + sum(a[idx:]))
            exit()
    ad += c*min(b, n-idx)
    idx += min(b, n-idx)
print(ad + sum(a[idx:]))

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
from itertools import accumulate
n, m = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
cnt = 0
for i in range(m):
    b, c = map(int,input().split())
    bi = bisect.bisect(a, c)
    mb = min(bi, b)
    if mb <= 0:
        continue
    a += [c]*mb
    cnt += mb
a.sort()
ac = list(accumulate(a))
if cnt == 0:
    print(ac[-1])
else:
    print(ac[-1] - ac[cnt-1])

Fixed submission:
n, m = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
bc = [list(map(int,input().split())) for _ in range(m)]
bc.sort(key=lambda x:x[1], reverse=True)

ad = 0
idx = 0
for b, c in bc:
    for i in range(min(b, n-idx)):
        if a[idx+i] < c:
            continue
        else:
            ad += c*i
            idx += i
            print(ad + sum(a[idx:]))
            exit()
    ad += c*min(b, n-idx)
    idx += min(b, n-idx)
print(ad + sum(a[idx:]))

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
n, m = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
total = sum(a)
cnt = 0
for i in range(m):
    b, c = map(int,input().split())
    bi = bisect.bisect(a, c)
    mb = min(bi, b)
    a += [c]*mb
    total += mb * c
    cnt += mb
a.sort()
print(total - sum(a[:cnt]))

Fixed submission:
n, m = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
bc = [list(map(int,input().split())) for _ in range(m)]
bc.sort(key=lambda x:x[1], reverse=True)

ad = 0
idx = 0
for b, c in bc:
    for i in range(min(b, n-idx)):
        if a[idx+i] < c:
            continue
        else:
            ad += c*i
            idx += i
            print(ad + sum(a[idx:]))
            exit()
    ad += c*min(b, n-idx)
    idx += min(b, n-idx)
print(ad + sum(a[idx:]))

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
n, m = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
cnt = 0
for i in range(m):
    b, c = map(int,input().split())
    bi = bisect.bisect(a, c)
    mb = min(bi-cnt, b)
    if mb < 0:
        continue
    cnt += mb
    for _ in range(mb):
        bisect.insort(a, c)
print(sum(a[cnt:]))

Fixed submission:
n, m = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
bc = [list(map(int,input().split())) for _ in range(m)]
bc.sort(key=lambda x:x[1], reverse=True)

ad = 0
idx = 0
for b, c in bc:
    for i in range(min(b, n-idx)):
        if a[idx+i] < c:
            continue
        else:
            ad += c*i
            idx += i
            print(ad + sum(a[idx:]))
            exit()
    ad += c*min(b, n-idx)
    idx += min(b, n-idx)
print(ad + sum(a[idx:]))