Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin
N,M=[int(x) for x in stdin.readline().rstrip().split()]
A=[int(x) for x in stdin.readline().rstrip().split()]
data=[]
A = sorted(A)
for i in range(M):
    r,q=[int(x) for x in stdin.readline().rstrip().split()]
    if q >= A[0]:
        for j in range(r):
            A.append(q)
 
ans = sum(sorted(A)[::-1][:N])
print(ans)

Fixed submission:
from sys import stdin
N,M=[int(x) for x in stdin.readline().rstrip().split()]
A=[(int(x),1) for x in stdin.readline().rstrip().split()]
data=[]
for i in range(M):
    r,q=[int(x) for x in stdin.readline().rstrip().split()]
    A.append((q,r))
ans=0
cnt=0
for j in reversed(sorted(A)):
    cnt+=j[1]
    ans+=(j[0]*j[1])
    if cnt > N:
        ans = ans-(j[0]*(cnt-N))
        break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin

def flatten(lst):
    result=[]
    for i in lst:
        if type(i) != list:
            result.append(i)
        else:
            #resultを上書きしないようにextendを使用
            result.extend(flatten(i))
    return result

N,M=[int(x) for x in stdin.readline().rstrip().split()]
A=[int(x) for x in stdin.readline().rstrip().split()]
data=[]
for i in range(M):
    r,q=[int(x) for x in stdin.readline().rstrip().split()]
    A.append([q]*r)
A = flatten(A) 
ans = sum(sorted(A)[::-1][:N])
print(ans)

Fixed submission:
from sys import stdin
N,M=[int(x) for x in stdin.readline().rstrip().split()]
A=[(int(x),1) for x in stdin.readline().rstrip().split()]
data=[]
for i in range(M):
    r,q=[int(x) for x in stdin.readline().rstrip().split()]
    A.append((q,r))
ans=0
cnt=0
for j in reversed(sorted(A)):
    cnt+=j[1]
    ans+=(j[0]*j[1])
    if cnt > N:
        ans = ans-(j[0]*(cnt-N))
        break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin
N,M=[int(x) for x in stdin.readline().rstrip().split()]
A=[int(x) for x in stdin.readline().rstrip().split()]
data=[]
for i in range(M):
    data.append([int(x) for x in stdin.readline().rstrip().split()])
for k in data:
    A=A+([k[1]]*k[0])


ans = sum(sorted(A)[::-1][:N])
print(ans)

Fixed submission:
from sys import stdin
N,M=[int(x) for x in stdin.readline().rstrip().split()]
A=[(int(x),1) for x in stdin.readline().rstrip().split()]
data=[]
for i in range(M):
    r,q=[int(x) for x in stdin.readline().rstrip().split()]
    A.append((q,r))
ans=0
cnt=0
for j in reversed(sorted(A)):
    cnt+=j[1]
    ans+=(j[0]*j[1])
    if cnt > N:
        ans = ans-(j[0]*(cnt-N))
        break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin
N,M=[int(x) for x in stdin.readline().rstrip().split()]
A=[int(x) for x in stdin.readline().rstrip().split()]
data=[]
for i in range(M):
    data.append([int(x) for x in stdin.readline().rstrip().split()])
for k in data:
    for j in range(k[0]):
        A.append(k[1])


ans = sum(sorted(A)[::-1][:N])
print(ans)

Fixed submission:
from sys import stdin
N,M=[int(x) for x in stdin.readline().rstrip().split()]
A=[(int(x),1) for x in stdin.readline().rstrip().split()]
data=[]
for i in range(M):
    r,q=[int(x) for x in stdin.readline().rstrip().split()]
    A.append((q,r))
ans=0
cnt=0
for j in reversed(sorted(A)):
    cnt+=j[1]
    ans+=(j[0]*j[1])
    if cnt > N:
        ans = ans-(j[0]*(cnt-N))
        break
print(ans)