Buggy python submission (verdict: Time Limit Exceeded):
# 2019-11-13 08:30:54(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# from scipy.misc import comb # float
# import numpy as np 

def main():
    n, m = [int(x) for x in sys.stdin.readline().split()]
    a = [int(x) for x in sys.stdin.readline().split()]
    a.sort()

    replace = []
    for _ in range(m):
        bi, ci = [int(x) for x in sys.stdin.readline().split()]
        replace += [ci] * bi
    
    replace.sort(reverse=True)

    for i in range(n):
        if i < len(replace):
            if a[i]< replace[i]:
                a[i] = replace[i]
                continue
            else:
                break
        else:
            break
    
    print(sum(a))



if __name__ == "__main__":
    main()


Fixed submission:
# 2019-11-13 08:30:54(JST)
import sys
import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# from scipy.misc import comb # float
# import numpy as np 

def main():
    n, m = [int(x) for x in sys.stdin.readline().split()]
    a = [int(x) for x in sys.stdin.readline().split()]

    a = list(collections.Counter(a).items())

    for _ in range(m):
        b, c = [int(x) for x in sys.stdin.readline().split()]
        a.append((c,b))
    
    a.sort(reverse=True)
    
    total = 0
    count = 0
    for i in range(n+m): # max
        select = a[i]
        for j in range(select[1], 0, -1):
            if count + j > n:
                continue
            else:
                count += j
                total += select[0] * j
                break
        if count == n:
            break
    
    print(total)

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
# 2019-11-13 08:30:54(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# from scipy.misc import comb # float
# import numpy as np 

def main():
    n, m = [int(x) for x in sys.stdin.readline().split()]
    a = [int(x) for x in sys.stdin.readline().split()]
    a.sort()

    bc = []
    for _ in range(m):
        bi, ci = [int(x) for x in sys.stdin.readline().split()]
        bc.append([bi, ci])
    
    bc.sort(reverse=True, key=lambda x: x[1])
    
    res = []
    for i in range(m):
        if a:
            index = bi_l(a, bc[i][1])
            if index == 0:
                res += a
                break
            else:
                if index > bc[i][0]:
                    a = a[bc[i][0]:]
                    res += [bc[i][1]] * bc[i][0]
                else:
                    a = a[index:]
                    res += [bc[i][1]] * index
        else:
            break
    else:
        res += a
    print(sum(res))


if __name__ == "__main__":
    main()


Fixed submission:
# 2019-11-13 08:30:54(JST)
import sys
import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# from scipy.misc import comb # float
# import numpy as np 

def main():
    n, m = [int(x) for x in sys.stdin.readline().split()]
    a = [int(x) for x in sys.stdin.readline().split()]

    a = list(collections.Counter(a).items())

    for _ in range(m):
        b, c = [int(x) for x in sys.stdin.readline().split()]
        a.append((c,b))
    
    a.sort(reverse=True)
    
    total = 0
    count = 0
    for i in range(n+m): # max
        select = a[i]
        for j in range(select[1], 0, -1):
            if count + j > n:
                continue
            else:
                count += j
                total += select[0] * j
                break
        if count == n:
            break
    
    print(total)

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
# 2019-11-13 08:30:54(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# from scipy.misc import comb # float
# import numpy as np 

def main():
    n, m = [int(x) for x in sys.stdin.readline().split()]
    a = [int(x) for x in sys.stdin.readline().split()]
    a.sort()

    bc = []
    for _ in range(m):
        bi, ci = [int(x) for x in sys.stdin.readline().split()]
        bc.append([bi, ci])
    
    bc.sort(reverse=True, key=lambda x: x[1])
    
    for i in range(m):
        index = bi_l(a, bc[i][1])
        if index == 0:
            break
        else:
            if index > bc[i][0]:
                a = [bc[i][1]] * bc[i][0] + a[bc[i][0]:]
            else:
                a = [bc[i][1]] * index + a[index:]
            a.sort()

    print(sum(a))


if __name__ == "__main__":
    main()


Fixed submission:
# 2019-11-13 08:30:54(JST)
import sys
import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# from scipy.misc import comb # float
# import numpy as np 

def main():
    n, m = [int(x) for x in sys.stdin.readline().split()]
    a = [int(x) for x in sys.stdin.readline().split()]

    a = list(collections.Counter(a).items())

    for _ in range(m):
        b, c = [int(x) for x in sys.stdin.readline().split()]
        a.append((c,b))
    
    a.sort(reverse=True)
    
    total = 0
    count = 0
    for i in range(n+m): # max
        select = a[i]
        for j in range(select[1], 0, -1):
            if count + j > n:
                continue
            else:
                count += j
                total += select[0] * j
                break
        if count == n:
            break
    
    print(total)

if __name__ == "__main__":
    main()