Buggy python submission (verdict: Time Limit Exceeded):
import sys
input=sys.stdin.readline
n,m=map(int,input().split())
a=list(map(int,input().split()))
a=sorted(a)
k=[[int(i) for i in input().split()] for i in range(m)]
k=sorted(k,key=lambda x:-x[1])

now=0
flag=True
for x,y in k:
    if flag:
        for i in range(x):
            if flag and i+now<n:
                if y>a[i+now]:
                    a[i+now]=y
                else:
                    flag=False
    now+=x
print(sum(a))

Fixed submission:
def main():
    import sys
    input=sys.stdin.readline
    n,m=map(int,input().split())
    a=list(map(int,input().split()))
    a=sorted(a)
    k=[[int(i) for i in input().split()] for i in range(m)]
    k=sorted(k,key=lambda x:-x[1])
    
    now=0
    flag=True
    for x,y in k:
        if flag:
            for i in range(x):
                if i+now>=n:
                    print(sum(a))
                    exit()
                if flag:
                    if y>a[i+now]:
                        a[i+now]=y
                    else:
                        flag=False
        now+=x
    print(sum(a))
if __name__=="__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input=sys.stdin.readline
n,m=map(int,input().split())
a=list(map(int,input().split()))
a=sorted(a)
k=[[int(i) for i in input().split()] for i in range(m)]
k=sorted(k,key=lambda x:-x[1])

now=0
flag=True
for x,y in k:
    if flag:
        for i in range(x):
            if flag and i+now<n:
                if y>a[i+now]:
                    a[i+now]=y
                else:
                    flag=False
    now+=x
print(sum(a))

Fixed submission:
def main():
    import sys
    input=sys.stdin.readline
    n,m=map(int,input().split())
    a=list(map(int,input().split()))
    a=sorted(a)
    k=[[int(i) for i in input().split()] for i in range(m)]
    k=sorted(k,key=lambda x:-x[1])
    
    now=0
    flag=True
    for x,y in k:
        if flag:
            for i in range(x):
                if i+now>=n:
                    print(sum(a))
                    exit()
                if flag:
                    if y>a[i+now]:
                        a[i+now]=y
                    else:
                        flag=False
        now+=x
    print(sum(a))
if __name__=="__main__":
    main()

Buggy python submission (verdict: Runtime Error):
import sys
input=sys.stdin.readline
n,m=map(int,input().split())
a=list(map(int,input().split()))
a=sorted(a)
k=[[int(i) for i in input().split()] for i in range(m)]
k=sorted(k,key=lambda x:-x[1])

now=0
flag=True
for x,y in k:
    if flag:
        for i in range(x):
            if flag:
                if y>a[i+now]:
                    a[i+now]=y
                else:
                    flag=False
    now+=x
print(sum(a))

Fixed submission:
def main():
    import sys
    input=sys.stdin.readline
    n,m=map(int,input().split())
    a=list(map(int,input().split()))
    a=sorted(a)
    k=[[int(i) for i in input().split()] for i in range(m)]
    k=sorted(k,key=lambda x:-x[1])
    
    now=0
    flag=True
    for x,y in k:
        if flag:
            for i in range(x):
                if i+now>=n:
                    print(sum(a))
                    exit()
                if flag:
                    if y>a[i+now]:
                        a[i+now]=y
                    else:
                        flag=False
        now+=x
    print(sum(a))
if __name__=="__main__":
    main()