Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
li = list(map(int,input().split()))
tmp_li = []
for _ in range(m):
    b,c = map(int,input().split())
    tmp_li.append((c,b))
tmp_li.sort(reverse=True)
#print(tmp_li)
""" for c,b in tmp_li:
    li.extend([c]*b)
    if len(li)>= 2*n:
        break
li.sort(reverse=True)
print(sum(li[:n])) """

count = 0
for c,d in tmp_li:
    kazu = min(d,n-count+1)
    li.extend([c]*kazu)
    if count>=n:
        break
li.sort(reverse=True)
print(sum(li[:n]))

Fixed submission:
n,m = map(int,input().split())
li = list(map(int,input().split()))
tmp_li = []
for _ in range(m):
    b,c = map(int,input().split())
    tmp_li.append((c,b))
tmp_li.sort(reverse=True)
#print(tmp_li)
for c,b in tmp_li:
    li.extend([c]*b)
    if len(li)>= 2*n:
        break
li.sort(reverse=True)
print(sum(li[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
li = list(map(int,input().split()))
tmp_li = []
for _ in range(m):
    b,c = map(int,input().split())
    tmp_li.append((c,b))
tmp_li.sort(reverse=True)
#print(tmp_li)
count = 0
for t in tmp_li:
    kazu = min(t[1],n-count+1)
    li.extend([t[0]]*kazu)
    if count>=n:
        break
li.sort(reverse=True)
print(sum(li[:n]))

Fixed submission:
n,m = map(int,input().split())
li = list(map(int,input().split()))
tmp_li = []
for _ in range(m):
    b,c = map(int,input().split())
    tmp_li.append((c,b))
tmp_li.sort(reverse=True)
#print(tmp_li)
for c,b in tmp_li:
    li.extend([c]*b)
    if len(li)>= 2*n:
        break
li.sort(reverse=True)
print(sum(li[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
li = list(map(int,input().split()))
tmp_li = []
for _ in range(m):
    b,c = map(int,input().split())
    tmp_li.append((c,b))
tmp_li.sort(reverse=True)
#print(tmp_li)
count = 0
for t in tmp_li:
    kazu = min(t[1],n-count+1)
    li += [t[0]]*kazu
    if count>=n:
        break
li.sort(reverse=True)
print(sum(li[:n]))

Fixed submission:
n,m = map(int,input().split())
li = list(map(int,input().split()))
tmp_li = []
for _ in range(m):
    b,c = map(int,input().split())
    tmp_li.append((c,b))
tmp_li.sort(reverse=True)
#print(tmp_li)
for c,b in tmp_li:
    li.extend([c]*b)
    if len(li)>= 2*n:
        break
li.sort(reverse=True)
print(sum(li[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
li = list(map(int,input().split()))
tmp_li = []
for _ in range(m):
    b,c = map(int,input().split())
    tmp_li.append([c,b])
tmp_li.sort(reverse=True)
#print(tmp_li)
count = 0
for t in tmp_li:
    kazu = min(t[1],n-count+1)
    li += [t[0]]*kazu
    if count>=n:
        break
li.sort(reverse=True)
print(sum(li[:n]))

Fixed submission:
n,m = map(int,input().split())
li = list(map(int,input().split()))
tmp_li = []
for _ in range(m):
    b,c = map(int,input().split())
    tmp_li.append((c,b))
tmp_li.sort(reverse=True)
#print(tmp_li)
for c,b in tmp_li:
    li.extend([c]*b)
    if len(li)>= 2*n:
        break
li.sort(reverse=True)
print(sum(li[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
li = list(map(int,input().split()))
tmp_li = []
for _ in range(m):
    b,c = map(int,input().split())
    tmp_li.append([c,b])
tmp_li.sort(reverse=True)
#print(tmp_li)
count = 0
for t in tmp_li:
    kazu = min(t[1],n-count+1)
    li += [t[0]]*kazu
    if count>=n:
        break
li.sort(reverse=True)
print(sum(li[:n]))

Fixed submission:
n,m = map(int,input().split())
li = list(map(int,input().split()))
tmp_li = []
for _ in range(m):
    b,c = map(int,input().split())
    tmp_li.append((c,b))
tmp_li.sort(reverse=True)
#print(tmp_li)
for c,b in tmp_li:
    li.extend([c]*b)
    if len(li)>= 2*n:
        break
li.sort(reverse=True)
print(sum(li[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
li = list(map(int,input().split()))
tmp_li = []
for _ in range(m):
    b,c = map(int,input().split())
    tmp_li.append([c,b])
tmp_li.sort(reverse=True)
#print(tmp_li)
count = 0
for t in tmp_li:
    li += [t[0]]*t[1]
    if count + t[1]>=n:
        break
li.sort(reverse=True)
print(sum(li[:n]))

Fixed submission:
n,m = map(int,input().split())
li = list(map(int,input().split()))
tmp_li = []
for _ in range(m):
    b,c = map(int,input().split())
    tmp_li.append((c,b))
tmp_li.sort(reverse=True)
#print(tmp_li)
for c,b in tmp_li:
    li.extend([c]*b)
    if len(li)>= 2*n:
        break
li.sort(reverse=True)
print(sum(li[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
li = list(map(int,input().split()))
tmp_li = []
for _ in range(m):
    b,c = map(int,input().split())
    tmp_li.append([c,b])
tmp_li.sort(reverse=True)
#print(tmp_li)
count = 0
for t in tmp_li:
    li += [t[0]]*t[1]
    if count + t[1]>=n:
        break
li.sort(reverse=True)
print(sum(li[:n]))

Fixed submission:
n,m = map(int,input().split())
li = list(map(int,input().split()))
tmp_li = []
for _ in range(m):
    b,c = map(int,input().split())
    tmp_li.append((c,b))
tmp_li.sort(reverse=True)
#print(tmp_li)
for c,b in tmp_li:
    li.extend([c]*b)
    if len(li)>= 2*n:
        break
li.sort(reverse=True)
print(sum(li[:n]))

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

Fixed submission:
n,m = map(int,input().split())
li = list(map(int,input().split()))
tmp_li = []
for _ in range(m):
    b,c = map(int,input().split())
    tmp_li.append((c,b))
tmp_li.sort(reverse=True)
#print(tmp_li)
for c,b in tmp_li:
    li.extend([c]*b)
    if len(li)>= 2*n:
        break
li.sort(reverse=True)
print(sum(li[:n]))

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

Fixed submission:
n,m = map(int,input().split())
li = list(map(int,input().split()))
tmp_li = []
for _ in range(m):
    b,c = map(int,input().split())
    tmp_li.append((c,b))
tmp_li.sort(reverse=True)
#print(tmp_li)
for c,b in tmp_li:
    li.extend([c]*b)
    if len(li)>= 2*n:
        break
li.sort(reverse=True)
print(sum(li[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
li = list(map(int,input().split()))
li.sort()
add = []
for _ in range(m):
    b,c = map(int,input().split())
    add += [c]*b
add.sort(reverse=True)
ans = 0
#print(len(li),len(add))
if len(li)>len(add):
    ans = sum(li[len(add):])
#print(fin)
for l,a in zip(li,add):
    #print(l,a)
    if a>l:
        ans += a
    else:
        ans += l
print(ans)

Fixed submission:
n,m = map(int,input().split())
li = list(map(int,input().split()))
tmp_li = []
for _ in range(m):
    b,c = map(int,input().split())
    tmp_li.append((c,b))
tmp_li.sort(reverse=True)
#print(tmp_li)
for c,b in tmp_li:
    li.extend([c]*b)
    if len(li)>= 2*n:
        break
li.sort(reverse=True)
print(sum(li[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
li = list(map(int,input().split()))
li.sort()
add = []
for _ in range(m):
    b,c = map(int,input().split())
    add += [c]*b
add.sort(reverse=True)
fin = []
#print(len(li),len(add))
if len(li)>len(add):
    fin = li[len(add):]
#print(fin)
for l,a in zip(li,add):
    #print(l,a)
    if a>l:
        fin.append(a)
    else:
        fin.append(l)
print(sum(fin))

Fixed submission:
n,m = map(int,input().split())
li = list(map(int,input().split()))
tmp_li = []
for _ in range(m):
    b,c = map(int,input().split())
    tmp_li.append((c,b))
tmp_li.sort(reverse=True)
#print(tmp_li)
for c,b in tmp_li:
    li.extend([c]*b)
    if len(li)>= 2*n:
        break
li.sort(reverse=True)
print(sum(li[:n]))