Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
A = list(map(int, input().split()))
ch = []
for _ in range(M):
    b, c = map(int, input().split())
    ch.append([c, b])

A.sort()
ch.sort(reverse = True)


n = -1
result = 0
fin = 0
for c, b in ch:
    if fin == 1:
        break

    n += b
    if n > N:
        for num in A[n-b+1:]:
            if c > num:
                result += c
            else:
                result += num
            fin += 1
        break
        
    if c < A[n]:
        for i, j in enumerate(A[n-b+1:]):
            if c < j:
                result += c*i + sum(A[n-b+i+1:])
                fin += 1
                break
    else:
        result += b * c


if not fin == 1:
    result += sum(A[n+1:])



print(result)


Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
ch = []
for _ in range(M):
    b, c = map(int, input().split())
    ch.append([c, b])

A.sort()
ch.sort(reverse = True)


n = -1
result = 0
fin = 0
for c, b in ch:
    if fin == 1:
        break

    n += b

    if n >= N:
        for num in A[n-b+1:]:
            if c > num:
                result += c
            else:
                result += num
            fin += 1
        break

    if c < A[n]:
        for i, j in enumerate(A[n-b+1:]):
            if c < j:
                result += c*i + sum(A[n-b+i+1:])
                fin += 1
                break
    else:
        result += b * c


if not fin == 1:
    result += sum(A[n+1:])



print(result)

Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
A = list(map(int, input().split()))
ch = []
for _ in range(M):
    b, c = map(int, input().split())
    ch.append([c, b])

A.sort()
ch.sort(reverse = True)


n = -1
result = 0
fin = 0
for c, b in ch:
    if fin == 1:
        break

    n += b
    if n > N:
        for num in A[n-b+i+1:]:
            if c > num:
                result += c
            else:
                result += num

    if c < A[n]:
        for i, j in enumerate(A[n-b+1:]):
            if c < j:
                result += c*i + sum(A[n-b+i+1:])
                fin += 1
                break
    else:
        result += b * c


if not fin == 1:
    result += sum(A[n+1:])



print(result)


Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
ch = []
for _ in range(M):
    b, c = map(int, input().split())
    ch.append([c, b])

A.sort()
ch.sort(reverse = True)


n = -1
result = 0
fin = 0
for c, b in ch:
    if fin == 1:
        break

    n += b

    if n >= N:
        for num in A[n-b+1:]:
            if c > num:
                result += c
            else:
                result += num
            fin += 1
        break

    if c < A[n]:
        for i, j in enumerate(A[n-b+1:]):
            if c < j:
                result += c*i + sum(A[n-b+i+1:])
                fin += 1
                break
    else:
        result += b * c


if not fin == 1:
    result += sum(A[n+1:])



print(result)

Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
A = list(map(int, input().split()))
ch = []
for _ in range(M):
    b, c = map(int, input().split())
    ch.append([c, b])

A.sort()
ch.sort(reverse = True)


n = -1
result = 0
fin = 0
for c, b in ch:
    if fin == 1:
        break

    n += b
    if c < A[n]:
        for i, j in enumerate(A[n-b+1:]):
            if c < j:
                result += c*i + sum(A[n-b+i+1:])
                fin += 1
                break
    else:
        result += b * c


if not fin == 1:
    result += sum(A[n+1:])



print(result)


Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
ch = []
for _ in range(M):
    b, c = map(int, input().split())
    ch.append([c, b])

A.sort()
ch.sort(reverse = True)


n = -1
result = 0
fin = 0
for c, b in ch:
    if fin == 1:
        break

    n += b

    if n >= N:
        for num in A[n-b+1:]:
            if c > num:
                result += c
            else:
                result += num
            fin += 1
        break

    if c < A[n]:
        for i, j in enumerate(A[n-b+1:]):
            if c < j:
                result += c*i + sum(A[n-b+i+1:])
                fin += 1
                break
    else:
        result += b * c


if not fin == 1:
    result += sum(A[n+1:])



print(result)