Buggy python submission (verdict: Runtime Error):
#!/usr/bin/env python3
import numpy as np


def solv(n, m, a, s):
    a = np.sort(a)
    s = sorted(s, key=lambda x: -x[1])
    i = 0
    for (b, c) in s:
        for _ in range(b):
            if c <= a[i]:
                return a.sum()
            elif i >= n:
                return a.sum()
            a[i] = c
            i += 1
    return a.sum()


if __name__ == '__main__':

    n, m = map(int, input().split())
    a = np.fromiter(map(int, input().split()), dtype='uint64')
    s = []
    for _ in range(m):
        b, c = map(int, input().split())
        s.append((b, c))

    ans = solv(n, m, a, s)

    print(ans)


Fixed submission:
#!/usr/bin/env python3


def solv(n, m, a, s):
    a = sorted(a)
    s = sorted(s, key=lambda x: -x[1])
    i = 0
    sum0 = 0
    for (b, c) in s:
        if (i+b) < n and a[i+b] < c:
            sum0 += b*c
            i += b
        else:
            for _ in range(b):
                if i >= n:
                    return sum0
                elif c <= a[i]:
                    return sum0 + sum(a[i:])
                sum0 += c
                i += 1
    if i >= n:
        return sum0
    return sum0 + sum(a[i:])


if __name__ == '__main__':

    n, m = map(int, input().split())
    a = map(int, input().split())
    s = []
    for _ in range(m):
        b, c = map(int, input().split())
        s.append((b, c))

    ans = solv(n, m, a, s)

    print(ans)

Buggy python submission (verdict: Runtime Error):
 
def solv(n, m, a, s):
    a = sorted(a)
    s = sorted(s, key=lambda x: -x[1])
    i = 0
    for (b, c) in s:
        for _ in range(b):
            if c <= a[i]:
                return sum(a)
            elif i >= n:
                return sum(a)
            a[i] = c
            i += 1
    return sum(a)
 
 
if __name__ == '__main__':
 
    n, m = map(int, input().split())
    a = map(int, input().split())
    s = []
    for _ in range(m):
        b, c = map(int, input().split())
        s.append((b, c))
 
    ans = solv(n, m, a, s)
 
    print(ans)

Fixed submission:
#!/usr/bin/env python3


def solv(n, m, a, s):
    a = sorted(a)
    s = sorted(s, key=lambda x: -x[1])
    i = 0
    sum0 = 0
    for (b, c) in s:
        if (i+b) < n and a[i+b] < c:
            sum0 += b*c
            i += b
        else:
            for _ in range(b):
                if i >= n:
                    return sum0
                elif c <= a[i]:
                    return sum0 + sum(a[i:])
                sum0 += c
                i += 1
    if i >= n:
        return sum0
    return sum0 + sum(a[i:])


if __name__ == '__main__':

    n, m = map(int, input().split())
    a = map(int, input().split())
    s = []
    for _ in range(m):
        b, c = map(int, input().split())
        s.append((b, c))

    ans = solv(n, m, a, s)

    print(ans)

Buggy python submission (verdict: Runtime Error):
#!/usr/bin/env python3


def solv(n, m, a, s):
    a = sorted(a)
    s = sorted(s, key=lambda x: -x[1])
    i = 0
    for (b, c) in s:
        for _ in range(b):
            if c <= a[i]:
                return sum(a)
            elif i >= n:
                return sum(a)
            a[i] = c
            i += 1
    return sum(a)


if __name__ == '__main__':

    n, m = map(int, input().split())
    a = map(int, input().split())
    s = []
    for _ in range(m):
        b, c = map(int, input().split())
        s.append((b, c))

    ans = solv(n, m, a, s)

    print(ans)


Fixed submission:
#!/usr/bin/env python3


def solv(n, m, a, s):
    a = sorted(a)
    s = sorted(s, key=lambda x: -x[1])
    i = 0
    sum0 = 0
    for (b, c) in s:
        if (i+b) < n and a[i+b] < c:
            sum0 += b*c
            i += b
        else:
            for _ in range(b):
                if i >= n:
                    return sum0
                elif c <= a[i]:
                    return sum0 + sum(a[i:])
                sum0 += c
                i += 1
    if i >= n:
        return sum0
    return sum0 + sum(a[i:])


if __name__ == '__main__':

    n, m = map(int, input().split())
    a = map(int, input().split())
    s = []
    for _ in range(m):
        b, c = map(int, input().split())
        s.append((b, c))

    ans = solv(n, m, a, s)

    print(ans)