Buggy python submission (verdict: Runtime Error):
n,m=map(int,input().split())
a=list(map(int,input().split()))
arr=[]

for i in range(m):
    b,c=map(int,input().split())
    arr.append((b,c))

a=sorted(a)
arr=sorted(arr,key=lambda x:-x[1])

flag=0
left=0
for tup in arr:
    for i in range(left,max(left+tup[0],n+1)):
        if tup[1]>a[i]:
            a[i]=tup[1]
        else:
            flag=-1
            break
        if i==n:
            flag=-1
            break
    left+=tup[0]
    if flag==-1:
        break

print(sum(a))

Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
arr=[]

for i in range(m):
    b,c=map(int,input().split())
    arr.append((b,c))

a=sorted(a)
arr=sorted(arr,key=lambda x:-x[1])

flag=0
left=0
for tup in arr:
    for i in range(left,min(left+tup[0],n)):
        if tup[1]>a[i]:
            a[i]=tup[1]
        else:
            flag=-1
            break
        if i==n-1:
            flag=-1
            break
    left+=tup[0]
    if flag==-1:
        break

print(sum(a))

Buggy python submission (verdict: Runtime Error):
n,m=map(int,input().split())
a=list(map(int,input().split()))
arr=[]

for i in range(m):
    b,c=map(int,input().split())
    arr.append((b,c))

a=sorted(a)
arr=sorted(arr,key=lambda x:-x[1])

flag=0
left=0
for tup in arr:
    for i in range(left,left+tup[0]):
        if tup[1]>a[i]:
            a[i]=tup[1]
        else:
            flag=-1
            break
    left+=tup[0]
    if flag==-1:
        break

print(sum(a))

Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
arr=[]

for i in range(m):
    b,c=map(int,input().split())
    arr.append((b,c))

a=sorted(a)
arr=sorted(arr,key=lambda x:-x[1])

flag=0
left=0
for tup in arr:
    for i in range(left,min(left+tup[0],n)):
        if tup[1]>a[i]:
            a[i]=tup[1]
        else:
            flag=-1
            break
        if i==n-1:
            flag=-1
            break
    left+=tup[0]
    if flag==-1:
        break

print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n,m=map(int,input().split())
    a=sorted(list(map(int,input().split())))
    d=[]
    for _ in range(m):
        b,c=map(int,input().split())
        for _ in range(b):
    	    d.append(c)

    d=sorted(d,reverse=True)
    right=sum(a)
    left=0

    for i in range(min(n,len(d))):
        if a[i]<d[i]:
            left+=d[i]
            right-=a[i]
        else:
            break
    print(right+left)

if __name__ == "__main__":
    main()

Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
arr=[]

for i in range(m):
    b,c=map(int,input().split())
    arr.append((b,c))

a=sorted(a)
arr=sorted(arr,key=lambda x:-x[1])

flag=0
left=0
for tup in arr:
    for i in range(left,min(left+tup[0],n)):
        if tup[1]>a[i]:
            a[i]=tup[1]
        else:
            flag=-1
            break
        if i==n-1:
            flag=-1
            break
    left+=tup[0]
    if flag==-1:
        break

print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
a=sorted(list(map(int,input().split())))
d=[]
for _ in range(m):
    b,c=map(int,input().split())
    for _ in range(b):
    	d.append(c)

d=sorted(d,reverse=True)

total=0
for i in range(n):
  total+=a[i]
  
right=total
left=0

for i in range(min(n,len(d))):
    if a[i]<d[i]:
        left+=d[i]
        right-=a[i]
    else:
        break
print(right+left)

Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
arr=[]

for i in range(m):
    b,c=map(int,input().split())
    arr.append((b,c))

a=sorted(a)
arr=sorted(arr,key=lambda x:-x[1])

flag=0
left=0
for tup in arr:
    for i in range(left,min(left+tup[0],n)):
        if tup[1]>a[i]:
            a[i]=tup[1]
        else:
            flag=-1
            break
        if i==n-1:
            flag=-1
            break
    left+=tup[0]
    if flag==-1:
        break

print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
a=sorted(list(map(int,input().split())))
bc=[]
d=[]

for _ in range(m):
    b,c=map(int,input().split())
    bc.append((b,c))
bc=sorted(bc,key=lambda x:x[1],reverse=True)
for i in range(m):
    for _ in range(int(bc[i][0])):
    	d.append(bc[i][1])
    
right=sum(a)
left=0

for i in range(min(n,len(d))):
    if a[i]<d[i]:
        left+=d[i]
        right-=a[i]
    else:
        break
print(right+left)

Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
arr=[]

for i in range(m):
    b,c=map(int,input().split())
    arr.append((b,c))

a=sorted(a)
arr=sorted(arr,key=lambda x:-x[1])

flag=0
left=0
for tup in arr:
    for i in range(left,min(left+tup[0],n)):
        if tup[1]>a[i]:
            a[i]=tup[1]
        else:
            flag=-1
            break
        if i==n-1:
            flag=-1
            break
    left+=tup[0]
    if flag==-1:
        break

print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
a=sorted(list(map(int,input().split())))
d=[]
d_len=0
for _ in range(m):
    b,c=map(int,input().split())
    d_len+=b
    for _ in range(b):
    	d.append(c)

d=sorted(d,reverse=True)
right=sum(a)
left=0

for i in range(min(n,d_len)):
    if a[i]<d[i]:
        left+=d[i]
        right-=a[i]
    else:
        break
print(right+left)

Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
arr=[]

for i in range(m):
    b,c=map(int,input().split())
    arr.append((b,c))

a=sorted(a)
arr=sorted(arr,key=lambda x:-x[1])

flag=0
left=0
for tup in arr:
    for i in range(left,min(left+tup[0],n)):
        if tup[1]>a[i]:
            a[i]=tup[1]
        else:
            flag=-1
            break
        if i==n-1:
            flag=-1
            break
    left+=tup[0]
    if flag==-1:
        break

print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
a=sorted(list(map(int,input().split())))
d=[]
for _ in range(m):
    b,c=map(int,input().split())
    for _ in range(b):
    	d.append(c)

d=sorted(d,reverse=True)
right=sum(a)
left=0

for i in range(min(n,len(d))):
    if a[i]<d[i]:
        left+=d[i]
        right-=a[i]
    else:
        break
print(right+left)

Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
arr=[]

for i in range(m):
    b,c=map(int,input().split())
    arr.append((b,c))

a=sorted(a)
arr=sorted(arr,key=lambda x:-x[1])

flag=0
left=0
for tup in arr:
    for i in range(left,min(left+tup[0],n)):
        if tup[1]>a[i]:
            a[i]=tup[1]
        else:
            flag=-1
            break
        if i==n-1:
            flag=-1
            break
    left+=tup[0]
    if flag==-1:
        break

print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
a=sorted(list(map(int,input().split())))
d=[]
for _ in range(m):
    b,c=map(int,input().split())
    for _ in range(b):
    	d.append(c)

d=sorted(d,reverse=True)
right=sum(a)
left=0

for i in range(min(n,len(d))):
    if a[i]<d[i]:
        left+=d[i]
        right-=a[i]
    else:
        break
print(right+left)

Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
arr=[]

for i in range(m):
    b,c=map(int,input().split())
    arr.append((b,c))

a=sorted(a)
arr=sorted(arr,key=lambda x:-x[1])

flag=0
left=0
for tup in arr:
    for i in range(left,min(left+tup[0],n)):
        if tup[1]>a[i]:
            a[i]=tup[1]
        else:
            flag=-1
            break
        if i==n-1:
            flag=-1
            break
    left+=tup[0]
    if flag==-1:
        break

print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
a=sorted(list(map(int,input().split())))
s=[]
for _ in range(m):
    b,c=map(int,input().split())
    s.append((b,c))

s=sorted(s,key=lambda x: x[1],reverse=True)
for i in range(m):
    for j in range(s[i][0]):
        if a[j]<s[i][1]:
            a[j]=s[i][1]
    a=sorted(a)
print(sum(a))

Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
arr=[]

for i in range(m):
    b,c=map(int,input().split())
    arr.append((b,c))

a=sorted(a)
arr=sorted(arr,key=lambda x:-x[1])

flag=0
left=0
for tup in arr:
    for i in range(left,min(left+tup[0],n)):
        if tup[1]>a[i]:
            a[i]=tup[1]
        else:
            flag=-1
            break
        if i==n-1:
            flag=-1
            break
    left+=tup[0]
    if flag==-1:
        break

print(sum(a))