Buggy python submission (verdict: Runtime Error):
n , m = map(int, input().split())
a = list(map(int,input().split()))
a.sort()
bc=[]
for i in range(m):
    b , c = map(int, input().split())
    bc.append((b,c))
bc.sort(key = lambda x : x[1],reverse=True)

t=0
for i in range(m):
    b,c=bc[i]
    if a[t]>=c:
        break
    for j in range(b):
        if a[t]<c:
            a[t]=c
            t+=1
        else:
            break
print(sum(a))


Fixed submission:
n , m = map(int, input().split())
a = list(map(int,input().split()))
a.sort()
bc=[]
for i in range(m):
    b , c = map(int, input().split())
    bc.append((b,c))
bc.sort(key = lambda x : x[1],reverse=True)

t=0
for i in range(m):
    b,c=bc[i]
    if t>=n:
        break
    if a[t]>=c:
        break
    for j in range(b):
        if a[t]<c:
            a[t]=c
            if t<n-1:
                t+=1
        else:
            break
print(sum(a))

Buggy python submission (verdict: Runtime Error):
n , m = map(int, input().split())
a = list(map(int,input().split()))
a.sort()
x=[]
for i in range(m):
    b , c = map(int, input().split())
    x+=[c]*b
x.sort(reverse=True)
t=n
if a[n-1]<x[n-1]:
    a=x[:n]
else:
    for i in range(n):
        if a[i]>=x[i]:
            t=i
            break
    a=x[:t]+a[t:]
print(sum(a))


Fixed submission:
n , m = map(int, input().split())
a = list(map(int,input().split()))
a.sort()
bc=[]
for i in range(m):
    b , c = map(int, input().split())
    bc.append((b,c))
bc.sort(key = lambda x : x[1],reverse=True)

t=0
for i in range(m):
    b,c=bc[i]
    if t>=n:
        break
    if a[t]>=c:
        break
    for j in range(b):
        if a[t]<c:
            a[t]=c
            if t<n-1:
                t+=1
        else:
            break
print(sum(a))

Buggy python submission (verdict: Runtime Error):
n , m = map(int, input().split())
a = list(map(int,input().split()))
a.sort()
x=[]
for i in range(m):
    b , c = map(int, input().split())
    x+=[c]*b
x.sort(reverse=True)
for i in range(n):
    if a[i]>=x[i]:
        t=i
        break
a=x[:t]+a[t:]
print(sum(a))


Fixed submission:
n , m = map(int, input().split())
a = list(map(int,input().split()))
a.sort()
bc=[]
for i in range(m):
    b , c = map(int, input().split())
    bc.append((b,c))
bc.sort(key = lambda x : x[1],reverse=True)

t=0
for i in range(m):
    b,c=bc[i]
    if t>=n:
        break
    if a[t]>=c:
        break
    for j in range(b):
        if a[t]<c:
            a[t]=c
            if t<n-1:
                t+=1
        else:
            break
print(sum(a))

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


Fixed submission:
n , m = map(int, input().split())
a = list(map(int,input().split()))
a.sort()
bc=[]
for i in range(m):
    b , c = map(int, input().split())
    bc.append((b,c))
bc.sort(key = lambda x : x[1],reverse=True)

t=0
for i in range(m):
    b,c=bc[i]
    if t>=n:
        break
    if a[t]>=c:
        break
    for j in range(b):
        if a[t]<c:
            a[t]=c
            if t<n-1:
                t+=1
        else:
            break
print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
n , m = map(int, input().split())
a = list(map(int,input().split()))
a.sort()
for i in range(m):
    b , c = map(int, input().split())
    index = bisect.bisect_left(a,c)
    if index>b-1:
        a=a[b:index]+[c]*b+a[index:]
    else:
        a=[c]*index+a[index:]
print(sum(a))


Fixed submission:
n , m = map(int, input().split())
a = list(map(int,input().split()))
a.sort()
bc=[]
for i in range(m):
    b , c = map(int, input().split())
    bc.append((b,c))
bc.sort(key = lambda x : x[1],reverse=True)

t=0
for i in range(m):
    b,c=bc[i]
    if t>=n:
        break
    if a[t]>=c:
        break
    for j in range(b):
        if a[t]<c:
            a[t]=c
            if t<n-1:
                t+=1
        else:
            break
print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
n , m = map(int, input().split())
a = list(map(int,input().split()))
a.sort()
for i in range(m):
    b , c = map(int, input().split())
    index = bisect.bisect_left(a,c)
    if index>b-1:
        a=a[b:index]+[c]*b+a[index:]
    else:
        a=[c]*index+a[index:]
print(sum(a))


Fixed submission:
n , m = map(int, input().split())
a = list(map(int,input().split()))
a.sort()
bc=[]
for i in range(m):
    b , c = map(int, input().split())
    bc.append((b,c))
bc.sort(key = lambda x : x[1],reverse=True)

t=0
for i in range(m):
    b,c=bc[i]
    if t>=n:
        break
    if a[t]>=c:
        break
    for j in range(b):
        if a[t]<c:
            a[t]=c
            if t<n-1:
                t+=1
        else:
            break
print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
n , m = map(int, input().split())
a = list(map(int,input().split()))
heapq.heapify(a)
for i in range(m):
    b , c = map(int, input().split())
    for j in range(b):
        if a[0]<c:
            heapq.heappushpop(a,c)
        else:
            break
print(sum(a))


Fixed submission:
n , m = map(int, input().split())
a = list(map(int,input().split()))
a.sort()
bc=[]
for i in range(m):
    b , c = map(int, input().split())
    bc.append((b,c))
bc.sort(key = lambda x : x[1],reverse=True)

t=0
for i in range(m):
    b,c=bc[i]
    if t>=n:
        break
    if a[t]>=c:
        break
    for j in range(b):
        if a[t]<c:
            a[t]=c
            if t<n-1:
                t+=1
        else:
            break
print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
n , m = map(int, input().split())
a = list(map(int,input().split()))
heapq.heapify(a)
for i in range(m):
    b , c = map(int, input().split())
    for j in range(b):
        if a[0]<c:
            heapq.heappushpop(a,c)
        else:
            break
print(sum(a))


Fixed submission:
n , m = map(int, input().split())
a = list(map(int,input().split()))
a.sort()
bc=[]
for i in range(m):
    b , c = map(int, input().split())
    bc.append((b,c))
bc.sort(key = lambda x : x[1],reverse=True)

t=0
for i in range(m):
    b,c=bc[i]
    if t>=n:
        break
    if a[t]>=c:
        break
    for j in range(b):
        if a[t]<c:
            a[t]=c
            if t<n-1:
                t+=1
        else:
            break
print(sum(a))