Buggy python submission (verdict: Runtime Error):
def main():
    import sys
    input = sys.stdin.readline
    #n = int(input())
    #l = list(map(int, input().split()))
    '''
    A=[]
    B=[]
    for i in range():
        a, b = map(int, input().split())
        A.append(a)
        B.append(b)'''

    #import bisect
    n, m = map(int, input().split())
    a = list(map(int, input().split()))
    a.sort()
    #print(a)

    bc=[]
    for i in range(m):
        B = list(map(int, input().split()))
        bc.append(B)
    bc.sort(key=lambda x: x[1])
    #print(bc)

    clis=[]
    for i in range(m)[::-1]:
        clis+=[bc[i][1] for i in range(bc[i][0])]
    #print(clis)
    ans=0
    le=len(clis)
    for i in range(n):
        #print(clis[i],a[i])
        if i<le and clis[i]>a[i]:
            ans+=clis[i]
        else:
            ans+=a[i]
        #print(ans)
    print(ans)

if __name__ == "__main__":
    main()

Fixed submission:
import sys
input = sys.stdin.readline
#n = int(input())
#l = list(map(int, input().split()))
'''
A=[]
B=[]
for i in range():
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)'''

n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
bc=[list(map(int,input().split())) for _ in range(m)]
bc.sort(key=lambda x: x[1],reverse=True)
#print(bc)
cnt=0
for i in range(n):
    if bc[cnt][0]>0:
        if bc[cnt][1]>a[i]:
            a[i]=bc[cnt][1]
        bc[cnt][0]-=1
    else:
        cnt+=1
        if cnt==len(bc):
            break
        if bc[cnt][1]>a[i]:
            a[i]=bc[cnt][1]
        bc[cnt][0]-=1
    #print(cnt,bc,a)
print(sum(a))

Buggy python submission (verdict: Runtime Error):
def main():
    import sys
    input = sys.stdin.readline
    #n = int(input())
    #l = list(map(int, input().split()))
    '''
    A=[]
    B=[]
    for i in range():
        a, b = map(int, input().split())
        A.append(a)
        B.append(b)'''

    #import bisect
    n, m = map(int, input().split())
    a = list(map(int, input().split()))
    a.sort()
    #print(a)

    bc=[]
    for i in range(m):
        B = list(map(int, input().split()))
        bc.append(B)
    bc.sort(key=lambda x: x[1])
    #print(bc)

    clis=[]
    for i in range(m)[::-1]:
        clis+=[bc[i][1]] for i in range(bc[i][0])
    #print(clis)
    ans=0
    le=len(clis)
    for i in range(n):
        #print(clis[i],a[i])
        if i<le and clis[i]>a[i]:
            ans+=clis[i]
        else:
            ans+=a[i]
        #print(ans)
    print(ans)

if __name__ == "__main__":
    main()

Fixed submission:
import sys
input = sys.stdin.readline
#n = int(input())
#l = list(map(int, input().split()))
'''
A=[]
B=[]
for i in range():
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)'''

n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
bc=[list(map(int,input().split())) for _ in range(m)]
bc.sort(key=lambda x: x[1],reverse=True)
#print(bc)
cnt=0
for i in range(n):
    if bc[cnt][0]>0:
        if bc[cnt][1]>a[i]:
            a[i]=bc[cnt][1]
        bc[cnt][0]-=1
    else:
        cnt+=1
        if cnt==len(bc):
            break
        if bc[cnt][1]>a[i]:
            a[i]=bc[cnt][1]
        bc[cnt][0]-=1
    #print(cnt,bc,a)
print(sum(a))

Buggy python submission (verdict: Runtime Error):
def main():
    import sys
    input = sys.stdin.readline
    #n = int(input())
    #l = list(map(int, input().split()))
    '''
    A=[]
    B=[]
    for i in range():
        a, b = map(int, input().split())
        A.append(a)
        B.append(b)'''

    #import bisect
    n, m = map(int, input().split())
    a = list(map(int, input().split()))
    a.sort()
    #print(a)

    bc=[]
    for i in range(m):
        B = list(map(int, input().split()))
        bc.append(B)
    bc.sort(key=lambda x: x[1])
    #print(bc)

    clis=[]
    for i in range(m)[::-1]:
        clis+=[bc[i][1]] for i in range(bc[i][0])
    #print(clis)
    ans=0
    le=len(clis)
    for i in range(n):
        #print(clis[i],a[i])
        if i<le and clis[i]>a[i]:
            ans+=clis[i]
        else:
            ans+=a[i]
        #print(ans)
    print(ans)

if __name__ == "__main__":
    main()

Fixed submission:
import sys
input = sys.stdin.readline
#n = int(input())
#l = list(map(int, input().split()))
'''
A=[]
B=[]
for i in range():
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)'''

n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
bc=[list(map(int,input().split())) for _ in range(m)]
bc.sort(key=lambda x: x[1],reverse=True)
#print(bc)
cnt=0
for i in range(n):
    if bc[cnt][0]>0:
        if bc[cnt][1]>a[i]:
            a[i]=bc[cnt][1]
        bc[cnt][0]-=1
    else:
        cnt+=1
        if cnt==len(bc):
            break
        if bc[cnt][1]>a[i]:
            a[i]=bc[cnt][1]
        bc[cnt][0]-=1
    #print(cnt,bc,a)
print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import sys
    input = sys.stdin.readline
    #n = int(input())
    #l = list(map(int, input().split()))
    '''
    A=[]
    B=[]
    for i in range():
        a, b = map(int, input().split())
        A.append(a)
        B.append(b)'''

    #import bisect
    n, m = map(int, input().split())
    a = list(map(int, input().split()))
    a.sort()
    #print(a)

    bc=[]
    for i in range(m):
        B = list(map(int, input().split()))
        bc.append(B)
    bc.sort(key=lambda x: x[1])
    #print(bc)

    clis=[]
    for i in range(m)[::-1]:
        clis+=[bc[i][1]]*bc[i][0]
    #print(clis)
    ans=0
    le=len(clis)
    for i in range(n):
        #print(clis[i],a[i])
        if i<le and clis[i]>a[i]:
            ans+=clis[i]
        else:
            ans+=a[i]
        #print(ans)
    print(ans)

if __name__ == "__main__":
    main()

Fixed submission:
import sys
input = sys.stdin.readline
#n = int(input())
#l = list(map(int, input().split()))
'''
A=[]
B=[]
for i in range():
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)'''

n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
bc=[list(map(int,input().split())) for _ in range(m)]
bc.sort(key=lambda x: x[1],reverse=True)
#print(bc)
cnt=0
for i in range(n):
    if bc[cnt][0]>0:
        if bc[cnt][1]>a[i]:
            a[i]=bc[cnt][1]
        bc[cnt][0]-=1
    else:
        cnt+=1
        if cnt==len(bc):
            break
        if bc[cnt][1]>a[i]:
            a[i]=bc[cnt][1]
        bc[cnt][0]-=1
    #print(cnt,bc,a)
print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import sys
    input = sys.stdin.readline
    #n = int(input())
    #l = list(map(int, input().split()))
    '''
    A=[]
    B=[]
    for i in range():
        a, b = map(int, input().split())
        A.append(a)
        B.append(b)'''

    import bisect
    n, m = map(int, input().split())
    a = list(map(int, input().split()))
    a.sort()
    #print(a)

    bc=[]
    for i in range(m):
        B = list(map(int, input().split()))
        bc.append(B)
    bc.sort(key=lambda x: x[1])
    #print(bc)

    clis=[]
    for i in range(m)[::-1]:
        clis+=[bc[i][1]]*bc[i][0]
    #print(clis)
    ans=0
    le=len(clis)
    for i in range(n):
        #print(clis[i],a[i])
        if i<le and clis[i]>a[i]:
            ans+=clis[i]
        else:
            ans+=a[i]
        #print(ans)
    print(ans)

if __name__ == "__main__":
    main()

Fixed submission:
import sys
input = sys.stdin.readline
#n = int(input())
#l = list(map(int, input().split()))
'''
A=[]
B=[]
for i in range():
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)'''

n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
bc=[list(map(int,input().split())) for _ in range(m)]
bc.sort(key=lambda x: x[1],reverse=True)
#print(bc)
cnt=0
for i in range(n):
    if bc[cnt][0]>0:
        if bc[cnt][1]>a[i]:
            a[i]=bc[cnt][1]
        bc[cnt][0]-=1
    else:
        cnt+=1
        if cnt==len(bc):
            break
        if bc[cnt][1]>a[i]:
            a[i]=bc[cnt][1]
        bc[cnt][0]-=1
    #print(cnt,bc,a)
print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import sys
    input = sys.stdin.readline
    #n = int(input())
    #l = list(map(int, input().split()))
    '''
    A=[]
    B=[]
    for i in range():
        a, b = map(int, input().split())
        A.append(a)
        B.append(b)'''

    import bisect
    n, m = map(int, input().split())
    a = list(map(int, input().split()))
    a.sort()
    #print(a)

    bc=[]
    for i in range(m):
        B = list(map(int, input().split()))
        bc.append(B)
    bc.sort(key=lambda x: x[1])
    #print(bc)

    clis=[]
    for i in range(m)[::-1]:
        clis+=[bc[i][1]]*bc[i][0]
    #print(clis)
    ans=0
    le=len(clis)
    for i in range(n):
        #print(clis[i],a[i])
        if i<le and clis[i]>a[i]:
            ans+=clis[i]
        else:
            ans+=a[i]
        #print(ans)
    print(ans)

if __name__ == "__main__":
    main()

Fixed submission:
import sys
input = sys.stdin.readline
#n = int(input())
#l = list(map(int, input().split()))
'''
A=[]
B=[]
for i in range():
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)'''

n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
bc=[list(map(int,input().split())) for _ in range(m)]
bc.sort(key=lambda x: x[1],reverse=True)
#print(bc)
cnt=0
for i in range(n):
    if bc[cnt][0]>0:
        if bc[cnt][1]>a[i]:
            a[i]=bc[cnt][1]
        bc[cnt][0]-=1
    else:
        cnt+=1
        if cnt==len(bc):
            break
        if bc[cnt][1]>a[i]:
            a[i]=bc[cnt][1]
        bc[cnt][0]-=1
    #print(cnt,bc,a)
print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
#n = int(input())
#l = list(map(int, input().split()))
'''
A=[]
B=[]
for i in range():
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)'''

import bisect
n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
#print(a)

bc=[]
for i in range(m):
    B = list(map(int, input().split()))
    bc.append(B)
bc.sort(key=lambda x: x[1])
#print(bc)

clis=[]
for i in range(m)[::-1]:
    clis+=[bc[i][1]]*bc[i][0]
#print(clis)
ans=0
le=len(clis)
for i in range(n):
    #print(clis[i],a[i])
    if i<le and clis[i]>a[i]:
        ans+=clis[i]
    else:
        ans+=a[i]
    #print(ans)
print(ans)


Fixed submission:
import sys
input = sys.stdin.readline
#n = int(input())
#l = list(map(int, input().split()))
'''
A=[]
B=[]
for i in range():
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)'''

n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
bc=[list(map(int,input().split())) for _ in range(m)]
bc.sort(key=lambda x: x[1],reverse=True)
#print(bc)
cnt=0
for i in range(n):
    if bc[cnt][0]>0:
        if bc[cnt][1]>a[i]:
            a[i]=bc[cnt][1]
        bc[cnt][0]-=1
    else:
        cnt+=1
        if cnt==len(bc):
            break
        if bc[cnt][1]>a[i]:
            a[i]=bc[cnt][1]
        bc[cnt][0]-=1
    #print(cnt,bc,a)
print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
#n = int(input())
#l = list(map(int, input().split()))
'''
A=[]
B=[]
for i in range():
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)'''

import bisect
n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
#print(a)

bc=[]
for i in range(m):
    B = list(map(int, input().split()))
    bc.append(B)
bc.sort(key=lambda x: x[1])
#print(bc)

clis=[]
for i in range(m)[::-1]:
    clis+=[bc[i][1]]*bc[i][0]
#print(clis)
ans=0
le=len(clis)
for i in range(n):
    #print(clis[i],a[i])
    if i<le and clis[i]>a[i]:
        ans+=clis[i]
    else:
        ans+=a[i]
    #print(ans)
print(ans)


Fixed submission:
import sys
input = sys.stdin.readline
#n = int(input())
#l = list(map(int, input().split()))
'''
A=[]
B=[]
for i in range():
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)'''

n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
bc=[list(map(int,input().split())) for _ in range(m)]
bc.sort(key=lambda x: x[1],reverse=True)
#print(bc)
cnt=0
for i in range(n):
    if bc[cnt][0]>0:
        if bc[cnt][1]>a[i]:
            a[i]=bc[cnt][1]
        bc[cnt][0]-=1
    else:
        cnt+=1
        if cnt==len(bc):
            break
        if bc[cnt][1]>a[i]:
            a[i]=bc[cnt][1]
        bc[cnt][0]-=1
    #print(cnt,bc,a)
print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
#n = int(input())
#l = list(map(int, input().split()))
'''
A=[]
B=[]
for i in range():
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)'''

import bisect
n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
#print(a)

bc=[]
for i in range(m):
    B = list(map(int, input().split()))
    bc.append(B)
bc.sort(key=lambda x: x[1])
#print(bc)

clis=[]
for i in range(m)[::-1]:
    clis+=[bc[i][1]]*bc[i][0]
#print(clis)

ans=0
for i in range(n):
    #print(clis[i],a[i])
    if i<len(clis) and clis[i]>a[i]:
        ans+=clis[i]
    else:
        ans+=a[i]
    #print(ans)
print(ans)


Fixed submission:
import sys
input = sys.stdin.readline
#n = int(input())
#l = list(map(int, input().split()))
'''
A=[]
B=[]
for i in range():
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)'''

n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
bc=[list(map(int,input().split())) for _ in range(m)]
bc.sort(key=lambda x: x[1],reverse=True)
#print(bc)
cnt=0
for i in range(n):
    if bc[cnt][0]>0:
        if bc[cnt][1]>a[i]:
            a[i]=bc[cnt][1]
        bc[cnt][0]-=1
    else:
        cnt+=1
        if cnt==len(bc):
            break
        if bc[cnt][1]>a[i]:
            a[i]=bc[cnt][1]
        bc[cnt][0]-=1
    #print(cnt,bc,a)
print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
#n = int(input())
#l = list(map(int, input().split()))
'''
A=[]
B=[]
for i in range():
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)'''

import bisect
n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
#print(a)

bc=[]
for i in range(m):
    B = list(map(int, input().split()))
    bc.append(B)
bc.sort(key=lambda x: x[1])
#print(bc)

for i in range(m):
    x = bisect.bisect_left(a, bc[i][1])
    if x!=0:
        a += [bc[i][1]]*min(bc[i][0],x)
    #print(a)
a.sort()
print(sum(a[len(a)-n:]))


Fixed submission:
import sys
input = sys.stdin.readline
#n = int(input())
#l = list(map(int, input().split()))
'''
A=[]
B=[]
for i in range():
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)'''

n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
bc=[list(map(int,input().split())) for _ in range(m)]
bc.sort(key=lambda x: x[1],reverse=True)
#print(bc)
cnt=0
for i in range(n):
    if bc[cnt][0]>0:
        if bc[cnt][1]>a[i]:
            a[i]=bc[cnt][1]
        bc[cnt][0]-=1
    else:
        cnt+=1
        if cnt==len(bc):
            break
        if bc[cnt][1]>a[i]:
            a[i]=bc[cnt][1]
        bc[cnt][0]-=1
    #print(cnt,bc,a)
print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
#n = int(input())
#l = list(map(int, input().split()))
'''
A=[]
B=[]
for i in range():
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)'''

import bisect
n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
#print(a)

bc=[]
for i in range(m):
    B = list(map(int, input().split()))
    bc.append(B)
bc.sort(key=lambda x: x[1])
#print(bc)

for i in range(m):
    x = bisect.bisect_left(a, bc[i][1])
    if x!=0:
        a += [bc[i][1]]*min(bc[i][0],x)
    #print(a)
a.sort()
print(sum(a[len(a)-n:]))

Fixed submission:
import sys
input = sys.stdin.readline
#n = int(input())
#l = list(map(int, input().split()))
'''
A=[]
B=[]
for i in range():
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)'''

n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
bc=[list(map(int,input().split())) for _ in range(m)]
bc.sort(key=lambda x: x[1],reverse=True)
#print(bc)
cnt=0
for i in range(n):
    if bc[cnt][0]>0:
        if bc[cnt][1]>a[i]:
            a[i]=bc[cnt][1]
        bc[cnt][0]-=1
    else:
        cnt+=1
        if cnt==len(bc):
            break
        if bc[cnt][1]>a[i]:
            a[i]=bc[cnt][1]
        bc[cnt][0]-=1
    #print(cnt,bc,a)
print(sum(a))