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

input = sys.stdin.readline
n, m = map(int, input().split())
a_list = list(map(int, input().split()))
for _ in range(m):
    b, c = map(int, input().split())
    a_list.extend([c]*b)
a_list.sort(reverse=True)
print(sum(a_list[:n]))


Fixed submission:
import sys

input = sys.stdin.readline
n, m = map(int, input().split())
a_list = list(map(int, input().split()))
m_list = []
for _ in range(m):
    b, c = map(int, input().split())
    m_list.append([c, b])
m_list.sort(reverse=True)
cnt = 0
tmp_list = []
for c, b in m_list:
    if cnt < n:
        tmp_list += [c]*b
    else:
        break
    cnt += b
a_list.extend(tmp_list)
a_list.sort(reverse=True)
print(sum(a_list[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
a_list = list(map(int, input().split()))
for _ in range(m):
    b, c = map(int, input().split())
    a_list.extend([c]*b)
a_list.sort(reverse=True)
print(sum(a_list[:n]))


Fixed submission:
import sys

input = sys.stdin.readline
n, m = map(int, input().split())
a_list = list(map(int, input().split()))
m_list = []
for _ in range(m):
    b, c = map(int, input().split())
    m_list.append([c, b])
m_list.sort(reverse=True)
cnt = 0
tmp_list = []
for c, b in m_list:
    if cnt < n:
        tmp_list += [c]*b
    else:
        break
    cnt += b
a_list.extend(tmp_list)
a_list.sort(reverse=True)
print(sum(a_list[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
a_list = list(map(int, input().split()))
for _ in range(m):
    b, c = map(int, input().split())
    a_list += [c]*b
a_list.sort(reverse=True)
print(sum(a_list[:n]))


Fixed submission:
import sys

input = sys.stdin.readline
n, m = map(int, input().split())
a_list = list(map(int, input().split()))
m_list = []
for _ in range(m):
    b, c = map(int, input().split())
    m_list.append([c, b])
m_list.sort(reverse=True)
cnt = 0
tmp_list = []
for c, b in m_list:
    if cnt < n:
        tmp_list += [c]*b
    else:
        break
    cnt += b
a_list.extend(tmp_list)
a_list.sort(reverse=True)
print(sum(a_list[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
a_list = list(map(int, input().split()))
ans = sum(a_list)
m_list = []
for _ in range(m):
    b, c = map(int, input().split())
    m_list += [c]*b
a_list.sort()
m_list.sort(reverse=True)
for a, m in zip(a_list, m_list):
    if a < m:
        ans += (m-a)
    else:
        break
print(ans)


Fixed submission:
import sys

input = sys.stdin.readline
n, m = map(int, input().split())
a_list = list(map(int, input().split()))
m_list = []
for _ in range(m):
    b, c = map(int, input().split())
    m_list.append([c, b])
m_list.sort(reverse=True)
cnt = 0
tmp_list = []
for c, b in m_list:
    if cnt < n:
        tmp_list += [c]*b
    else:
        break
    cnt += b
a_list.extend(tmp_list)
a_list.sort(reverse=True)
print(sum(a_list[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
a_list = list(map(int, input().split()))
m_list = []
cnt = 0
for _ in range(m):
    b, c = map(int, input().split())
    m_list += [c]*b
    cnt += b
if cnt < n:
    m_list += [0]*(n-cnt)
a_list.sort()
m_list.sort(reverse=True)
ans = 0
for a, m in zip(a_list, m_list):
    ans += max(a, m)
print(ans)


Fixed submission:
import sys

input = sys.stdin.readline
n, m = map(int, input().split())
a_list = list(map(int, input().split()))
m_list = []
for _ in range(m):
    b, c = map(int, input().split())
    m_list.append([c, b])
m_list.sort(reverse=True)
cnt = 0
tmp_list = []
for c, b in m_list:
    if cnt < n:
        tmp_list += [c]*b
    else:
        break
    cnt += b
a_list.extend(tmp_list)
a_list.sort(reverse=True)
print(sum(a_list[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

n, m = map(int, input().split())
a_list = list(map(int, input().split()))
m_list = []
cnt = 0
for _ in range(m):
    b, c = map(int, input().split())
    m_list += [c]*b
    cnt += b
a_list.sort()
m_list.sort(reverse=True)
m_q = deque(m_list)
ans = 0
check = m_q.popleft()
for a in a_list:
    if a < check:
        ans += check
        if cnt <= 1:
            check = 0
        else:
            check = m_q.popleft()
            cnt -= 1
    else:
        ans += a
print(ans)


Fixed submission:
import sys

input = sys.stdin.readline
n, m = map(int, input().split())
a_list = list(map(int, input().split()))
m_list = []
for _ in range(m):
    b, c = map(int, input().split())
    m_list.append([c, b])
m_list.sort(reverse=True)
cnt = 0
tmp_list = []
for c, b in m_list:
    if cnt < n:
        tmp_list += [c]*b
    else:
        break
    cnt += b
a_list.extend(tmp_list)
a_list.sort(reverse=True)
print(sum(a_list[:n]))

Buggy python submission (verdict: Runtime Error):
from collections import deque

n, m = map(int, input().split())
a_list = list(map(int, input().split()))
m_list = []
for _ in range(m):
    b, c = map(int, input().split())
    m_list += [c]*b
a_list.sort()
m_list.sort(reverse=True)
m_q = deque(m_list)
ans = 0
check = m_q.popleft()
cnt = 1
for a in a_list:
    if a < check:
        ans += check
        check = m_q.popleft()
        cnt += 1
        if cnt >= m:
            check = 0
    else:
        ans += a
print(ans)


Fixed submission:
import sys

input = sys.stdin.readline
n, m = map(int, input().split())
a_list = list(map(int, input().split()))
m_list = []
for _ in range(m):
    b, c = map(int, input().split())
    m_list.append([c, b])
m_list.sort(reverse=True)
cnt = 0
tmp_list = []
for c, b in m_list:
    if cnt < n:
        tmp_list += [c]*b
    else:
        break
    cnt += b
a_list.extend(tmp_list)
a_list.sort(reverse=True)
print(sum(a_list[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

n, m = map(int, input().split())
a_list = list(map(int, input().split()))
m_list = []
for _ in range(m):
    b, c = map(int, input().split())
    m_list += [c]*b
a_list.sort()
m_list.sort(reverse=True)
m_q = deque(m_list)
ans = 0
check = m_q.popleft()
for a in a_list:
    if a < check:
        ans += check
        if len(m_q) > 0:
            check = m_q.popleft()
        else:
            check = 0
    else:
        ans += a
print(ans)


Fixed submission:
import sys

input = sys.stdin.readline
n, m = map(int, input().split())
a_list = list(map(int, input().split()))
m_list = []
for _ in range(m):
    b, c = map(int, input().split())
    m_list.append([c, b])
m_list.sort(reverse=True)
cnt = 0
tmp_list = []
for c, b in m_list:
    if cnt < n:
        tmp_list += [c]*b
    else:
        break
    cnt += b
a_list.extend(tmp_list)
a_list.sort(reverse=True)
print(sum(a_list[:n]))