Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
a = list(map(int, input().split()))
bc = [tuple(map(int, input().split())) for i in range(m)]
a.sort()
bc.sort(key=lambda x: -x[1])
# print(a)
# print(bc)

count = 0
idx = -1
end = False
for b, c in bc:
  if idx + b >= n and a[idx+b] < c:
    count += b * c
    idx += b
  else:
    for i in range(idx+1, idx+1+b):
      if i >= n or end:
        break
      else:
        if a[i] < c:
          count += c
          idx += 1
        else:
          end = True
  # print((idx, count))
  if end:
    break

if idx <= n-2:
  count += sum(a[idx+1:])

print(count)


Fixed submission:
n, m = map(int, input().split())
a = list(map(int, input().split()))
bc = [tuple(map(int, input().split())) for i in range(m)]
a.sort()
bc.sort(key=lambda x: -x[1])
# print(a)
# print(bc)

count = 0
idx = -1
end = False
for b, c in bc:
  if idx + b <= n-1 and a[idx+b] < c:
    count += b * c
    idx += b
  else:
    for i in range(idx+1, idx+1+b):
      if i >= n or end:
        break
      else:
        if a[i] < c:
          count += c
          idx += 1
        else:
          end = True
  # print((idx, count))
  if end:
    break

if idx <= n-2:
  count += sum(a[idx+1:])

print(count)

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
a = list(map(int, input().split()))
bc = [tuple(map(int, input().split())) for i in range(m)]
a.sort()
bc.sort(key=lambda x: -x[1])
# print(a)
# print(bc)

count = 0
end = False
for b, c in bc:
  if end:
    break
  if len(a) <= 0:
    break
  for j in range(b):
    if end:
      break
    if a[0] < c:
      count += c
      # print(c)
      del a[0]
    else:
      end = True

print(count + sum(a))


Fixed submission:
n, m = map(int, input().split())
a = list(map(int, input().split()))
bc = [tuple(map(int, input().split())) for i in range(m)]
a.sort()
bc.sort(key=lambda x: -x[1])
# print(a)
# print(bc)

count = 0
idx = -1
end = False
for b, c in bc:
  if idx + b <= n-1 and a[idx+b] < c:
    count += b * c
    idx += b
  else:
    for i in range(idx+1, idx+1+b):
      if i >= n or end:
        break
      else:
        if a[i] < c:
          count += c
          idx += 1
        else:
          end = True
  # print((idx, count))
  if end:
    break

if idx <= n-2:
  count += sum(a[idx+1:])

print(count)

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
a = list(map(int, input().split()))
bc = [tuple(map(int, input().split())) for i in range(m)]
a.sort()
bc.sort(key=lambda x: -x[1])

count = 0
end = False
for b, c in bc:
  if end:
    break
  if len(a) <= 0:
    break
  for j in range(b):
    if a[0] < c:
      count += c
      del a[0]
    else:
      end = True

print(count + sum(a))


Fixed submission:
n, m = map(int, input().split())
a = list(map(int, input().split()))
bc = [tuple(map(int, input().split())) for i in range(m)]
a.sort()
bc.sort(key=lambda x: -x[1])
# print(a)
# print(bc)

count = 0
idx = -1
end = False
for b, c in bc:
  if idx + b <= n-1 and a[idx+b] < c:
    count += b * c
    idx += b
  else:
    for i in range(idx+1, idx+1+b):
      if i >= n or end:
        break
      else:
        if a[i] < c:
          count += c
          idx += 1
        else:
          end = True
  # print((idx, count))
  if end:
    break

if idx <= n-2:
  count += sum(a[idx+1:])

print(count)