Buggy python submission (verdict: Time Limit Exceeded):
import bisect
n, m = map(int, input().split())
arr = sorted(list(map(int, input().split())))
ope = []
d = []
for i in range(m):
    ope.append(list(map(int,input().split())))
ope = sorted(ope,key = lambda x:-x[1])
for b,c in ope:
    if len(d) == n:
        break
    elif len(d) + b > n:
        for i in range(n-b):
            d.append(c)
    else:
        for i in range(b):
            d.append(c)
d.reverse()
arr = sorted(arr,reverse = True)
x = [0]*(n+1)
x[0] = sum(arr)
for i in range(1,n+1):
    t_arr = arr.pop()
    if d:
        t_d = d.pop()
    else:
        t_d = t_arr
    x[i] = x[i-1] + t_d - t_arr
    if x[i] < x[i-1]:
        break
print(max(x))


Fixed submission:
import bisect
n, m = map(int, input().split())
arr = sorted(list(map(int, input().split())))
ope = []
d = []
for i in range(m):
    ope.append(list(map(int,input().split())))
ope = sorted(ope,key = lambda x:-x[1])
i = 0
ok = True
for b,c in ope:
    for rep in range(b):
        if i >= n or arr[i] > c:
            ok = False
            break
        arr[i] = c
        i += 1
    if ok:
        continue
    else:
        break
print(sum(arr))

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
n, m = map(int, input().split())
arr = sorted(list(map(int, input().split())))
ope = []
d = []
for i in range(m):
    ope.append(list(map(int,input().split())))
ope = sorted(ope,key = lambda x:-x[1])
for b,c in ope:
    if len(d) == n:
        break
    elif len(d) + b > n:
        for i in range(n-b):
            d.append(c)
    else:
        for i in range(b):
            d.append(c)
d.reverse()
arr = sorted(arr,reverse = True)
x = [0]*(n+1)
x[0] = sum(arr)
for i in range(1,n+1):
    t_arr = arr.pop()
    if d:
        t_d = d.pop()
    else:
        t_d = t_arr
    x[i] = x[i-1] + t_d - t_arr
    if x[i] < x[i-1]:
        break
print(max(x))


Fixed submission:
import bisect
n, m = map(int, input().split())
arr = sorted(list(map(int, input().split())))
ope = []
d = []
for i in range(m):
    ope.append(list(map(int,input().split())))
ope = sorted(ope,key = lambda x:-x[1])
i = 0
ok = True
for b,c in ope:
    for rep in range(b):
        if i >= n or arr[i] > c:
            ok = False
            break
        arr[i] = c
        i += 1
    if ok:
        continue
    else:
        break
print(sum(arr))

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
n, m = map(int, input().split())
arr = sorted(list(map(int, input().split())))
ope = []
d = []
for i in range(m):
    ope.append(list(map(int,input().split())))
ope = sorted(ope,key = lambda x:-x[1])
for b,c in ope:
    if len(d) == n:
        break
    elif len(d) + b > n:
        d  = d + [c]*(n-len(d))
    else:
        d = d + [c]*b
d.reverse()
arr = sorted(arr,reverse = True)
x = [0]*(n+1)
x[0] = sum(arr)
for i in range(1,n+1):
    t_arr = arr.pop()
    if d:
        t_d = d.pop()
    else:
        t_d = t_arr
    x[i] = x[i-1] + t_d - t_arr
    if x[i] < x[i-1]:
        break
print(max(x))

Fixed submission:
import bisect
n, m = map(int, input().split())
arr = sorted(list(map(int, input().split())))
ope = []
d = []
for i in range(m):
    ope.append(list(map(int,input().split())))
ope = sorted(ope,key = lambda x:-x[1])
i = 0
ok = True
for b,c in ope:
    for rep in range(b):
        if i >= n or arr[i] > c:
            ok = False
            break
        arr[i] = c
        i += 1
    if ok:
        continue
    else:
        break
print(sum(arr))

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
n, m = map(int, input().split())
arr = sorted(list(map(int, input().split())))
ope = []
d = []
for i in range(m):
    ope.append(list(map(int,input().split())))
ope = sorted(ope,key = lambda x:-x[1])
for b,c in ope:
    if len(d) == n:
        break
    elif len(d) + b > n:
        d  = d + [c]*(n-len(d))
    else:
        d = d + [c]*b
d.reverse()
arr = sorted(arr,reverse = True)
x = [0]*(n+1)
x[0] = sum(arr)
for i in range(1,n+1):
    t_arr = arr.pop()
    if d:
        t_d = d.pop()
    else:
        t_d = t_arr
    x[i] = x[i-1] + t_d - t_arr
print(max(x))

Fixed submission:
import bisect
n, m = map(int, input().split())
arr = sorted(list(map(int, input().split())))
ope = []
d = []
for i in range(m):
    ope.append(list(map(int,input().split())))
ope = sorted(ope,key = lambda x:-x[1])
i = 0
ok = True
for b,c in ope:
    for rep in range(b):
        if i >= n or arr[i] > c:
            ok = False
            break
        arr[i] = c
        i += 1
    if ok:
        continue
    else:
        break
print(sum(arr))

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
n, m = map(int, input().split())
arr = sorted(list(map(int, input().split())))
ope = []
d = []
for i in range(m):
    ope.append(list(map(int,input().split())))
ope = sorted(ope,key = lambda x:-x[1])
for b,c in ope:
    if len(d) == n:
        break
    elif len(d) + b > n:
        d  = d + [c]*(n-len(d))
    else:
        d = d + [c]*b
d.reverse()
arr = sorted(arr,reverse = True)
x = [0]*(n+1)
x[0] = sum(arr)
for i in range(1,n+1):
    t_arr = arr.pop()
    if d:
        t_d = d.pop()
    else:
        t_d = t_arr
    x[i] = x[i-1] + t_d - t_arr
print(max(x))

Fixed submission:
import bisect
n, m = map(int, input().split())
arr = sorted(list(map(int, input().split())))
ope = []
d = []
for i in range(m):
    ope.append(list(map(int,input().split())))
ope = sorted(ope,key = lambda x:-x[1])
i = 0
ok = True
for b,c in ope:
    for rep in range(b):
        if i >= n or arr[i] > c:
            ok = False
            break
        arr[i] = c
        i += 1
    if ok:
        continue
    else:
        break
print(sum(arr))

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
n, m = map(int, input().split())
arr = sorted(list(map(int, input().split())))
ope = []
for i in range(m):
    ope.append(list(map(int,input().split())))
for b,c in ope:
    if arr[0] > c:
        continue
    i = min(bisect.bisect_right(arr,c),b)
    j = bisect.bisect_right(arr[i:],c) + i
    arr = arr[i:j] + [c]*i + arr[j:]
print(sum(arr))

Fixed submission:
import bisect
n, m = map(int, input().split())
arr = sorted(list(map(int, input().split())))
ope = []
d = []
for i in range(m):
    ope.append(list(map(int,input().split())))
ope = sorted(ope,key = lambda x:-x[1])
i = 0
ok = True
for b,c in ope:
    for rep in range(b):
        if i >= n or arr[i] > c:
            ok = False
            break
        arr[i] = c
        i += 1
    if ok:
        continue
    else:
        break
print(sum(arr))

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
n, m = map(int, input().split())
arr = sorted(list(map(int, input().split())))
ope = []
for i in range(m):
    ope.append(list(map(int,input().split())))
for b,c in ope:
    if arr[0] > c:
        continue
    i = min(bisect.bisect_right(arr,c),b)
    j = bisect.bisect_right(arr[i:],c) + i
    arr = arr[i:j] + [c]*i + arr[j:]
print(sum(arr))

Fixed submission:
import bisect
n, m = map(int, input().split())
arr = sorted(list(map(int, input().split())))
ope = []
d = []
for i in range(m):
    ope.append(list(map(int,input().split())))
ope = sorted(ope,key = lambda x:-x[1])
i = 0
ok = True
for b,c in ope:
    for rep in range(b):
        if i >= n or arr[i] > c:
            ok = False
            break
        arr[i] = c
        i += 1
    if ok:
        continue
    else:
        break
print(sum(arr))

Buggy python submission (verdict: Time Limit Exceeded):
import heapq as hq
n, m = map(int, input().split())
q = list(map(int,input().split()))
hq.heapify(q)
for i in range(m):
    b, c = map(int,input().split())
    for j in range(b):
        if q[0] > c:
            continue
        else:
            hq.heappop(q)
            hq.heappush(q,c)
print(sum(q))

Fixed submission:
import bisect
n, m = map(int, input().split())
arr = sorted(list(map(int, input().split())))
ope = []
d = []
for i in range(m):
    ope.append(list(map(int,input().split())))
ope = sorted(ope,key = lambda x:-x[1])
i = 0
ok = True
for b,c in ope:
    for rep in range(b):
        if i >= n or arr[i] > c:
            ok = False
            break
        arr[i] = c
        i += 1
    if ok:
        continue
    else:
        break
print(sum(arr))