Buggy python submission (verdict: Time Limit Exceeded):
import heapq
n,m = map(int,input().split())
ls = list(map(int,input().split()))
ls = list(map(lambda x:x*(-1),ls))
di = [list(map(int,input().split())) for _ in range(m)]
heapq.heapify(ls)
for i,j in di:
    for k in range(i):
        heapq.heappush(ls,j*(-1))
ans = 0
for l in range(n):
    ans += (-1)*heapq.heappop(ls)
print(ans)

Fixed submission:
n,m = map(int,input().split())
ls = list(map(int,input().split()))
di = [list(map(int,input().split())) for _ in range(m)]
fi = []
for i,j in di:
    fi.append([j,i])
for k in ls:
    fi.append([k,1])
fi.sort(reverse=True)
ans = 0
cnt = 0
how = 0
while True:
    if how + fi[cnt][1] < n:
        how += fi[cnt][1]
        ans += fi[cnt][1]*fi[cnt][0]
    else:
        ans += (n-how)*fi[cnt][0]
        break
    cnt += 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
n,m = map(int,input().split())
ls = list(map(int,input().split()))
ls = list(map(lambda x:x*(-1),ls))
di = [list(map(int,input().split())) for _ in range(m)]
heapq.heapify(ls)
for i,j in di:
    for k in range(i):
        heapq.heappush(ls,j*(-1))
ans = 0
for l in range(n):
    ans += (-1)*heapq.heappop(ls)
print(ans)

Fixed submission:
n,m = map(int,input().split())
ls = list(map(int,input().split()))
di = [list(map(int,input().split())) for _ in range(m)]
fi = []
for i,j in di:
    fi.append([j,i])
for k in ls:
    fi.append([k,1])
fi.sort(reverse=True)
ans = 0
cnt = 0
how = 0
while True:
    if how + fi[cnt][1] < n:
        how += fi[cnt][1]
        ans += fi[cnt][1]*fi[cnt][0]
    else:
        ans += (n-how)*fi[cnt][0]
        break
    cnt += 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
ls = list(map(int,input().split()))
for i in range(m):
    a,b = map(int,input().split())
    for j in range(a):
        ls.append(b)
ls.sort()
ls = ls[n*(-1):]
print(sum(ls))

Fixed submission:
n,m = map(int,input().split())
ls = list(map(int,input().split()))
di = [list(map(int,input().split())) for _ in range(m)]
fi = []
for i,j in di:
    fi.append([j,i])
for k in ls:
    fi.append([k,1])
fi.sort(reverse=True)
ans = 0
cnt = 0
how = 0
while True:
    if how + fi[cnt][1] < n:
        how += fi[cnt][1]
        ans += fi[cnt][1]*fi[cnt][0]
    else:
        ans += (n-how)*fi[cnt][0]
        break
    cnt += 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
ls = list(map(int,input().split()))
di = [list(map(int,input().split())) for _ in range(m)]
mn = min(ls)
for i in range(m):
    if di[i][1] > mn:
        ls += [di[i][1]]*di[i][0]
ls.sort()
ls = ls[n*(-1):]
print(sum(ls))

Fixed submission:
n,m = map(int,input().split())
ls = list(map(int,input().split()))
di = [list(map(int,input().split())) for _ in range(m)]
fi = []
for i,j in di:
    fi.append([j,i])
for k in ls:
    fi.append([k,1])
fi.sort(reverse=True)
ans = 0
cnt = 0
how = 0
while True:
    if how + fi[cnt][1] < n:
        how += fi[cnt][1]
        ans += fi[cnt][1]*fi[cnt][0]
    else:
        ans += (n-how)*fi[cnt][0]
        break
    cnt += 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
ls = list(map(int,input().split()))
di = [list(map(int,input().split())) for _ in range(m)]
for i in range(m):
    ls += [di[i][1]]*di[i][0]
ls.sort()
ls = ls[n*(-1):]
print(sum(ls))

Fixed submission:
n,m = map(int,input().split())
ls = list(map(int,input().split()))
di = [list(map(int,input().split())) for _ in range(m)]
fi = []
for i,j in di:
    fi.append([j,i])
for k in ls:
    fi.append([k,1])
fi.sort(reverse=True)
ans = 0
cnt = 0
how = 0
while True:
    if how + fi[cnt][1] < n:
        how += fi[cnt][1]
        ans += fi[cnt][1]*fi[cnt][0]
    else:
        ans += (n-how)*fi[cnt][0]
        break
    cnt += 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
ls = list(map(int,input().split()))
di = [list(map(int,input().split())) for _ in range(m)]
di.sort(key=lambda x:x[1])
di = [e for e in di if e[1] >= min(ls)]
ls.sort()
for i in range(len(di)):
    for j in range(di[i][0]):
        if di[i][1] > ls[j]:
            ls[j] = di[i][1]
        else:
            break
    ls.sort()
print(sum(ls))

Fixed submission:
n,m = map(int,input().split())
ls = list(map(int,input().split()))
di = [list(map(int,input().split())) for _ in range(m)]
fi = []
for i,j in di:
    fi.append([j,i])
for k in ls:
    fi.append([k,1])
fi.sort(reverse=True)
ans = 0
cnt = 0
how = 0
while True:
    if how + fi[cnt][1] < n:
        how += fi[cnt][1]
        ans += fi[cnt][1]*fi[cnt][0]
    else:
        ans += (n-how)*fi[cnt][0]
        break
    cnt += 1
print(ans)

Buggy python submission (verdict: Runtime Error):
n,m = map(int,input().split())
ls = set(map(int,input().split()))
for i in range(m):
    b,c = map(int,input().split())
    ls.sort()
    for j in range(b):
        if ls[j] < c:
            ls[j] = c
print(sum(ls))


Fixed submission:
n,m = map(int,input().split())
ls = list(map(int,input().split()))
di = [list(map(int,input().split())) for _ in range(m)]
fi = []
for i,j in di:
    fi.append([j,i])
for k in ls:
    fi.append([k,1])
fi.sort(reverse=True)
ans = 0
cnt = 0
how = 0
while True:
    if how + fi[cnt][1] < n:
        how += fi[cnt][1]
        ans += fi[cnt][1]*fi[cnt][0]
    else:
        ans += (n-how)*fi[cnt][0]
        break
    cnt += 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
ls = list(map(int,input().split()))
for i in range(m):
    b,c = map(int,input().split())
    ls.sort()
    for j in range(b):
        if ls[j] < c:
            ls[j] = c
print(sum(ls))

Fixed submission:
n,m = map(int,input().split())
ls = list(map(int,input().split()))
di = [list(map(int,input().split())) for _ in range(m)]
fi = []
for i,j in di:
    fi.append([j,i])
for k in ls:
    fi.append([k,1])
fi.sort(reverse=True)
ans = 0
cnt = 0
how = 0
while True:
    if how + fi[cnt][1] < n:
        how += fi[cnt][1]
        ans += fi[cnt][1]*fi[cnt][0]
    else:
        ans += (n-how)*fi[cnt][0]
        break
    cnt += 1
print(ans)