Buggy python submission (verdict: Time Limit Exceeded):
#どっちで出すか注意, rstrip注意
#提出前に見返すこと！
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate
    #from itertools import product
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

    n,m = map(int, input().split())
    a = list(map(int, input().split()))
    for _ in range(m):
        b,c = map(int, input().split())
        a += [c]*b
    a.sort(reverse=1)
    print(sum(a[:n]))

if __name__ == '__main__':
    main()


Fixed submission:
#どっちで出すか注意, rstrip注意
#提出前に見返すこと！
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate
    #from itertools import product
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

    n,m = map(int, input().split())
    a = list(map(int, input().split()))
    bc = [list(map(int, input().split())) for _ in range(m)]
    bc.sort(key=lambda a: a[1])
    a.sort()
    res = 0
    cnt = 0
    while cnt<n:
        if not bc:
            res += sum(a[cnt-n:])
            break
        if a[-1] >= bc[-1][1]:
            res += a.pop()
            cnt += 1
        else:
            d, e = bc.pop()
            if n-cnt >= d:
                res += d*e
                cnt += d
            else:
                res += (n-cnt)*e
                break

    print(res)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
#どっちで出すか注意, rstrip注意
#提出前に見返すこと！
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate
    #from itertools import product
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

    n,m = map(int, input().split())
    a = list(map(int, input().split()))
    a.sort()
    for _ in range(m):
        b,c = map(int, input().split())
        i = bisect_left(a, c)
        if b >= i:
            a = [c]*i + a[i:]
        else:
            a = a[b:i] + [c]*b +a[i:]
    print(sum(a))


if __name__ == '__main__':
    main()


Fixed submission:
#どっちで出すか注意, rstrip注意
#提出前に見返すこと！
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate
    #from itertools import product
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

    n,m = map(int, input().split())
    a = list(map(int, input().split()))
    bc = [list(map(int, input().split())) for _ in range(m)]
    bc.sort(key=lambda a: a[1])
    a.sort()
    res = 0
    cnt = 0
    while cnt<n:
        if not bc:
            res += sum(a[cnt-n:])
            break
        if a[-1] >= bc[-1][1]:
            res += a.pop()
            cnt += 1
        else:
            d, e = bc.pop()
            if n-cnt >= d:
                res += d*e
                cnt += d
            else:
                res += (n-cnt)*e
                break

    print(res)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
#どっちで出すか注意, rstrip注意
#提出前に見返すこと！
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate
    #from itertools import product
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

    n,m = map(int, input().split())
    a = list(map(int, input().split()))
    a.sort()
    for _ in range(m):
        b,c = map(int, input().split())
        i = bisect_left(a, c)
        if b >= i:
            a = [c]*i + a[i:]
        else:
            a = a[b:i] + [c]*b +a[i:]
    print(sum(a))


if __name__ == '__main__':
    main()


Fixed submission:
#どっちで出すか注意, rstrip注意
#提出前に見返すこと！
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate
    #from itertools import product
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

    n,m = map(int, input().split())
    a = list(map(int, input().split()))
    bc = [list(map(int, input().split())) for _ in range(m)]
    bc.sort(key=lambda a: a[1])
    a.sort()
    res = 0
    cnt = 0
    while cnt<n:
        if not bc:
            res += sum(a[cnt-n:])
            break
        if a[-1] >= bc[-1][1]:
            res += a.pop()
            cnt += 1
        else:
            d, e = bc.pop()
            if n-cnt >= d:
                res += d*e
                cnt += d
            else:
                res += (n-cnt)*e
                break

    print(res)


if __name__ == '__main__':
    main()