Buggy python submission (verdict: Time Limit Exceeded):
N,M = map(int,input().split())
A = [int(a) for a in input().split()]
BC = [[int(x) for x in input().split()] for _ in range(M)]

A.sort(reverse=True)
BC.sort(key=lambda x:x[1], reverse=True)

def bysectsearch(List,num,Left):
    Right = len(List)-1
    while True:
        Midle = (Left+Right)//2
        if List[Left]<=num:
            return Left
        if List[Right]>=num:
            return Right+1
        if Right-Left<=1:
            return Right
        if List[Midle]==num:
            return Midle
        if List[Midle]>num:
            Left = Midle
            continue
        if List[Midle]<num:
            Right = Midle
            continue

Left = 0
for B,C in BC:
    insert_point = bysectsearch(A,C,Left)
    if insert_point == N:
        break
    A = A[:insert_point]+[C]*B+A[insert_point:]
    A = A[:N]
    Left = insert_point + B
    if Left>N-1:
        break
print(sum(A))


Fixed submission:
N,M = map(int,input().split())
A = [int(a) for a in input().split()]
BC = [[int(x) for x in input().split()] for _ in range(M)]

BC.sort(key=lambda x:x[1], reverse=True)

count = 0
Added_list = []
for i in range(M):
    B,C = BC[i]
    Added_list += [C]*B
    count += B
    if count>N:
        Added_list = Added_list[:N]
        break

A += Added_list
A.sort(reverse=True)
print(sum(A[:N]))

Buggy python submission (verdict: Runtime Error):
N,M = map(int,input().split())
A = [int(a) for a in input().split()]
BC = [[int(x) for x in input().split()] for _ in range(M)]

A.sort(reverse=True)
BC.sort(key=lambda x:x[1], reverse=True)

def bysectsearch(List,num,Left):
    Right = len(List)-1
    while True:
        Midle = (Left+Right)//2
        if List[Left]<=num:
            return Left
        if List[Right]>=num:
            return Right+1
        if Right-Left<=1:
            return Right
        if List[Midle]==num:
            return Midle
        if List[Midle]>num:
            Left = Midle
            continue
        if List[Midle]<num:
            Right = Midle
            continue

Left = 0
for B,C in BC:
    insert_point = bysectsearch(A,C,Left)
    if insert_point == N:
        break
    A = A[:insert_point]+[C]*B+A[insert_point:]
    A = A[:N]
    Left = insert_point + B
print(sum(A))


Fixed submission:
N,M = map(int,input().split())
A = [int(a) for a in input().split()]
BC = [[int(x) for x in input().split()] for _ in range(M)]

BC.sort(key=lambda x:x[1], reverse=True)

count = 0
Added_list = []
for i in range(M):
    B,C = BC[i]
    Added_list += [C]*B
    count += B
    if count>N:
        Added_list = Added_list[:N]
        break

A += Added_list
A.sort(reverse=True)
print(sum(A[:N]))

Buggy python submission (verdict: Time Limit Exceeded):
N,M = map(int,input().split())
A = [int(a) for a in input().split()]
for _ in range(M):
    B,C = map(int,input().split())
    A += [C]*B
A.sort(reverse=True)
print(sum(A[:N]))


Fixed submission:
N,M = map(int,input().split())
A = [int(a) for a in input().split()]
BC = [[int(x) for x in input().split()] for _ in range(M)]

BC.sort(key=lambda x:x[1], reverse=True)

count = 0
Added_list = []
for i in range(M):
    B,C = BC[i]
    Added_list += [C]*B
    count += B
    if count>N:
        Added_list = Added_list[:N]
        break

A += Added_list
A.sort(reverse=True)
print(sum(A[:N]))

Buggy python submission (verdict: Time Limit Exceeded):
N,M = map(int,input().split())
A = [int(a) for a in input().split()]
BC = [[int(x) for x in input().split()] for _ in range(M)]

A.sort(reverse=True)
BC.sort(key=lambda x:x[1], reverse=True)

def bysectsearch(List,num):
    Left = 0
    Right = len(List)-1
    while True:
        Midle = (Left+Right)//2
        if List[Left]<=num:
            return Left
        if List[Right]>=num:
            return Right+1
        if Right-Left<=1:
            return Right
        if List[Midle]==num:
            return Midle
        if List[Midle]>num:
            Left = Midle
            continue
        if List[Midle]<num:
            Right = Midle
            continue


for B,C in BC:
    insert_point = bysectsearch(A,C)
    if insert_point == N:
        break
    A = A[:insert_point]+[C]*B+A[insert_point:]
    A = A[:N]
print(sum(A))


Fixed submission:
N,M = map(int,input().split())
A = [int(a) for a in input().split()]
BC = [[int(x) for x in input().split()] for _ in range(M)]

BC.sort(key=lambda x:x[1], reverse=True)

count = 0
Added_list = []
for i in range(M):
    B,C = BC[i]
    Added_list += [C]*B
    count += B
    if count>N:
        Added_list = Added_list[:N]
        break

A += Added_list
A.sort(reverse=True)
print(sum(A[:N]))