alexjercan/bugnet huggingface dataset:
Problem p03038 (Python) — buggy submission, verdict: Runtime Error

#!/usr/bin/env python3
import bisect
import numpy as np

N, M = list(map(int, input().split()))
a_list = list(map(int, input().split()))
bc_list = []

for i in range(M):
    try:
        b, c = list(map(int, input().split()))
        bc_list.append((b, c))
    except:
        M = i

bc_list = sorted(bc_list, key=lambda x: x[1], reverse=True)
a_list = sorted(a_list)

fix_list = []
s = 0
b, c = bc_list[0]
bc_index = 0
now_b, now_c = 0, c

for i, a in enumerate(a_list):
    # a = a_list[i]
    if now_b >= b:
        bc_index += 1
        if bc_index >= len(bc_list):
            # TODO: 残りを足す
            fix_list.append(a)
            s += a
            continue
        now_b = 0
        b, c = bc_list[bc_index]

    if a < c:
        fix_list.append(c)
        s += c
    else:
        fix_list.append(a)
        s += a
    now_b += 1

ans = s
print(ans)


Error: TLE

Fixed submission:

#!/usr/bin/env python3
import bisect
import numpy as np

N, M = list(map(int, input().split()))
a_list = list(map(int, input().split()))
bc_list = []

for _ in range(M):
    b, c = list(map(int, input().split()))
    bc_list.append((b, c))

bc_list = sorted(bc_list, key=lambda x: x[1], reverse=True)
now_list = np.array(sorted(a_list))
fix_list = np.array([])
min_a = min(a_list)

for bc in bc_list:
    b, c = bc
    if min_a >= c:
        # もう置き換えても合計値は増えない
        break
    # cより小さい数は何個あるか？
    # 線形探索だとTLEになるので、2分探索で考える
    c_index = bisect.bisect_right(now_list, c)
    target_index = c_index if c_index < b else b

    # c_indexまでをcで置き換えても、順序の入れ替えはない
    now_list[:target_index] = c
    # c_index以前の整数は使用確定なので掃き出す
    fix_list = np.hstack([fix_list, now_list[:target_index]])
    now_list = now_list[target_index:]
    min_a = now_list[0]

fix_list = np.hstack([fix_list, now_list])
ans = fix_list.sum()
ans = int(ans)
print(ans)
Problem p03038 (Python) — buggy submission, verdict: Runtime Error

n, m = map(int, input().split())
a = list(map(int, input().split()))
bc = [0] * m
ans = []
a.sort()
for i in range(m):
    bc[i] = list(map(int, input().split()))
bc.sort(key=lambda x: x[1], reverse=True)


j = 0
for i in range(n):
    if j < m:
        if bc[j][0] == 0:
            j += 1
    if j >= m:
        ans.append(a[i])
    elif a[i] >= bc[j][1]:
        ans.append(a[i])
    else:
        ans.append(bc[j][1])
        bc[j][0] -= 1

print(sum(ans))


Error: 0

Fixed submission:

n, m = map(int, input().split())
a = list(map(int, input().split()))
bc = [0] * m
ans = []
a.sort()
for i in range(m):
    bc[i] = list(map(int, input().split()))
bc.sort(key=lambda x: x[1], reverse=True)


j = 0
for i in range(n):
    if bc[j][0] == 0:
        j += 1
    if j >= m:
        ans.append(a[i])
    elif a[i] >= bc[j][1]:
        ans.append(a[i])
    else:
        ans.append(bc[j][1])
        bc[j][0] -= 1

print(sum(ans))
Problem p03038 (Python) — buggy submission, verdict: Time Limit Exceeded

from collections import deque

N, M = map(int, input().split())
A = list(map(int, input().split()))
operation = []
ans = 0

for _ in range(M):
    t = tuple(map(int, input().split()))
    operation.append(t)

operation.sort(key=lambda x: x[1], reverse=True)
A.sort()
A = deque(A)
operation = deque(operation)

while A and operation:
    if A[-1] >= operation[0][1]:
        ans += A[-1]
        A.pop()
    else:
        cnt = 0
        for i in range(operation[0][0]):
            if len(A) == i or A[i] > operation[0][1]:
                cnt = i
                break
        else:
            cnt = operation[0][0]
        for _ in range(cnt):
            A.popleft()
        ans += cnt * operation[0][1]
        operation.popleft()

ans += sum(A)

print(ans)


Error: TLE

Fixed submission:

N, M = map(int, input().split())
A = list(map(int, input().split()))
operation = []
ans = 0

for _ in range(M):
    t = tuple(map(int, input().split()))
    operation.append(t)

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

while A and operation:
    if A[-1] >= operation[0][1]:
        ans += A[-1]
        A.pop(-1)
    else:
        cnt = 0
        for i in range(operation[0][0]):
            if len(A) == i or A[i] > operation[0][1]:
                cnt = i
                break
        else:
            cnt = operation[0][0]
        for _ in range(cnt):
            A.pop(0)
        ans += cnt * operation[0][1]
        operation.pop(0)

ans += sum(A)

print(ans)
Problem p03038 (Python) — buggy submission, verdict: Runtime Error

#!/usr/bin/env python3
N, M = map(int, input().rstrip().split())
A = list(map(int, input().rstrip().split()))

B = []
C = []
for _ in range(M):
    b, c = map(int, input().rstrip().split())
    B.append(b)
    C.append(c)

Index = sorted(range(M), key=lambda i: C[i], reverse=True)

sum = 0
A.sort()
for index in Index:
    if A and A[0] >= C[index]:
        break
    count = 0
    i = 0
    while True:
        if i >= len(A):
            break
        if A[i] < C[index] and count < B[index]:
            sum += C[index]
            A.pop(i)
            # print(A)
            count += 1
            continue
        if A[i] >= C[index] or count >= B[index]:
            break
        i += 1

if A:
    for a in A:
        sum += a

print(sum)


Error: 0

Fixed submission:

#!/usr/bin/env python3
N, M = map(int, input().rstrip().split())
A = list(map(int, input().rstrip().split()))

B = []
C = []
for _ in range(M):
    b, c = map(int, input().rstrip().split())
    B.append(b)
    C.append(c)

Index = sorted(range(M), key=lambda i: C[i], reverse=True)

sum = 0
A.sort()
for index in Index:
    if A[0] >= C[index]:
        break
    count = 0
    i = 0
    while True:
        if i >= len(A):
            break
        if A[i] < C[index] and count < B[index]:
            sum += C[index]
            A.pop(i)
            # print(A)
            count += 1
            continue
        if A[i] >= C[index] or count >= B[index]:
            break
        i += 1

if A:
    for a in A:
        sum += a

print(sum)
Problem p03038 (Python) — buggy submission, verdict: Time Limit Exceeded

N, M = map(int, input().split())
A = list(map(int, input().split()))
CB = []
for _ in range(M):
    b, c = map(int, input().split())
    CB.append((c, b))

# ascend
A.sort()

# descend
CB.sort(reverse=True)

# fill convert ary
conv = []
for cb in CB:
    c, b = cb
    conv.extend([c] * b)
    if len(conv) >= N:
        break

cl = len(conv)

sum = 0
for i in range(N):
    if i >= cl:
        sum += A[i]
    else:
        sum += max(A[i], conv[i])

print(sum)


Error: TLE

Fixed submission:

N, M = map(int, input().split())
A = list(map(int, input().split()))
CB = []
for _ in range(M):
    b, c = map(int, input().split())
    CB.append((c, b))

# ascend
A.sort()

# descend
CB.sort(reverse=True)

# fill convert ary
conv = []
for cb in CB:
    c, b = cb
    conv.extend([c] * b)

cl = len(conv)

sum = 0
for i in range(N):
    if i >= cl:
        sum += A[i]
    else:
        sum += max(A[i], conv[i])

print(sum)
Problem p03038 (Python) — buggy submission, verdict: Time Limit Exceeded

from heapq import *


n, m = map(int, input().split())
PAIRS = [[-a, -1] for a in map(int, input().split())]
heapify(PAIRS)

for _ in range(m):
    b, c = map(int, input().split())
    heappush(PAIRS, [-c, -b])

answer = 0
for _ in range(n):
    pair = heappop(PAIRS)
    answer += -pair[0]
    pair[1] += 1
    if pair[1] != 0:
        heappush(PAIRS, pair)

print(answer)


Error: TLE

Fixed submission:

import bisect


n, m = map(int, input().split())
A = list(map(int, input().split()))
A.sort()

for _ in range(m):
    b, c = map(int, input().split())
    i = bisect.bisect_left(A, c)
    if i >= b:
        A[:b] = [c] * b
    else:
        A[:i] = [c] * i
    A.sort()

answer = sum(A)
print(answer)
Problem p03038 (Python) — buggy submission, verdict: Time Limit Exceeded

from bisect import bisect_right

n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
a.sort()
bc = [list(map(int, input().split())) for _ in range(m)]
bc.sort(key=lambda x: x[1], reverse=True)

res = 0
fin = 0
for b, c in bc:
    count = bisect_right(a, c, lo=fin, hi=min(fin + b, len(a))) - fin
    fin += count
    res += count * c
for i in range(fin, len(a)):
    res += a[i]

print(res)


Error: TLE

Fixed submission:

from bisect import bisect_right

n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
a.sort()
bc = [list(map(int, input().split())) for _ in range(m)]
bc.sort(key=lambda x: x[1], reverse=True)

res = 0
for b, c in bc:
    count = bisect_right(a, c, hi=min(b, len(a)))
    res += count * c
    if count > 0:
        a = a[count:]

print(res + sum(a))
Problem p03038 (Python) — buggy submission, verdict: Time Limit Exceeded

from heapq import heapify, heappush, heappop


N, M = map(int, input().split())
lst_A = list(map(int, input().split()))


hq = []
heapify(hq)
for a in lst_A:
    heappush(hq, [-a, 1])
for _ in range(M):
    b, c = map(int, input().split())
    heappush(hq, [-c, b])

ans = 0
while N > 0:
    c, b = heappop(hq)
    c *= -1
    if b < N:
        ans += c * b
        N -= b
    else:
        ans += c * N
        N = 0


print(ans)


Error: TLE

Fixed submission:

from heapq import heapify, heappush, heappop


N, M = map(int, input().split())
lst_A = list(map(int, input().split()))


numbers = []
heapify(numbers)
for a in lst_A:
    heappush(numbers, -a)
for _ in range(M):
    b, c = map(int, input().split())
    for _ in range(b):
        heappush(numbers, -c)


ans = -sum(heappop(numbers) for _ in range(N))
print(ans)
Problem p03038 (Python) — buggy submission, verdict: Runtime Error

N, M = map(int, input().split())
A = sorted(list(map(int, input().split())))
BC = [None] * M
for i in range(M):
    BC[i] = tuple(map(int, input().split()))
BC.sort(key=lambda tpl: tpl[1], reverse=True)  # sort by C descendant

D = [0] * N
i = 0
j = 0
while i < N and j < M:
    b, c = BC[j]
    for k in range(i, min(i + b, N)):
        D[k] = c
    i += b
    j += 1

for i in range(N):
    A[i] = max(A[i], D[i])

print(sum(A))


Error: 0

Fixed submission:

N, M = map(int, input().split())
A = sorted(list(map(int, input().split())))
BC = [None] * M
for i in range(M):
    BC[i] = tuple(map(int, input().split()))
BC.sort(key=lambda tpl: tpl[1], reverse=True)  # sort by C descendant

D = [0] * N
i = 0
j = 0
while i < N and j < M:
    b, c = BC[j]
    for k in range(i, i + b):
        D[k] = c
    i += b
    j += 1

for i in range(N):
    A[i] = max(A[i], D[i])

print(sum(A))
Problem p03038 (Python) — buggy submission, verdict: Time Limit Exceeded

from collections import Counter

N, M = map(int, input().split())
A = list(map(int, input().split()))
count_a = Counter(A)
A = [(v, k) for k, v in count_a.items()]
for i in range(M):
    B, C = map(int, input().split())
    A.append((B, C))
A = sorted(A, reverse=True, key=lambda x: x[1])
ans = 0
for k, v in A:
    N = N - k
    if N <= 0:
        ans += k * v + N * v
        break
    ans += k * v
print(ans)


Error: TLE

Fixed submission:

N, M = map(int, input().split())
A = list(map(int, input().split()))
min_a = min(A)
for i in range(M):
    B, C = map(int, input().split())
    if min_a < C:
        C = [C for i in range(B)]
        A.extend(C)
A = sorted(A, reverse=True)
print(sum(A[:N]))
Problem p03038 (Python) — buggy submission, verdict: Time Limit Exceeded

from sys import stdin

n, m = map(int, stdin.readline().split())
a = [int(num) for num in stdin.readline().split()]

cnt_list = [(a[i], 1) for i in range(n)]
for i in range(m):
    b, c = map(int, stdin.readline().split())
    cnt_list.append((c, b))

cnt_list.sort()
cnt_list.reverse()

cnt = n
ans = 0

for (k, v) in cnt_list:
    acc_v = min(v, cnt)
    ans += k * acc_v
    cnt -= acc_v


print(ans)


Error: TLE

Fixed submission:

import numpy as np
from sys import stdin

n, m = map(int, stdin.readline().split())
a = [int(num) for num in stdin.readline().split()]

cnt_dict = {}
mtr = [stdin.readline().rstrip().split() for _ in range(m)]
for row in mtr:
    cnt_dict.setdefault(int(row[1]), 0)
    cnt_dict[int(row[1])] += int(row[0])
for a_ in a:
    cnt_dict.setdefault(a_, 0)
    cnt_dict[a_] += 1

cnt_dict = sorted(cnt_dict.items(), key=lambda x: -x[0])

cnt = 0
ans = 0

for (k, v) in cnt_dict:
    v_ = np.min([v, n - cnt])
    ans += v_ * k
    cnt += v_
    if cnt == n:
        break


print(ans)
Problem p03038 (Python) — buggy submission, verdict: Time Limit Exceeded

N, M = [int(i) for i in input().strip().split()]
A = [int(i) for i in input().strip().split()]
A.sort()

BC = [[0, 0] for _ in range(M)]
for j in range(M):
    BC[j][0], BC[j][1] = [int(i) for i in input().strip().split()]
BC.sort(key=lambda x: x[1])

res = 0
cnt = 0
i = N - 1
j = M - 1
while True:
    if cnt == N:
        break

    if j == -1:
        res += A[i]
        i -= 1
    elif A[i] > BC[j][1]:
        res += A[i]
        i -= 1
    elif A[i] <= BC[j][1]:
        res += BC[j][1]
        BC[j][0] -= 1
        if BC[j][0] == 0:
            j -= 1
    else:
        raise ValueError
    cnt += 1
print(res)


Error: TLE

Fixed submission:

N, M = [int(i) for i in input().strip().split()]
A = [int(i) for i in input().strip().split()]
A.sort()
for i in range(M):
    B, C = [int(i) for i in input().strip().split()]
    is_changed = False
    for j in range(0, B):
        if A[j] < C:
            A[j] = C
            is_changed = True
        else:
            break
    if is_changed:
        A.sort()
print(sum(A))
Problem p03038 (Python) — buggy submission, verdict: Time Limit Exceeded

N, M = map(int, input().split())
A = list(map(int, input().split()))
opes = [tuple(map(int, input().split())) for _ in range(M)]
opes.sort(key=lambda x: -x[1])

D = []
lenD = 0
for B, C in opes:
    D += [C] * B
    lenD += B
    if lenD >= N:
        break

A.sort()
for i, d in enumerate(D[:N]):
    if A[i] >= d:
        break
    A[i] = d

print(sum(A))


Error: TLE

Fixed submission:

N, M = map(int, input().split())
A = list(map(int, input().split()))
opes = [tuple(map(int, input().split())) for _ in range(M)]

D = [C for B, C in opes for _ in range(B)]
D = sorted(D, reverse=True)[:N]

A.sort()
for i, d in enumerate(D):
    if A[i] >= d:
        break
    A[i] = d

print(sum(A))
Problem p03038 (Python) — buggy submission, verdict: Runtime Error

n, m = map(int, input().split())
a = list(map(int, input().split()))
bc = [tuple(map(int, input().split())) for _ in range(m)]
a.sort()
bc.sort(reverse=True, key=lambda x: x[1])
last_idx = len(a) - 1
idx = 0
for e in bc:
    b, c = e
    for i in range(b):
        if i + idx > last_idx:
            break
        if a[i + idx] < c:
            a[i + idx] = c
    idx += b

print(sum(a))


Error: WA

Fixed submission:

n, m = map(int, input().split())
a = list(map(int, input().split()))
bc = [tuple(map(int, input().split())) for _ in range(m)]
a.sort()
bc.sort(reverse=True, key=lambda x: x[1])
last_idx = len(a) - 1
idx = 0
for e in bc:
    b, c = e
    for i in range(b):
        if i + idx > last_idx:
            break
        if a[i + idx] < c:
            a[i + idx] = c
        else:
            break
    else:
        idx += b
        print(a)
        print(idx)
        continue
    break

print(sum(a))
Problem p03038 (Python) — buggy submission, verdict: Time Limit Exceeded

import sys


N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort(reverse=True)

ope = [list(map(int, x.split())) for x in sys.stdin.readlines()]

ope.sort(key=lambda x: -x[1])

mp = 0
ap = 0
cnt = 0
ans = 0

while cnt < N:
    a = A[ap]
    m = ope[mp][1] if mp < M else 0

    if m >= a:
        mc = min(ope[mp][0], N - cnt)
        ans += m * mc
        cnt += mc
        mp += 1
    else:
        ans += a
        cnt += 1
        ap += 1

print(ans)


Error: TLE

Fixed submission:

import sys


N, M = map(int, input().split())
A = list(map(int, input().split()))

ope = [tuple(map(int, x.split())) for x in sys.stdin.readlines()]

ope.sort(key=lambda x: -x[1])

cnt = 0
for b, c in ope:
    A = A + [c] * b
    cnt += b

    if cnt >= N:
        break

A.sort(reverse=True)

print(sum(A[:N]))
Problem p03038 (Python) — buggy submission, verdict: Time Limit Exceeded

import sys
import heapq

N, M = map(int, input().split())
A = list(map(int, input().split()))
origin = sum(A)
heapq.heapify(A)

BC = [list(map(int, input().split())) for _ in range(M)]
BC = sorted(BC, reverse=True, key=lambda x: x[1])

count = 0

for i in range(M):
    b = BC[i][0]
    c = BC[i][1]
    for _ in range(b):
        smallest = heapq.heappop(A)
        tmp = c - smallest
        if tmp <= 0:
            heapq.heappush(A, smallest)
            break
        else:
            heapq.heappush(A, c)
            origin += tmp
            count += 1
            if count >= N:
                print(origin)
                sys.exit()

print(origin)


Error: TLE

Fixed submission:

import heapq

N, M = map(int, input().split())
A = list(map(int, input().split()))
origin = sum(A)
heapq.heapify(A)

for _ in range(M):
    b, c = map(int, input().split())
    for _ in range(b):
        smallest = heapq.heappop(A)
        tmp = c - smallest
        if tmp <= 0:
            heapq.heappush(A, smallest)
            break
        else:
            heapq.heappush(A, c)
            origin += tmp

print(origin)
Problem p03038 (Python) — buggy submission, verdict: Runtime Error

N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
BC = [list(map(int, input().split())) for i in range(M)]
BC = sorted(BC, key=lambda x: -x[1])
i = 0
for B, C in BC:
    for _ in range(B):
        if i < N and A[i] < C:
            A[i] = C
            i += 1
        else:
            print(sum(A))
            exit()
print(sum(A))


Error: 0

Fixed submission:

N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
BC = [list(map(int, input().split())) for i in range(M)]
BC = sorted(BC, key=lambda x: -x[1])
i = 0
for B, C in BC:
    for _ in range(B):
        if A[i] < C:
            A[i] = C
            i += 1
        else:
            print(sum(A))
            exit()
print(sum(A))
Problem p03038 (Python) — buggy submission, verdict: Runtime Error

n, m = map(int, input().split())
a_list = list(map(int, input().split()))
bc_list = [list(map(int, input().split())) for _ in range(m)]
a_list.sort()
bc_list.sort(key=lambda i: -i[1])

a_idx = 0
bc_idx = 0
while bc_idx < len(bc_list):
    b, c = bc_list[bc_idx]
    finish = False
    for _ in range(b):
        if a_idx >= len(a_list):
            finish = True
            break
        if c > a_list[a_idx]:
            a_list[a_idx] = c
            a_idx += 1
        else:
            finish = True
            break
    if finish:
        break
    bc_idx += 1

print(sum(a_list))


Error: 0

Fixed submission:

n, m = map(int, input().split())
a_list = list(map(int, input().split()))
bc_list = [list(map(int, input().split())) for _ in range(m)]
a_list.sort()
bc_list.sort(key=lambda i: -i[1])

a_idx = 0
bc_idx = 0
while bc_idx < len(bc_list):
    b, c = bc_list[bc_idx]
    finish = False
    for _ in range(b):
        if c > a_list[a_idx]:
            a_list[a_idx] = c
            a_idx += 1
        else:
            finish = True
            break
    if finish:
        break
    bc_idx += 1

print(sum(a_list))
Problem p03038 (Python) — buggy submission, verdict: Runtime Error

def main():
    N, M = map(int, input().split())
    A = sorted(list(map(int, input().split())))
    BC = []
    for _ in range(M):
        B, C = map(int, input().split())
        BC.append((B, C))
    BC = sorted(BC, key=lambda x: x[1], reverse=True)
    i = 0
    for b, c in BC:
        for _ in range(b):
            if i >= len(A) or A[i] >= c:
                break
            A[i] = c
            i += 1
    print(sum(A))


main()


Error: 0

Fixed submission:

def main():
    N, M = map(int, input().split())
    A = sorted(list(map(int, input().split())))
    BC = []
    for _ in range(M):
        B, C = map(int, input().split())
        BC.append((B, C))
    BC = sorted(BC, key=lambda x: x[1], reverse=True)
    i = 0
    for b, c in BC:
        for _ in range(b):
            if A[i] >= c:
                break
            A[i] = c
            i += 1
    print(sum(A))


main()
Problem p03038 (Python) — buggy submission, verdict: Time Limit Exceeded

from sys import stdin, setrecursionlimit
from heapq import heapify, heappop, heappush


def main():
    input = stdin.buffer.readline
    n, m = map(int, input().split())
    a = list(map(int, input().split()))
    bc = [list(map(int, input().split())) for _ in range(m)]
    bc.sort(key=lambda x: -x[1])
    heapify(a)

    for i in range(m):
        b, c = bc[i]
        for _ in range(b):
            ai = heappop(a)
            if ai < c:
                heappush(a, c)
            else:
                heappush(a, ai)
                break

    print(sum(a))


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()


Error: TLE

Fixed submission:

from sys import stdin, setrecursionlimit
from heapq import heapify, heappop, heappush


def main():
    input = stdin.buffer.readline
    n, m = map(int, input().split())
    a = list(map(int, input().split()))
    heapify(a)

    for _ in range(m):
        b, c = map(int, input().split())
        for _ in range(b):
            ai = heappop(a)
            if ai < c:
                heappush(a, c)
            else:
                heappush(a, ai)
                break

    print(sum(a))


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()
Problem p03038 (Python) — buggy submission, verdict: Time Limit Exceeded

# 書き換える枚数は確定できる
# cの大きい順にしたものでaの小さい順にしたものを置き換えるイメージ
# なんか尺取っぽくなった

n, m = map(int, input().split())
arr = sorted(list(map(int, input().split())))

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

bcs = sorted(bcs, key=lambda x: x[1], reverse=True)
ans = 0

i = 0
for b in bcs:
    while i < n and arr[i] < b[1] and b[0] > 0:
        ans += b[1]
        b[0] -= 1
        i += 1
print(ans + sum(arr[i:]))


Error: TLE

Fixed submission:

import bisect

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

bc = list(sorted(bc, key=lambda x: x[1], reverse=True))
ans = 0

for bci in bc:
    d = bisect.bisect_left(arr, bci[1])
    arr = arr[min(d, bci[0]) :]
    ans += min(d, bci[0]) * bci[1]
print(ans + sum(arr))
Problem p03038 (Python) — buggy submission, verdict: Time Limit Exceeded

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

ans = 0
j = 0
for b, c in BC:
    for i in range(b):
        if j < n:
            ans += max(c, A[j])
            j += 1
        else:
            break

if j < n:
    for i in range(n - j):
        ans += A[n - 1 - i]

print(ans)


Error: TLE

Fixed submission:

n, m = map(int, input().split())
A = sorted(list(map(int, input().split())))
C = []
for _ in range(m):
    b, c = map(int, input().split())
    C.extend([c] * b)
C = sorted(C, reverse=True)

ans = 0
for i in range(n):
    try:
        ans += max(A[i], C[i])
    except IndexError:
        ans += A[i]

print(ans)
Problem p03038 (Python) — buggy submission, verdict: Time Limit Exceeded

n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
bc = [[0] * 2] * m

for i in range(m):
    bc[i] = list(map(int, input().split()))

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

cnt = 0

for e in bc:
    new += [e[1]] * e[0]
    cnt += e[0]

    if n <= cnt:
        break

for i in range(min(n, cnt)):
    a[i] = max(a[i], new[i])

print(sum(a))


Error: TLE

Fixed submission:

n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
bc = [[0] * 2] * m

for i in range(m):
    bc[i] = list(map(int, input().split()))

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

for e in bc:
    new.extend([e[1]] * e[0])

for i in range(min(n, len(new))):
    a[i] = max(a[i], new[i])

print(sum(a))