Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
ai = list(map(int, input().split()))

#print(ai)
#ais = sorted(ai, reverse=True)
ais = sorted(ai)
#print(ais)
dica={}
for i in range(len(ais)):
    dica.setdefault(i,ais[i])
#print(dica)

bc = []
for i in range(m):
    bc.append(list(map(int, input().split())))

#print(bc)
bcs = sorted(bc, reverse=True, key=lambda x:x[1])
#print(bcs)

pt = 0
for i in range(m):
    tn, tv =bcs[i]
    #print(pt, n)
    #print(dica)
    for j in range(pt,n):
        if dica[j] < tv:
            dica[j]=tv
            tn=tn-1
            pt=j
            if tn == 0:
                break

#print(ais)
print(sum(dica.values()))

Fixed submission:

n, m = map(int, input().split())
ai = list(map(int, input().split()))

#print(ai)
#ais = sorted(ai, reverse=True)
ais = sorted(ai)
#print(ais)
dica={}
for i in range(len(ais)):
    dica.setdefault(i,ais[i])
#print(dica)

bc = []
for i in range(m):
    bc.append(list(map(int, input().split())))

#print(bc)
bcs = sorted(bc, reverse=True, key=lambda x:x[1])
#print(bcs)

pt = 0
for i in range(m):
    tn, tv =bcs[i]
    #print(pt, n)
    #print(dica)
    for j in range(pt,n):
        if dica[j] < tv and 0 < tn:
            dica[j]=tv
            tn=tn-1
            pt=j+1
        else:
            break

#print(ais)
print(sum(dica.values()))

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
ai = list(map(int, input().split()))

#print(ai)
#ais = sorted(ai, reverse=True)
ais = sorted(ai)
#print(ais)
dica={}
for i in range(len(ais)):
    dica.setdefault(i,ais[i])
#print(dica)

bc = []
for i in range(m):
    bc.append(list(map(int, input().split())))

#print(bc)
bcs = sorted(bc, reverse=True, key=lambda x:x[1])
#print(bcs)

pt = 0
for i in range(m):
    tn, tv =bcs[i]
    for j in range(pt,n):
        if dica[j] < tv and 0<tn:
            dica[j]=tv
            tn=tn-1
            pt=j

#print(ais)
print(sum(dica.values()))


Fixed submission:

n, m = map(int, input().split())
ai = list(map(int, input().split()))

#print(ai)
#ais = sorted(ai, reverse=True)
ais = sorted(ai)
#print(ais)
dica={}
for i in range(len(ais)):
    dica.setdefault(i,ais[i])
#print(dica)

bc = []
for i in range(m):
    bc.append(list(map(int, input().split())))

#print(bc)
bcs = sorted(bc, reverse=True, key=lambda x:x[1])
#print(bcs)

pt = 0
for i in range(m):
    tn, tv =bcs[i]
    #print(pt, n)
    #print(dica)
    for j in range(pt,n):
        if dica[j] < tv and 0 < tn:
            dica[j]=tv
            tn=tn-1
            pt=j+1
        else:
            break

#print(ais)
print(sum(dica.values()))

Buggy python submission (verdict: Time Limit Exceeded):

n, m = map(int, input().split())
ai = list(map(int, input().split()))

#print(ai)
#ais = sorted(ai, reverse=True)
ais = sorted(ai)
#print(ais)

bc = []
for i in range(m):
    bc.append(list(map(int, input().split())))

#print(bc)
bcs = sorted(bc, reverse=True, key=lambda x:x[1])
#print(bcs)

pt = 0
for i in range(m):
    tn, tv =bcs[i]
    for j in range(pt,n):
        if ais[j] < tv and 0<tn:
            ais[j]=tv
            tn=tn-1
            pt=j

#print(ais)
print(sum(ais))

Fixed submission:

n, m = map(int, input().split())
ai = list(map(int, input().split()))

#print(ai)
#ais = sorted(ai, reverse=True)
ais = sorted(ai)
#print(ais)
dica={}
for i in range(len(ais)):
    dica.setdefault(i,ais[i])
#print(dica)

bc = []
for i in range(m):
    bc.append(list(map(int, input().split())))

#print(bc)
bcs = sorted(bc, reverse=True, key=lambda x:x[1])
#print(bcs)

pt = 0
for i in range(m):
    tn, tv =bcs[i]
    #print(pt, n)
    #print(dica)
    for j in range(pt,n):
        if dica[j] < tv and 0 < tn:
            dica[j]=tv
            tn=tn-1
            pt=j+1
        else:
            break

#print(ais)
print(sum(dica.values()))