Buggy python submission (verdict: Time Limit Exceeded):
import bisect
import sys
sys.setrecursionlimit(10**9)

def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))

class Counter:
    def __init__(self):
        self.dict = {}

    def add(self,x):
        if x in self.dict: self.dict[x] += 1
        else: self.dict[x] = 1

    def decrement(self,x):
        self.dict[x] -= 1
        if self.dict[x] <= 0:
            del self.dict[x]

    def get_dict(self):
        return self.dict


INF=10**20
def main():
    N,M=mi()
    A=list(mi())

    X = []
    counter = Counter()
    for a in A:
        counter.add(a)

    # count = counter.get_dict()
    # for a,count in count_dic.items():
    #     X.append((a,count))
    

    
    # X.sort()
    keys = list(counter.get_dict().keys()) # カードの数字
    keys.sort()

    # print(counter.get_dict())
    for i in range(M):
        b,c = mi()
        j = bisect.bisect_left(keys,c)

        rest = b
        for k in range(j+1):
            if not 0<=k<len(keys): continue
            key = keys[k]
            if key >= c: continue

            count = counter.get_dict()[key]
            if count < rest:
                counter.get_dict()[key] = 0
                rest -= count
                counter.add(c)
                counter.get_dict()[c] += -1 + count

            else:
                counter.get_dict()[key] = count - rest
                counter.add(c)
                counter.get_dict()[c] += -1 + rest
                rest = 0
            
            if rest == 0:
                break


            # if key < c:
            #     counter.decrement(key)
            #     counter.add(c)

        keys = list(counter.get_dict().keys())
        keys.sort()
        # print(counter.get_dict())
    
    ans = 0
    for a, count in counter.get_dict().items():
        # print(a,count)
        ans += a * count
    
    print(ans)

        


if __name__ == "__main__":
    main()

Fixed submission:
import sys
sys.setrecursionlimit(10**9)

def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))

class Counter:
    def __init__(self):
        self.dict = {}

    def add(self,x):
        if x in self.dict: self.dict[x] += 1
        else: self.dict[x] = 1

    def decrement(self,x):
        self.dict[x] -= 1
        if self.dict[x] <= 0:
            del self.dict[x]

    def get_dict(self):
        return self.dict


INF=10**20
def main():
    N,M=mi()
    A=list(mi())

    X = []
    for i in range(M):
        b,c=mi()
        X.append((c,b))
    
    X.sort(reverse=True)
    A.sort(reverse=True)

    xi = 0
    b = 0

    newA = []
    while A:
        if not xi < len(X): break
        
        if b == 0: c,b = X[xi]
        a = A.pop() # 小さい順に

        # print(a,b,c)
        if a < c:   
            newA.append(c)
        else:
            A.append(a)
            b = 0

        b -= 1 
        if b <= 0:
            xi += 1
    
    # print(A)
    # print(newA)
    print(sum(newA) + sum(A))
        

        




if __name__ == "__main__":
    main()