Buggy python submission (verdict: Runtime Error):
n,m = map(int,input().split())
a_array = [int(x) for x in input().split()]
d = dict()
for a in a_array:
    d.setdefault(a, 0)
    d[a] += 1
for _ in range(m):
    b,c = map(int, input().split())
    d.setdefault(c, 0)
    d[c] += b
key = sort(d.keys(),reverse=True)
ans = 0
index = 0
while n > 0:
    num = key[index]
    if d[num] >= n:
        ans += num * n
        break
    ans += num * d[num]
    n -= d[num]
    index += 1

print(ans)

Fixed submission:
import bisect

n, m = map(int, input().split())
a_array = sorted([int(x) for x in input().split()])
bc_array = sorted([[int(x) for x in input().split()] for x in range(m)], key=lambda x: x[1], reverse=True)

# 何枚入れ替えられるか
candidate_replace_cnt = n
for b, c in bc_array:
    if a_array[0] >= c or candidate_replace_cnt < 0:
        break
    for i in range(b):
        a_array.append(c)
    candidate_replace_cnt -= b
a_array.sort(reverse=True)
print(sum(a_array[:n]))

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

n, m = map(int, input().split())
a_array = sorted([int(x) for x in input().split()])
bc_array = sorted([[int(x) for x in input().split()] for x in range(m)], key=lambda x: x[1], reverse=True)

candidate_replace_cnt = n
replace_sum = 0
replace_cnt = 0
for b, c in bc_array:
    if a_array[0] > c and candidate_replace_cnt <= 0:
        break
    # 何枚入れられそうか
    num = bisect.bisect_left(a_array, c)
    # 入れ替えられる最大枚数が決まる
    candidate_replace_cnt = min(candidate_replace_cnt, num)
    # 全部入れ替えられる
    if candidate_replace_cnt <= b:
        replace_sum += c*candidate_replace_cnt
        a_array = a_array[candidate_replace_cnt:]
        break
    candidate_replace_cnt -= b
    replace_sum += c*b
    a_array = a_array[b:]
print(replace_sum + sum(a_array))


Fixed submission:
import bisect

n, m = map(int, input().split())
a_array = sorted([int(x) for x in input().split()])
bc_array = sorted([[int(x) for x in input().split()] for x in range(m)], key=lambda x: x[1], reverse=True)

# 何枚入れ替えられるか
candidate_replace_cnt = n
for b, c in bc_array:
    if a_array[0] >= c or candidate_replace_cnt < 0:
        break
    for i in range(b):
        a_array.append(c)
    candidate_replace_cnt -= b
a_array.sort(reverse=True)
print(sum(a_array[:n]))

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

n, m = map(int, input().split())
a_array = sorted([int(x) for x in input().split()])
bc_array = sorted([[int(x) for x in input().split()] for x in range(m)], key=lambda x: x[1], reverse=True)

candidate_replace_cnt = n
replace_sum = 0
replace_cnt = 0
for b, c in bc_array:
    if a_array[0] > c:
        break
    # 何枚入れられそうか
    num = bisect.bisect_left(a_array, c)
    # 入れ替えられる最大枚数が決まる
    candidate_replace_cnt = min(candidate_replace_cnt, num)
    # 全部入れ替えられる
    if candidate_replace_cnt <= b:
        replace_sum += c*candidate_replace_cnt
        a_array = a_array[candidate_replace_cnt:]
        break
    candidate_replace_cnt -= b
    replace_sum += c*b
    a_array = a_array[b:]
print(replace_sum + sum(a_array))


Fixed submission:
import bisect

n, m = map(int, input().split())
a_array = sorted([int(x) for x in input().split()])
bc_array = sorted([[int(x) for x in input().split()] for x in range(m)], key=lambda x: x[1], reverse=True)

# 何枚入れ替えられるか
candidate_replace_cnt = n
for b, c in bc_array:
    if a_array[0] >= c or candidate_replace_cnt < 0:
        break
    for i in range(b):
        a_array.append(c)
    candidate_replace_cnt -= b
a_array.sort(reverse=True)
print(sum(a_array[:n]))

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

n, m = map(int, input().split())
a_array = sorted([int(x) for x in input().split()])
bc_array = sorted([[int(x) for x in input().split()] for x in range(m)], key=lambda x: x[1], reverse=True)

candidate_replace_cnt = n
replace_sum = 0
replace_cnt = 0
for b, c in bc_array:
    if a_array[0] > c:
        break
    # 何枚入れられそうか
    num = bisect.bisect_left(a_array, c)
    # 入れ替えられる最大枚数が決まる
    candidate_replace_cnt = min(candidate_replace_cnt, num)
    # 全部入れ替えられる
    if candidate_replace_cnt <= b:
        replace_sum += c*candidate_replace_cnt
        a_array = a_array[candidate_replace_cnt:]
        break
    candidate_replace_cnt -= b
    replace_sum += c*b
    a_array = a_array[b:]
print(replace_sum + sum(a_array))


Fixed submission:
import bisect

n, m = map(int, input().split())
a_array = sorted([int(x) for x in input().split()])
bc_array = sorted([[int(x) for x in input().split()] for x in range(m)], key=lambda x: x[1], reverse=True)

# 何枚入れ替えられるか
candidate_replace_cnt = n
for b, c in bc_array:
    if a_array[0] >= c or candidate_replace_cnt < 0:
        break
    for i in range(b):
        a_array.append(c)
    candidate_replace_cnt -= b
a_array.sort(reverse=True)
print(sum(a_array[:n]))

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

n, m = map(int, input().split())
a_array = sorted([int(x) for x in input().split()])
bc_array = sorted([[int(x) for x in input().split()] for x in range(m)], key=lambda x: x[1], reverse=True)

candidate_replace_cnt = n
replace_array = []
replace_cnt = 0
for b, c in bc_array:
    if a_array[0] > c:
        break
    # 何枚入れられそうか
    num = bisect.bisect_left(a_array, c)
    # 入れ替えられる最大枚数が決まる
    candidate_replace_cnt = min(candidate_replace_cnt, num)
    # 全部入れ替えられる
    if candidate_replace_cnt <= b:
        replace_array += [c for x in range(candidate_replace_cnt)]
        a_array = a_array[candidate_replace_cnt:]
        break
    candidate_replace_cnt -= b
    replace_array += [c for x in range(b)]
    a_array = a_array[b:]
print(sum(replace_array + a_array))


Fixed submission:
import bisect

n, m = map(int, input().split())
a_array = sorted([int(x) for x in input().split()])
bc_array = sorted([[int(x) for x in input().split()] for x in range(m)], key=lambda x: x[1], reverse=True)

# 何枚入れ替えられるか
candidate_replace_cnt = n
for b, c in bc_array:
    if a_array[0] >= c or candidate_replace_cnt < 0:
        break
    for i in range(b):
        a_array.append(c)
    candidate_replace_cnt -= b
a_array.sort(reverse=True)
print(sum(a_array[:n]))