Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
bc=[list(map(int,input().split()))[::-1]for _ in range(m)]+[[0,1000000000000]]
bc.sort(reverse=1)
t=[]
for c,b in bc:
  for _ in range(b):
    t.append(c)
    if len(t)==n:break
ans=0
for i in range(n):ans+=max(a[i],t[i])
print(ans)

Fixed submission:
from collections import deque
import sys
input=sys.stdin.readline
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
a=deque(a)
x=[]
for i in range(m):x.append(list(map(int,input().split()))[::-1])
x.sort(reverse=1)
for i,j in x:
  for k in range(j):
    if i<=a[0]:print(sum(a));exit()
    a.popleft();a.append(i)
print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
import sys
input=sys.stdin.readline
n,m=map(int,input().split())
a=deque(sorted(list(map(int,input().split()))))
x=[]
for i in range(m):x.append(list(map(int,input().split()))[::-1])
x.sort(reverse=1)
s=0
for i,j in x:
  for k in range(j):
    if i<a[0]:s=1;break
    aa=a.popleft();a.append(i)
  if s==1:break
print(sum(a))

Fixed submission:
from collections import deque
import sys
input=sys.stdin.readline
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
a=deque(a)
x=[]
for i in range(m):x.append(list(map(int,input().split()))[::-1])
x.sort(reverse=1)
for i,j in x:
  for k in range(j):
    if i<=a[0]:print(sum(a));exit()
    a.popleft();a.append(i)
print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input=sys.stdin.readline
n,m=map(int,input().split())
a=sorted(list(map(int,input().split())))
x=[]
for i in range(m):x.append(list(map(int,input().split()))[::-1])
x.sort(reverse=1)
s=0
for i,j in x:
  for k in range(j):
    if i<=a[0]:s=1;break
    del a[0];a.append(i)
  if s==1:break
print(sum(a))

Fixed submission:
from collections import deque
import sys
input=sys.stdin.readline
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
a=deque(a)
x=[]
for i in range(m):x.append(list(map(int,input().split()))[::-1])
x.sort(reverse=1)
for i,j in x:
  for k in range(j):
    if i<=a[0]:print(sum(a));exit()
    a.popleft();a.append(i)
print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
a=sorted(list(map(int,input().split())))
x=[]
for i in range(m):x.append(list(map(int,input().split()))[::-1])
x.sort(reverse=1)
s=0
for i,j in x:
  for k in range(j):
    if i<a[0]:s=1;break
    del a[0];a.append(i)
  if s==1:break
print(sum(a))

Fixed submission:
from collections import deque
import sys
input=sys.stdin.readline
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
a=deque(a)
x=[]
for i in range(m):x.append(list(map(int,input().split()))[::-1])
x.sort(reverse=1)
for i,j in x:
  for k in range(j):
    if i<=a[0]:print(sum(a));exit()
    a.popleft();a.append(i)
print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
a=sorted(list(map(int,input().split())))
x=[]
for i in range(m):x.append(list(map(int,input().split()))[::-1])
x.sort(reverse=1)
s=0
for i,j in x:
  for k in range(j):
    if i<a[0]:s=1;break
    del a[0];a.append(i)
  if s==1:break
print(sum(a))

Fixed submission:
from collections import deque
import sys
input=sys.stdin.readline
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
a=deque(a)
x=[]
for i in range(m):x.append(list(map(int,input().split()))[::-1])
x.sort(reverse=1)
for i,j in x:
  for k in range(j):
    if i<=a[0]:print(sum(a));exit()
    a.popleft();a.append(i)
print(sum(a))