Buggy python submission (verdict: Time Limit Exceeded):
from bisect import bisect_left
from collections import deque

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

a.sort()
bc = sorted(bc, key=lambda x:x[1])

l = [0 for _ in range(n+1)]
for i in range(1,n+1):
	l[i] = a[i-1]
for i in range(n):
	l[i+1] += l[i]
#print(a,sum(a))
#print(bc)
#print(l)
ans = l[-1]
num = 0
loop = 0
A = deque(a)
while True:
	if len(bc) == 0:
		break
	tmp = bc.pop()
	b = tmp[0]
	c = tmp[1]
	index = bisect_left(a,c)
	if index == 0:
		break
	if b >= index:
		ans -= l[index+num] - l[num]
		ans += index * c
		num += index
		loop = index
	else:
		ans -= l[b+num] - l[num]
		ans += b * c
		num += b
		loop = b
	for _ in range(loop):
		A.popleft()
	a = list(A)
print(ans)


Fixed submission:
import sys

def main():
    input = sys.stdin.readline

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

    a.sort()
    a = tuple(a)

    bc.sort(key=lambda x: x[1], reverse=True)

    num = 0
    ans = 0

    for i in range(m):
        b = bc[i][0]
        c = bc[i][1]
        for j in range(num,min(num+b,n)):
            if c > a[j]:
                ans += c
            else:
                ans += a[j]

        num += b
        if num >= n:
            break

    if num < n:
        print(ans + sum(a[num:]))
    else:
        print(ans)
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from bisect import bisect_left

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

a.sort()
bc = sorted(bc, key=lambda x:x[1])

l = [0 for _ in range(n+1)]
for i in range(1,n+1):
	l[i] = a[i-1]
for i in range(n):
	l[i+1] += l[i]
#print(a,sum(a))
#print(bc)
#print(l)
ans = l[-1]
num = 0
while True:
	if len(bc) == 0:
		break
	tmp = bc.pop()
	b = tmp[0]
	c = tmp[1]
	index = bisect_left(a,c)
	if index == 0:
		break
	if b >= index:
		ans -= l[index+num] - l[num]
		ans += index * c
		num += index
		a = a[index:]
	else:
		ans -= l[b+num] - l[num]
		ans += b * c
		a = a[b:]
		num += b

print(ans)


Fixed submission:
import sys

def main():
    input = sys.stdin.readline

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

    a.sort()
    a = tuple(a)

    bc.sort(key=lambda x: x[1], reverse=True)

    num = 0
    ans = 0

    for i in range(m):
        b = bc[i][0]
        c = bc[i][1]
        for j in range(num,min(num+b,n)):
            if c > a[j]:
                ans += c
            else:
                ans += a[j]

        num += b
        if num >= n:
            break

    if num < n:
        print(ans + sum(a[num:]))
    else:
        print(ans)
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
import bisect
from collections import deque

n,m = map(int,input().split())
a = list(map(int,input().split()))
bc = []
for _ in range(m):
    tmp = list(map(int,input().split()))
    bc.append(tmp[::-1])

a.sort()
a = deque(a)
bc.sort(reverse=True)

ans = 0
num = 0
for i in range(m):
    index = bisect.bisect_left(a,bc[i][0])
    if index == 0:
        break
    if bc[i][1] > index:
        ans += bc[i][0] * index
        num += index
    else:
        ans += bc[i][0] * bc[i][1]
        num += bc[i][1]
    for i in range(num):
        a.popleft()

print(ans + sum(a))


Fixed submission:
import sys

def main():
    input = sys.stdin.readline

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

    a.sort()
    a = tuple(a)

    bc.sort(key=lambda x: x[1], reverse=True)

    num = 0
    ans = 0

    for i in range(m):
        b = bc[i][0]
        c = bc[i][1]
        for j in range(num,min(num+b,n)):
            if c > a[j]:
                ans += c
            else:
                ans += a[j]

        num += b
        if num >= n:
            break

    if num < n:
        print(ans + sum(a[num:]))
    else:
        print(ans)
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import bisect

n,m = map(int,input().split())
a = list(map(int,input().split()))
bc = []
for _ in range(m):
    tmp = list(map(int,input().split()))
    bc.append(tmp[::-1])

a.sort()
bc.sort(reverse=True)

ans = 0
num = 0
for i in range(m):
    index = bisect.bisect_left(a[num:],bc[i][0])
    if index == 0:
        break
    if bc[i][1] > index:
        ans += bc[i][0] * index
        num += index
    else:
        ans += bc[i][0] * bc[i][1]
        num += bc[i][1]

print(ans + sum(a[num:]))


Fixed submission:
import sys

def main():
    input = sys.stdin.readline

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

    a.sort()
    a = tuple(a)

    bc.sort(key=lambda x: x[1], reverse=True)

    num = 0
    ans = 0

    for i in range(m):
        b = bc[i][0]
        c = bc[i][1]
        for j in range(num,min(num+b,n)):
            if c > a[j]:
                ans += c
            else:
                ans += a[j]

        num += b
        if num >= n:
            break

    if num < n:
        print(ans + sum(a[num:]))
    else:
        print(ans)
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import bisect

n,m = map(int,input().split())
a = list(map(int,input().split()))
bc = []
for _ in range(m):
    tmp = list(map(int,input().split()))
    bc.append(tmp[::-1])

a.sort()
bc.sort(reverse=True)

ans = 0
num = 0
for i in range(m):
    index = bisect.bisect_left(a[num:],bc[i][0])
    if index == 0:
        break
    if bc[i][1] > index:
        ans += bc[i][0] * index
        num += index
    else:
        ans += bc[i][0] * bc[i][1]
        num += bc[i][1]

print(ans + sum(a[num:]))


Fixed submission:
import sys

def main():
    input = sys.stdin.readline

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

    a.sort()
    a = tuple(a)

    bc.sort(key=lambda x: x[1], reverse=True)

    num = 0
    ans = 0

    for i in range(m):
        b = bc[i][0]
        c = bc[i][1]
        for j in range(num,min(num+b,n)):
            if c > a[j]:
                ans += c
            else:
                ans += a[j]

        num += b
        if num >= n:
            break

    if num < n:
        print(ans + sum(a[num:]))
    else:
        print(ans)
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import bisect


def main():

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

    a.sort()
    a = tuple(a)

    bc.sort(key=lambda x: x[1], reverse=True)

    num = 0
    ans = 0

    for i in range(m):
        b = bc[i][0]
        c = bc[i][1]
        index = bisect.bisect_left(a[num:],c)
        if index == 0:
            break
        elif index > b:
            ans += c * b
        else:
            ans += c * index
            ans += sum(a[num+index:num+b])

        num += b
        if num >= n:
            break

    if num < n:
        print(ans + sum(a[num:]))
    else:
        print(ans)
if __name__ == '__main__':
    input = sys.stdin.readline
    main()


Fixed submission:
import sys

def main():
    input = sys.stdin.readline

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

    a.sort()
    a = tuple(a)

    bc.sort(key=lambda x: x[1], reverse=True)

    num = 0
    ans = 0

    for i in range(m):
        b = bc[i][0]
        c = bc[i][1]
        for j in range(num,min(num+b,n)):
            if c > a[j]:
                ans += c
            else:
                ans += a[j]

        num += b
        if num >= n:
            break

    if num < n:
        print(ans + sum(a[num:]))
    else:
        print(ans)
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import bisect

input = sys.stdin.readline

def main():

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

    a.sort()
    a = tuple(a)

    bc.sort(key=lambda x: x[1], reverse=True)

    num = 0
    ans = 0

    for i in range(m):
        b = bc[i][0]
        c = bc[i][1]
        index = bisect.bisect_left(a[num:],c)
        if index == 0:
            break
        elif index > b:
            ans += c * b
        else:
            ans += c * index
            ans += sum(a[num+index:num+b])

        num += b
        if num >= n:
            break

    if num < n:
        print(ans + sum(a[num:]))
    else:
        print(ans)
if __name__ == '__main__':
  main()


Fixed submission:
import sys

def main():
    input = sys.stdin.readline

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

    a.sort()
    a = tuple(a)

    bc.sort(key=lambda x: x[1], reverse=True)

    num = 0
    ans = 0

    for i in range(m):
        b = bc[i][0]
        c = bc[i][1]
        for j in range(num,min(num+b,n)):
            if c > a[j]:
                ans += c
            else:
                ans += a[j]

        num += b
        if num >= n:
            break

    if num < n:
        print(ans + sum(a[num:]))
    else:
        print(ans)
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import bisect

def main():
    input = sys.stdin.readline

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

    a.sort()
    a = tuple(a)

    bc.sort(key=lambda x: x[1], reverse=True)

    num = 0
    ans = 0
    for i in range(m):
        b = bc[i][0]
        c = bc[i][1]
        index = bisect.bisect_left(a[num:],c)
        if index == 0:
            break
        elif index > b:
            ans += c * b
        else:
            ans += c * index
            ans += sum(a[num+index:num+b])

        num += b
        if num >= n:
            break
    if num < n:
        print(ans + sum(a[num:]))
    else:
        print(ans)

if __name__ == '__main__':
  main()


Fixed submission:
import sys

def main():
    input = sys.stdin.readline

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

    a.sort()
    a = tuple(a)

    bc.sort(key=lambda x: x[1], reverse=True)

    num = 0
    ans = 0

    for i in range(m):
        b = bc[i][0]
        c = bc[i][1]
        for j in range(num,min(num+b,n)):
            if c > a[j]:
                ans += c
            else:
                ans += a[j]

        num += b
        if num >= n:
            break

    if num < n:
        print(ans + sum(a[num:]))
    else:
        print(ans)
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import bisect

def main():
    input = sys.stdin.readline

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

    a.sort()
    a = tuple(a)

    bc.sort(key=lambda x: x[1], reverse=True)

    num = 0
    ans = 0
    for i in range(m):
        b = bc[i][0]
        c = bc[i][1]
        index = bisect.bisect_left(a[num:],c)
        if index == 0:
            break
        elif index > b:
            ans += c * b
        else:
            ans += c * index
            ans += sum(a[num+index:num+b])

        num += b
        if num >= n:
            break
    if num < n:
        print(ans + sum(a[num:]))
    else:
        print(ans)

if __name__ == '__main__':
  main()


Fixed submission:
import sys

def main():
    input = sys.stdin.readline

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

    a.sort()
    a = tuple(a)

    bc.sort(key=lambda x: x[1], reverse=True)

    num = 0
    ans = 0

    for i in range(m):
        b = bc[i][0]
        c = bc[i][1]
        for j in range(num,min(num+b,n)):
            if c > a[j]:
                ans += c
            else:
                ans += a[j]

        num += b
        if num >= n:
            break

    if num < n:
        print(ans + sum(a[num:]))
    else:
        print(ans)
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import bisect

def main():
    input = sys.stdin.readline

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

    a.sort()
    a = tuple(a)

    bc.sort(key=lambda x: x[1], reverse=True)
    bc = tuple(bc)

    num = 0
    ans = 0
    for i in range(m):
        index = bisect.bisect_left(a[num:],bc[i][1])
        if index == 0:
            break
        elif index > bc[i][0]:
            ans += bc[i][1] * bc[i][0]
        else:
            ans += bc[i][1] * index
            ans += sum(a[num+index:num+bc[i][0]])

        num += bc[i][0]
        if num >= n:
            break
    if num < n:
        print(ans + sum(a[num:]))
    else:
        print(ans)

if __name__ == '__main__':
  main()


Fixed submission:
import sys

def main():
    input = sys.stdin.readline

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

    a.sort()
    a = tuple(a)

    bc.sort(key=lambda x: x[1], reverse=True)

    num = 0
    ans = 0

    for i in range(m):
        b = bc[i][0]
        c = bc[i][1]
        for j in range(num,min(num+b,n)):
            if c > a[j]:
                ans += c
            else:
                ans += a[j]

        num += b
        if num >= n:
            break

    if num < n:
        print(ans + sum(a[num:]))
    else:
        print(ans)
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import bisect

def main():
    input = sys.stdin.readline

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

    a.sort()

    bc.sort(key=lambda x: x[1], reverse=True)
    num = 0
    ans = 0
    for i in range(m):
        index = bisect.bisect_left(a[num:],bc[i][1])
        if index == 0:
            break
        elif index > bc[i][0]:
            ans += bc[i][1] * bc[i][0]
        else:
            ans += bc[i][1] * index
            ans += sum(a[num+index:num+bc[i][0]])

        num += bc[i][0]
        if num >= n:
            break
    if num < n:
        print(ans + sum(a[num:]))
    else:
        print(ans)

if __name__ == '__main__':
  main()

Fixed submission:
import sys

def main():
    input = sys.stdin.readline

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

    a.sort()
    a = tuple(a)

    bc.sort(key=lambda x: x[1], reverse=True)

    num = 0
    ans = 0

    for i in range(m):
        b = bc[i][0]
        c = bc[i][1]
        for j in range(num,min(num+b,n)):
            if c > a[j]:
                ans += c
            else:
                ans += a[j]

        num += b
        if num >= n:
            break

    if num < n:
        print(ans + sum(a[num:]))
    else:
        print(ans)
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import bisect
input = sys.stdin.readline

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

a.sort()

bc.sort(key=lambda x: x[1], reverse=True)
num = 0
ans = 0
for i in range(m):
    index = bisect.bisect_left(a[num:],bc[i][1])
    if index == 0:
        break
    elif index > bc[i][0]:
        ans += bc[i][1] * bc[i][0]
    else:
        ans += bc[i][1] * index
        ans += sum(a[num+index:num+bc[i][0]])

    num += bc[i][0]
    if num >= n:
        break
if num < n:
    print(ans + sum(a[num:]))
else:
    print(ans)


Fixed submission:
import sys

def main():
    input = sys.stdin.readline

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

    a.sort()
    a = tuple(a)

    bc.sort(key=lambda x: x[1], reverse=True)

    num = 0
    ans = 0

    for i in range(m):
        b = bc[i][0]
        c = bc[i][1]
        for j in range(num,min(num+b,n)):
            if c > a[j]:
                ans += c
            else:
                ans += a[j]

        num += b
        if num >= n:
            break

    if num < n:
        print(ans + sum(a[num:]))
    else:
        print(ans)
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import bisect
input = sys.stdin.readline

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

a.sort()

bc.sort(key=lambda x: x[1], reverse=True)
num = 0
for i in range(m):
    index = bisect.bisect_left(a[num:],bc[i][1])
    if index == 0:
        break
    elif index > bc[i][0]:
        for j in range(bc[i][0]):
            a[num+j] = bc[i][1]
    else:
        for j in range(index):
            a[num+j] = bc[i][1]

    num += bc[i][0]
    if num >= n:
        break

print(sum(a))


Fixed submission:
import sys

def main():
    input = sys.stdin.readline

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

    a.sort()
    a = tuple(a)

    bc.sort(key=lambda x: x[1], reverse=True)

    num = 0
    ans = 0

    for i in range(m):
        b = bc[i][0]
        c = bc[i][1]
        for j in range(num,min(num+b,n)):
            if c > a[j]:
                ans += c
            else:
                ans += a[j]

        num += b
        if num >= n:
            break

    if num < n:
        print(ans + sum(a[num:]))
    else:
        print(ans)
if __name__ == '__main__':
    main()