Buggy python submission (verdict: Time Limit Exceeded):
N,M = map(int, input().split())
A = list(map(int, input().split()))
BC = [list(map(int, input().split())) for _ in range(M)]
BC = sorted(BC, reverse=True, key=lambda x:x[1])
cnt = 0
for B,C in BC:
    A += [C]*B
    cnt += B
    if B >= N:
        break
A = sorted(A, reverse=True)
print(sum(A[:N]))


Fixed submission:
n,m=map(int,input().split())
ans=list(map(int,input().split()))
arr=[list(map(int,input().split())) for _ in range(m)]
arr=sorted(arr,reverse=True,key=lambda x:x[1])
tmp=0
for cnt,val in arr:
  ans+=[val]*cnt
  tmp+=cnt
  if tmp>=n:
    break
ans=sorted(ans,reverse=True)
print(sum(ans[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
N,M = map(int, input().split())
A = list(map(int, input().split()))
BC = sorted([list(map(int, input().split())) for _ in range(M)], reverse=True, key=lambda x:x[1])
cnt = 0
for B,C in BC:
    A += [C]*B
    cnt += B
    if B >= N:
        break
A = sorted(A, reverse=True)
print(sum(A[:N]))


Fixed submission:
n,m=map(int,input().split())
ans=list(map(int,input().split()))
arr=[list(map(int,input().split())) for _ in range(m)]
arr=sorted(arr,reverse=True,key=lambda x:x[1])
tmp=0
for cnt,val in arr:
  ans+=[val]*cnt
  tmp+=cnt
  if tmp>=n:
    break
ans=sorted(ans,reverse=True)
print(sum(ans[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N,M = map(int, input().split())
    A = list(map(int, input().split()))
    BC = sorted([list(map(int, input().split())) for _ in range(M)], reverse=True, key=lambda x:x[1])
    cnt = 0
    for B,C in BC:
        A += [C]*B
        cnt += B
        if B >= N:
            break
    A = sorted(A, reverse=True)
    print(sum(A[:N]))

main()

Fixed submission:
n,m=map(int,input().split())
ans=list(map(int,input().split()))
arr=[list(map(int,input().split())) for _ in range(m)]
arr=sorted(arr,reverse=True,key=lambda x:x[1])
tmp=0
for cnt,val in arr:
  ans+=[val]*cnt
  tmp+=cnt
  if tmp>=n:
    break
ans=sorted(ans,reverse=True)
print(sum(ans[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N,M = map(int, input().split())
    A = list(map(int, input().split()))
    BC = [list(map(int, input().split())) for _ in range(M)]
    BC.sort(reverse=True, key=lambda x:x[1])
    cnt = 0
    for B,C in BC:
        A += [C]*B
        cnt += B
        if B >= N:
            break
    A.sort(reverse=True)
    print(sum(A[:N]))

main()

Fixed submission:
n,m=map(int,input().split())
ans=list(map(int,input().split()))
arr=[list(map(int,input().split())) for _ in range(m)]
arr=sorted(arr,reverse=True,key=lambda x:x[1])
tmp=0
for cnt,val in arr:
  ans+=[val]*cnt
  tmp+=cnt
  if tmp>=n:
    break
ans=sorted(ans,reverse=True)
print(sum(ans[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N,M = map(int, input().split())
    A = list(map(int, input().split()))
    BC = [list(map(int, input().split())) for _ in range(M)]
    BC.sort(reverse=True, key=lambda x:x[1])
    cnt = 0
    for B,C in BC:
        A.extend([C for _ in range(B)])
        cnt += B
        if B >= N:
            break
    A.sort(reverse=True)
    print(sum(A[:N]))

main()

Fixed submission:
n,m=map(int,input().split())
ans=list(map(int,input().split()))
arr=[list(map(int,input().split())) for _ in range(m)]
arr=sorted(arr,reverse=True,key=lambda x:x[1])
tmp=0
for cnt,val in arr:
  ans+=[val]*cnt
  tmp+=cnt
  if tmp>=n:
    break
ans=sorted(ans,reverse=True)
print(sum(ans[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N,M = map(int, input().split())
    A = list(map(int, input().split()))
    BC = [list(map(int, input().split())) for _ in range(M)]
    BC.sort(reverse=True, key=lambda x:x[1])
    cnt = 0
    for B,C in BC:
        A.extend([C for _ in range(B)])
        cnt += B
        if B >= N:
            break
    A.sort(reverse=True)
    ans = 0
    for i in range(N):
        ans += A[i]
    print(ans)

main()

Fixed submission:
n,m=map(int,input().split())
ans=list(map(int,input().split()))
arr=[list(map(int,input().split())) for _ in range(m)]
arr=sorted(arr,reverse=True,key=lambda x:x[1])
tmp=0
for cnt,val in arr:
  ans+=[val]*cnt
  tmp+=cnt
  if tmp>=n:
    break
ans=sorted(ans,reverse=True)
print(sum(ans[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    from collections import deque
    N,M = map(int, input().split())
    A = deque(map(int, input().split()))
    for _ in range(M):
        B,C=map(int, input().split())
        A.extend([C for _ in range(B)])
    A = list(A)
    A.sort(reverse=True)
    ans = 0
    for i in range(N):
        ans += A[i]
    print(ans)

main()

Fixed submission:
n,m=map(int,input().split())
ans=list(map(int,input().split()))
arr=[list(map(int,input().split())) for _ in range(m)]
arr=sorted(arr,reverse=True,key=lambda x:x[1])
tmp=0
for cnt,val in arr:
  ans+=[val]*cnt
  tmp+=cnt
  if tmp>=n:
    break
ans=sorted(ans,reverse=True)
print(sum(ans[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    from collections import deque
    N,M = map(int, input().split())
    A = deque(map(int, input().split()))
    for _ in range(M):
        B,C=map(int, input().split())
        A.extend([C for _ in range(B)])
    A = list(A)
    A.sort()
    print(sum(A[len(A)-N:]))

main()

Fixed submission:
n,m=map(int,input().split())
ans=list(map(int,input().split()))
arr=[list(map(int,input().split())) for _ in range(m)]
arr=sorted(arr,reverse=True,key=lambda x:x[1])
tmp=0
for cnt,val in arr:
  ans+=[val]*cnt
  tmp+=cnt
  if tmp>=n:
    break
ans=sorted(ans,reverse=True)
print(sum(ans[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N,M = map(int, input().split())
    A = list(map(int, input().split()))
    A.sort()
    number = set(A)
    l = {n:0 for n in list(number)}
    for i in A:
        l[i] += 1
    for _ in range(M):
        B,C = map(int, input().split())
        f = False
        for i in number:
            if l[i] == 0:
                continue
            if B == 0:
                break
            if i < C:
                minus = min(l[i],B)
                if C not in l:
                    l[C] = 0
                l[C] += minus
                l[i] -= minus
                B -= minus
                f = True
        if f:
            number.add(C)
    ans = 0
    for k,v in l.items():
        ans += k*v
    print(ans)

main()

Fixed submission:
n,m=map(int,input().split())
ans=list(map(int,input().split()))
arr=[list(map(int,input().split())) for _ in range(m)]
arr=sorted(arr,reverse=True,key=lambda x:x[1])
tmp=0
for cnt,val in arr:
  ans+=[val]*cnt
  tmp+=cnt
  if tmp>=n:
    break
ans=sorted(ans,reverse=True)
print(sum(ans[:n]))