Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A_list = sorted(list(map(int, input().split())))

BC_list = [tuple(map(int, input().split())) for _ in range(M)]
BC_list.sort(key=lambda x: x[1], reverse=True)

n_replace = 0
sum_target = 0
for B, C in BC_list:
    if n_replace + B <= N:
        for A in A_list[n_replace:n_replace+B]:
            if A < C:
                sum_target += C
                n_replace += 1
            else:
                break
    else:
        remaining = N - n_replace
        for A in A_list[n_replace:n_replace+remaining]:
            if A < C:
                sum_target += C
                n_replace += 1
            else:
                break
if n_replace < N:
    sum_target += sum(A_list[n_replace:])

# Output sumed A_list
print(sum_target)


Fixed submission:
N, M = map(int, input().split())
A_list = sorted(list(map(int, input().split())))

BC_list = [tuple(map(int, input().split())) for _ in range(M)]
BC_list.sort(key=lambda x: x[1], reverse=True)

n_replace = 0
sum_target = 0
is_broken = False
for B, C in BC_list:
    if n_replace + B <= N:
        for A in A_list[n_replace:n_replace+B]:
            if A < C:
                sum_target += C
                n_replace += 1
            else:
                is_broken = True
                break
    else:
        remaining = N - n_replace
        for A in A_list[n_replace:n_replace+remaining]:
            if A < C:
                sum_target += C
                n_replace += 1
            else:
                is_broken = True
                break
    if is_broken is True:
        break
if n_replace < N:
    sum_target += sum(A_list[n_replace:])

# Output sumed A_list
print(sum_target)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A_list = sorted(list(map(int, input().split())))

D_list = []
for i in range(M):
    B_i, C_i = map(int, input().split())
    D_list += [C_i] * B_i

D_list = sorted(D_list, reverse=True)

# Operate Top N of BC_sorted
len_target = min(len(D_list), N)
for i in range(len_target):
    if A_list[i] < D_list[i]:
        A_list[i] = D_list[i]
    else:
        break

# Output sumed A_list
print(sum(A_list))


Fixed submission:
N, M = map(int, input().split())
A_list = sorted(list(map(int, input().split())))

BC_list = [tuple(map(int, input().split())) for _ in range(M)]
BC_list.sort(key=lambda x: x[1], reverse=True)

n_replace = 0
sum_target = 0
is_broken = False
for B, C in BC_list:
    if n_replace + B <= N:
        for A in A_list[n_replace:n_replace+B]:
            if A < C:
                sum_target += C
                n_replace += 1
            else:
                is_broken = True
                break
    else:
        remaining = N - n_replace
        for A in A_list[n_replace:n_replace+remaining]:
            if A < C:
                sum_target += C
                n_replace += 1
            else:
                is_broken = True
                break
    if is_broken is True:
        break
if n_replace < N:
    sum_target += sum(A_list[n_replace:])

# Output sumed A_list
print(sum_target)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A_list = sorted(list(map(int, input().split())))

D_list = []
len_D_list = 0
for i in range(M):
    B_i, C_i = map(int, input().split())
    D_list += [C_i] * B_i
    len_D_list += B_i

D_list = sorted(D_list, reverse=True)

# Operate Top N of BC_sorted
len_target = min(len_D_list, N)
for i in range(len_target):
    if A_list[i] < D_list[i]:
        A_list[i] = D_list[i]
    else:
        break

# Output sumed A_list
print(sum(A_list))


Fixed submission:
N, M = map(int, input().split())
A_list = sorted(list(map(int, input().split())))

BC_list = [tuple(map(int, input().split())) for _ in range(M)]
BC_list.sort(key=lambda x: x[1], reverse=True)

n_replace = 0
sum_target = 0
is_broken = False
for B, C in BC_list:
    if n_replace + B <= N:
        for A in A_list[n_replace:n_replace+B]:
            if A < C:
                sum_target += C
                n_replace += 1
            else:
                is_broken = True
                break
    else:
        remaining = N - n_replace
        for A in A_list[n_replace:n_replace+remaining]:
            if A < C:
                sum_target += C
                n_replace += 1
            else:
                is_broken = True
                break
    if is_broken is True:
        break
if n_replace < N:
    sum_target += sum(A_list[n_replace:])

# Output sumed A_list
print(sum_target)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A_list = sorted(list(map(int, input().split())))

D_list = []
for _ in range(M):
    B_i, C_i = map(int, input().split())
    D_list += [C_i] * B_i

D_list = sorted(D_list, reverse=True)

# Operate Top min(len(D_list), N) of BC_sorted
len_target = min(len(D_list), N)
for i in range(len_target):
    if A_list[i] < D_list[i]:
        A_list[i] = D_list[i]
    else:
        break

# Output sumed A_list
print(sum(A_list))

Fixed submission:
N, M = map(int, input().split())
A_list = sorted(list(map(int, input().split())))

BC_list = [tuple(map(int, input().split())) for _ in range(M)]
BC_list.sort(key=lambda x: x[1], reverse=True)

n_replace = 0
sum_target = 0
is_broken = False
for B, C in BC_list:
    if n_replace + B <= N:
        for A in A_list[n_replace:n_replace+B]:
            if A < C:
                sum_target += C
                n_replace += 1
            else:
                is_broken = True
                break
    else:
        remaining = N - n_replace
        for A in A_list[n_replace:n_replace+remaining]:
            if A < C:
                sum_target += C
                n_replace += 1
            else:
                is_broken = True
                break
    if is_broken is True:
        break
if n_replace < N:
    sum_target += sum(A_list[n_replace:])

# Output sumed A_list
print(sum_target)