Buggy python submission (verdict: Time Limit Exceeded):
# ABC127D - Integer Cards
import sys
input = sys.stdin.readline

n, m = list(map(int, input().rstrip().split()))
num = list(map(int, input().rstrip().split()))
lst = [list(map(int, input().rstrip().split())) for _ in range(m)]
for i in lst:
    num += [i[1]] * i[0]
print(sum(sorted(num, reverse=True)[:n]))

Fixed submission:
# ABC127D - Integer Cards
import sys
input = sys.stdin.readline

n, m = list(map(int, input().rstrip().split()))
num = sorted(list(map(int, input().rstrip().split())))
lst = sorted([list(map(int, input().rstrip().split())) for _ in range(m)], key=lambda x:x[1], reverse=True)
ans = 0
idx = 0
flg = False
for i, j in lst:
    for _ in range(i):
        if idx < n and num[idx] < j:
            ans += j
            idx += 1
        else:
            flg = True
            break
    if flg:
        break
if idx < n:
    ans += sum(num[idx:])
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
# ABC127D - Integer Cards
n, m = list(map(int, input().rstrip().split()))
num = list(map(int, input().rstrip().split()))
lst = [list(map(int, input().rstrip().split())) for _ in range(m)]
for i in lst:
    num += [i[1]] * i[0]
print(sum(sorted(num, reverse=True)[:n]))

Fixed submission:
# ABC127D - Integer Cards
import sys
input = sys.stdin.readline

n, m = list(map(int, input().rstrip().split()))
num = sorted(list(map(int, input().rstrip().split())))
lst = sorted([list(map(int, input().rstrip().split())) for _ in range(m)], key=lambda x:x[1], reverse=True)
ans = 0
idx = 0
flg = False
for i, j in lst:
    for _ in range(i):
        if idx < n and num[idx] < j:
            ans += j
            idx += 1
        else:
            flg = True
            break
    if flg:
        break
if idx < n:
    ans += sum(num[idx:])
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
# ABC127D - Integer Cards
n, m = list(map(int, input().rstrip().split()))
num = list(map(int, input().rstrip().split()))
lst = [list(map(int, input().rstrip().split())) for _ in range(m)]
for i in lst:
    num += [i[1]] * i[0]
print(sum(sorted(num, reverse=True)[:n]))

Fixed submission:
# ABC127D - Integer Cards
import sys
input = sys.stdin.readline

n, m = list(map(int, input().rstrip().split()))
num = sorted(list(map(int, input().rstrip().split())))
lst = sorted([list(map(int, input().rstrip().split())) for _ in range(m)], key=lambda x:x[1], reverse=True)
ans = 0
idx = 0
flg = False
for i, j in lst:
    for _ in range(i):
        if idx < n and num[idx] < j:
            ans += j
            idx += 1
        else:
            flg = True
            break
    if flg:
        break
if idx < n:
    ans += sum(num[idx:])
print(ans)

Buggy python submission (verdict: Runtime Error):
# ABC127D - Integer Cards
n, m = list(map(int, input().rstrip().split()))
num = list(map(int, input().rstrip().split()))
lst = [list(map(int, input().rstrip().split())) for _ in range(m)]
ans = 0
for i in lst:
    num += [i[1]] * i[0]
print(sum(sorted(lst, reverse=True)[:n]))

Fixed submission:
# ABC127D - Integer Cards
import sys
input = sys.stdin.readline

n, m = list(map(int, input().rstrip().split()))
num = sorted(list(map(int, input().rstrip().split())))
lst = sorted([list(map(int, input().rstrip().split())) for _ in range(m)], key=lambda x:x[1], reverse=True)
ans = 0
idx = 0
flg = False
for i, j in lst:
    for _ in range(i):
        if idx < n and num[idx] < j:
            ans += j
            idx += 1
        else:
            flg = True
            break
    if flg:
        break
if idx < n:
    ans += sum(num[idx:])
print(ans)