Buggy python submission (verdict: Time Limit Exceeded):
import bisect
n, m = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
a.sort()
for i in range(m):
    b, c = [int(i) for i in input().split()]
    idx = bisect.bisect_left(a, c)
    a[idx:idx]=[c]*b
a = a[::-1]
print(sum(a[:n]))

Fixed submission:
import bisect
n, m = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
a.sort()
d = []
cnt = 1
for i in range(n-1):
    if a[i+1] == a[i]:
        cnt += 1
    else:
        d.append([a[i], cnt])
        cnt = 1
d.append([a[n-1], cnt])


for i in range(m):
    b, c = [int(i) for i in input().split()]
    d.append([c, b])
d.sort(reverse=True)
ans = 0
j = 0
for i in range(n):
    if d[j][1] > 0:
        ans += d[j][0]
        d[j][1] -= 1
    if d[j][1] == 0:
        j += 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, m = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]

for i in range(m):
    b, c = [int(i) for i in input().split()]
    a.extend([c]*b)

a.sort(reverse=True)

print(sum(a[:n]))



Fixed submission:
import bisect
n, m = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
a.sort()
d = []
cnt = 1
for i in range(n-1):
    if a[i+1] == a[i]:
        cnt += 1
    else:
        d.append([a[i], cnt])
        cnt = 1
d.append([a[n-1], cnt])


for i in range(m):
    b, c = [int(i) for i in input().split()]
    d.append([c, b])
d.sort(reverse=True)
ans = 0
j = 0
for i in range(n):
    if d[j][1] > 0:
        ans += d[j][0]
        d[j][1] -= 1
    if d[j][1] == 0:
        j += 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
n, m = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
a.sort()
for i in range(m):
    b, c = [int(i) for i in input().split()]
    idx = bisect.bisect_left(a, c)
    
    if idx <= b:
        a[:idx] = [c]*idx
    else:
        a[:b] = [c]*b
        a[:idx] = a[b:idx] + a[:b]

print(sum(a))


Fixed submission:
import bisect
n, m = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
a.sort()
d = []
cnt = 1
for i in range(n-1):
    if a[i+1] == a[i]:
        cnt += 1
    else:
        d.append([a[i], cnt])
        cnt = 1
d.append([a[n-1], cnt])


for i in range(m):
    b, c = [int(i) for i in input().split()]
    d.append([c, b])
d.sort(reverse=True)
ans = 0
j = 0
for i in range(n):
    if d[j][1] > 0:
        ans += d[j][0]
        d[j][1] -= 1
    if d[j][1] == 0:
        j += 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
n, m = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
a.sort()
for i in range(m):
    b, c = [int(i) for i in input().split()]
    idx = bisect.bisect_left(a, c)
    if idx <= b:
        a[:idx] = [c for _ in range(idx)]
    else:
        a[:b] = [c for _ in range(b)]
        a[:idx] = a[b:idx] + a[:b]
print(sum(a))


Fixed submission:
import bisect
n, m = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
a.sort()
d = []
cnt = 1
for i in range(n-1):
    if a[i+1] == a[i]:
        cnt += 1
    else:
        d.append([a[i], cnt])
        cnt = 1
d.append([a[n-1], cnt])


for i in range(m):
    b, c = [int(i) for i in input().split()]
    d.append([c, b])
d.sort(reverse=True)
ans = 0
j = 0
for i in range(n):
    if d[j][1] > 0:
        ans += d[j][0]
        d[j][1] -= 1
    if d[j][1] == 0:
        j += 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
n, m = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
a.sort()
for i in range(m):
    b, c = [int(i) for i in input().split()]
    idx = bisect.bisect_left(a, c)
    if idx <= b:
        a[:idx] = [c for _ in range(idx)]
    else:
        a[:b] = [c for _ in range(b)]
    a.sort()
print(sum(a))


Fixed submission:
import bisect
n, m = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
a.sort()
d = []
cnt = 1
for i in range(n-1):
    if a[i+1] == a[i]:
        cnt += 1
    else:
        d.append([a[i], cnt])
        cnt = 1
d.append([a[n-1], cnt])


for i in range(m):
    b, c = [int(i) for i in input().split()]
    d.append([c, b])
d.sort(reverse=True)
ans = 0
j = 0
for i in range(n):
    if d[j][1] > 0:
        ans += d[j][0]
        d[j][1] -= 1
    if d[j][1] == 0:
        j += 1
print(ans)