Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [list(map(int, input().split())) for i in range(M)]

L = []

# cnt_sum = 0
# min_num = BC[0][1]
# for i in range(M):
#     cnt = BC[i][0]
#     for j in range(N):
#         if cnt_sum <= N:
#             L.append(BC[i][1])
#             cnt_sum += 1
#             cnt -= 1
#             if BC[i][1] < min_num:
#                 min_num = BC[i][1]
#         else:
#             if BC[i][1] < min_num:
#                 break
#             if L[j] < BC[i][1]:
#                 L[j] = BC[i][1]
#                 cnt -= 1
#         if cnt <= 0:
#             break
len_L = 0
for i in range(M):
    # for j in range(BC[i][0]):
    #     L.append(BC[i][1])
    L.extend([BC[i][1] for j in range(BC[i][0])])
    len_L += BC[i][0]
L.sort(reverse=True)
A.sort()
# ans = sum(A)
ans = 0
flag = True
for k in range(N):
    if k <= len_L-1:
        if flag and L[k] > A[k]:
            ans += L[k]
        else:
            flag = False
            ans += A[k]
    else:
        ans += A[k]
print(ans)


Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [list(map(int, input().split())) for i in range(M)]

L = []
BC.sort(key=lambda x:x[1], reverse=True)
A.sort()
# ans = sum(A)
ans = 0
# flag = False
cnt = 0
# for k in range(N):
#     if k <= len_L-1:
#         if flag and L[k] > A[k]:
#             ans += L[k]
#         else:
#             flag = False
#             ans += A[k]
#     else:
#         ans += A[k]
for i in range(M):
    for j in range(BC[i][0]):
        if cnt < N and A[cnt] < BC[i][1]:
            ans += BC[i][1]
            cnt += 1
        else:
            break
    else:
        continue
    break

if cnt < N:
    ans += sum(A[cnt::])

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [list(map(int, input().split())) for i in range(M)]

L = []

# cnt_sum = 0
# min_num = BC[0][1]
# for i in range(M):
#     cnt = BC[i][0]
#     for j in range(N):
#         if cnt_sum <= N:
#             L.append(BC[i][1])
#             cnt_sum += 1
#             cnt -= 1
#             if BC[i][1] < min_num:
#                 min_num = BC[i][1]
#         else:
#             if BC[i][1] < min_num:
#                 break
#             if L[j] < BC[i][1]:
#                 L[j] = BC[i][1]
#                 cnt -= 1
#         if cnt <= 0:
#             break
len_L = 0
for i in range(M):
    # for j in range(BC[i][0]):
    #     L.append(BC[i][1])
    L.extend([BC[i][1] for j in range(BC[i][0])])
    len_L += BC[i][0]
L.sort(reverse=True)
A.sort()
# ans = sum(A)
ans = 0
flag = True
for k in range(N):
    if k <= len_L-1:
        if flag and L[k] > A[k]:
            ans += L[k]
        else:
            flag = False
            ans += A[k]
    else:
        ans += A[k]
print(ans)

Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [list(map(int, input().split())) for i in range(M)]

L = []
BC.sort(key=lambda x:x[1], reverse=True)
A.sort()
# ans = sum(A)
ans = 0
# flag = False
cnt = 0
# for k in range(N):
#     if k <= len_L-1:
#         if flag and L[k] > A[k]:
#             ans += L[k]
#         else:
#             flag = False
#             ans += A[k]
#     else:
#         ans += A[k]
for i in range(M):
    for j in range(BC[i][0]):
        if cnt < N and A[cnt] < BC[i][1]:
            ans += BC[i][1]
            cnt += 1
        else:
            break
    else:
        continue
    break

if cnt < N:
    ans += sum(A[cnt::])

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [list(map(int, input().split())) for i in range(M)]

L = []

# cnt_sum = 0
# min_num = BC[0][1]
# for i in range(M):
#     cnt = BC[i][0]
#     for j in range(N):
#         if cnt_sum <= N:
#             L.append(BC[i][1])
#             cnt_sum += 1
#             cnt -= 1
#             if BC[i][1] < min_num:
#                 min_num = BC[i][1]
#         else:
#             if BC[i][1] < min_num:
#                 break
#             if L[j] < BC[i][1]:
#                 L[j] = BC[i][1]
#                 cnt -= 1
#         if cnt <= 0:
#             break
for i in range(M):
    # for j in range(BC[i][0]):
    #     L.append(BC[i][1])
    L.extend([BC[i][1] for j in range(BC[i][0])])
L.sort(reverse=True)
A.sort()
ans = sum(A)
LL = len(L)
for k in range(N):
    if k <= LL-1 and L[k] > A[k]:
        ans += (L[k] - A[k])
    else:
        break
print(ans)

Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [list(map(int, input().split())) for i in range(M)]

L = []
BC.sort(key=lambda x:x[1], reverse=True)
A.sort()
# ans = sum(A)
ans = 0
# flag = False
cnt = 0
# for k in range(N):
#     if k <= len_L-1:
#         if flag and L[k] > A[k]:
#             ans += L[k]
#         else:
#             flag = False
#             ans += A[k]
#     else:
#         ans += A[k]
for i in range(M):
    for j in range(BC[i][0]):
        if cnt < N and A[cnt] < BC[i][1]:
            ans += BC[i][1]
            cnt += 1
        else:
            break
    else:
        continue
    break

if cnt < N:
    ans += sum(A[cnt::])

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [list(map(int, input().split())) for i in range(M)]

L = []

cnt_sum = 0
min_num = BC[0][1]
for i in range(M):
    cnt = BC[i][0]
    for j in range(N):
        if cnt_sum <= N:
            L.append(BC[i][1])
            cnt_sum += 1
            cnt -= 1
            if BC[i][1] < min_num:
                min_num = BC[i][1]
        else:
            if BC[i][1] < min_num:
                break
            if L[j] < BC[i][1]:
                L[j] = BC[i][1]
                cnt -= 1
        if cnt <= 0:
            break
# for i in range(M):
#     for j in range(BC[i][0]):
#         L.append(BC[i][1])
L.sort(reverse=True)
A.sort()
ans = sum(A)
LL = len(L)
for k in range(N):
    if k <= LL-1 and L[k] > A[k]:
        ans += (L[k] - A[k])
    else:
        break
print(ans)


Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [list(map(int, input().split())) for i in range(M)]

L = []
BC.sort(key=lambda x:x[1], reverse=True)
A.sort()
# ans = sum(A)
ans = 0
# flag = False
cnt = 0
# for k in range(N):
#     if k <= len_L-1:
#         if flag and L[k] > A[k]:
#             ans += L[k]
#         else:
#             flag = False
#             ans += A[k]
#     else:
#         ans += A[k]
for i in range(M):
    for j in range(BC[i][0]):
        if cnt < N and A[cnt] < BC[i][1]:
            ans += BC[i][1]
            cnt += 1
        else:
            break
    else:
        continue
    break

if cnt < N:
    ans += sum(A[cnt::])

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [list(map(int, input().split())) for i in range(M)]

L = []

cnt_sum = 0
# for i in range(M):
#     cnt = BC[i][0]
#     for j in range(N):
#         if cnt_sum <= N:
#             L.append(BC[i][1])
#             cnt_sum += 1
#             cnt -= 1
#         elif L[j] < BC[i][1]:
#             L[j] = BC[i][1]
#             cnt -= 1
#         if cnt <= 0:
#             break
for i in range(M):
    for j in range(BC[i][0]):
        L.append(BC[i][1])
L.sort(reverse=True)
A.sort()
ans = sum(A)
LL = len(L)
for k in range(N):
    if k <= LL-1 and L[k] > A[k]:
        ans += (L[k] - A[k])
    else:
        break
print(ans)


Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [list(map(int, input().split())) for i in range(M)]

L = []
BC.sort(key=lambda x:x[1], reverse=True)
A.sort()
# ans = sum(A)
ans = 0
# flag = False
cnt = 0
# for k in range(N):
#     if k <= len_L-1:
#         if flag and L[k] > A[k]:
#             ans += L[k]
#         else:
#             flag = False
#             ans += A[k]
#     else:
#         ans += A[k]
for i in range(M):
    for j in range(BC[i][0]):
        if cnt < N and A[cnt] < BC[i][1]:
            ans += BC[i][1]
            cnt += 1
        else:
            break
    else:
        continue
    break

if cnt < N:
    ans += sum(A[cnt::])

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [list(map(int, input().split())) for i in range(M)]

L = []

cnt_sum = 0
for i in range(M):
    cnt = BC[i][0]
    for j in range(N):
        if cnt_sum <= N:
            L.append(BC[i][1])
            cnt_sum += 1
            cnt -= 1
        elif L[j] < BC[i][1]:
            L[j] = BC[i][1]
            cnt -= 1
        if cnt <= 0:
            break
L.sort(reverse=True)
A.sort()
ans = sum(A)
LL = len(L)
for k in range(N):
    if k <= LL-1 and L[k] > A[k]:
        ans += (L[k] - A[k])
    else:
        break
print(ans)

Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [list(map(int, input().split())) for i in range(M)]

L = []
BC.sort(key=lambda x:x[1], reverse=True)
A.sort()
# ans = sum(A)
ans = 0
# flag = False
cnt = 0
# for k in range(N):
#     if k <= len_L-1:
#         if flag and L[k] > A[k]:
#             ans += L[k]
#         else:
#             flag = False
#             ans += A[k]
#     else:
#         ans += A[k]
for i in range(M):
    for j in range(BC[i][0]):
        if cnt < N and A[cnt] < BC[i][1]:
            ans += BC[i][1]
            cnt += 1
        else:
            break
    else:
        continue
    break

if cnt < N:
    ans += sum(A[cnt::])

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [list(map(int, input().split())) for i in range(M)]

A.sort()
for i in range(M):
  cnt = 0
  for j in range(N):
    if cnt >= BC[i][0]:
      break
    if BC[i][1] > A[j]:
      A[j] = BC[i][1]
      cnt += 1
  if i != M-1:
    A.sort()
print(sum(A))

    


Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [list(map(int, input().split())) for i in range(M)]

L = []
BC.sort(key=lambda x:x[1], reverse=True)
A.sort()
# ans = sum(A)
ans = 0
# flag = False
cnt = 0
# for k in range(N):
#     if k <= len_L-1:
#         if flag and L[k] > A[k]:
#             ans += L[k]
#         else:
#             flag = False
#             ans += A[k]
#     else:
#         ans += A[k]
for i in range(M):
    for j in range(BC[i][0]):
        if cnt < N and A[cnt] < BC[i][1]:
            ans += BC[i][1]
            cnt += 1
        else:
            break
    else:
        continue
    break

if cnt < N:
    ans += sum(A[cnt::])

print(ans)

Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [list(map(int, input().split())) for i in range(M)]

A.sort()
for i in range(M):
  cnt = 0
  for j in range(M):
    if cnt >= BC[i][0]:
      break
    if BC[i][1] > A[j]:
      A[j] = BC[i][1]
      cnt += 1
  if i != M-1:
    A.sort()
print(sum(A))

    

Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [list(map(int, input().split())) for i in range(M)]

L = []
BC.sort(key=lambda x:x[1], reverse=True)
A.sort()
# ans = sum(A)
ans = 0
# flag = False
cnt = 0
# for k in range(N):
#     if k <= len_L-1:
#         if flag and L[k] > A[k]:
#             ans += L[k]
#         else:
#             flag = False
#             ans += A[k]
#     else:
#         ans += A[k]
for i in range(M):
    for j in range(BC[i][0]):
        if cnt < N and A[cnt] < BC[i][1]:
            ans += BC[i][1]
            cnt += 1
        else:
            break
    else:
        continue
    break

if cnt < N:
    ans += sum(A[cnt::])

print(ans)