Buggy python submission (verdict: Runtime Error):
n,m = map(int,input().split())
a_ls = list(map(int, input().split()))
n_b = 0
b_ls = [[0,0] for _ in range(m)]
for i in range(m):
    num, c = map(int,input().split())
    n_b += num
    b_ls[i] = [num,c]
d_ls = [0] * (n+n_b)
c_ls = [0] * n_b
j = 0
rest = b_ls[0][0]
i = 0
while j < n_b:
    c_ls[j] = b_ls[i][1]
    j += 1
    rest -= 1
    if rest <= 0:
        i += 1
        if i == m:
            break
        rest = b_ls[i][0]
for i in range(n+n_b):
    if i < n:
        d_ls[i] = a_ls[i]
    else:
        d_ls[i] = c_ls[i-n]
d_ls.sort(reverse= True)
print(sum(d_ls[:n]))



Fixed submission:
from collections import Counter
n,m = map(int,input().split())
a_ls = list(map(int, input().split()))
counter = Counter(a_ls)
for i in range(m):
    b,c = map(int,input().split())
    counter[c] += b

cards = sorted(counter.items(), key = lambda x:x[0], reverse=True)
rest = n
Sum = 0
for num, count in cards:
    if count < rest:
        Sum += num * count
        rest -= count
    else:
        Sum += rest * num
        break
print(Sum)

Buggy python submission (verdict: Runtime Error):
n,m = map(int,input().split())
a_ls = list(map(int, input().split()))
n_b = 0
b_ls = [[0,0] for _ in range(m)]
for i in range(m):
    num, c = map(int,input().split())
    n_b += num
    b_ls[i] = [num,c]
c_ls = [0] * n_b
j = 0
rest = b_ls[0][0]
i = 0
while j < n_b:
    c_ls[j] = b_ls[i][1]
    j += 1
    rest -= 1
    if rest <= 0:
        i += 1
        if i == m:
            break
        rest = b_ls[i][0]
a_ls = a_ls + c_ls
a_ls.sort(reverse= True)
print(sum(a_ls[:n]))



Fixed submission:
from collections import Counter
n,m = map(int,input().split())
a_ls = list(map(int, input().split()))
counter = Counter(a_ls)
for i in range(m):
    b,c = map(int,input().split())
    counter[c] += b

cards = sorted(counter.items(), key = lambda x:x[0], reverse=True)
rest = n
Sum = 0
for num, count in cards:
    if count < rest:
        Sum += num * count
        rest -= count
    else:
        Sum += rest * num
        break
print(Sum)

Buggy python submission (verdict: Runtime Error):
n,m = map(int,input().split())
a_ls = list(map(int, input().split()))
n_b = 0
b_ls = [[0,0] for _ in range(m)]
for i in range(m):
    num, c = map(int,input().split())
    n_b += num
    b_ls[i] = [num,c]
c_ls = [0] * n_b
j = 0
rest = b_ls[0][0]
i = 0
while j < n_b:
    c_ls[j] = b_ls[i][1]
    j += 1
    rest -= 1
    if rest <= 0:
        i += 1
        if i == m:
            break
        rest = b_ls[i][0]
a_ls = a_ls + c_ls
a_ls.sort(reverse= True)
print(sum(a_ls[:n]))



Fixed submission:
from collections import Counter
n,m = map(int,input().split())
a_ls = list(map(int, input().split()))
counter = Counter(a_ls)
for i in range(m):
    b,c = map(int,input().split())
    counter[c] += b

cards = sorted(counter.items(), key = lambda x:x[0], reverse=True)
rest = n
Sum = 0
for num, count in cards:
    if count < rest:
        Sum += num * count
        rest -= count
    else:
        Sum += rest * num
        break
print(Sum)

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
a_ls = list(map(int, input().split()))
b_ls = []
for i in range(m):
    num, c = map(int,input().split())
    for _ in range(num):
        b_ls.append(c)
a_ls = a_ls + b_ls
a_ls.sort(reverse= True)
print(sum(a_ls[:n]))



Fixed submission:
from collections import Counter
n,m = map(int,input().split())
a_ls = list(map(int, input().split()))
counter = Counter(a_ls)
for i in range(m):
    b,c = map(int,input().split())
    counter[c] += b

cards = sorted(counter.items(), key = lambda x:x[0], reverse=True)
rest = n
Sum = 0
for num, count in cards:
    if count < rest:
        Sum += num * count
        rest -= count
    else:
        Sum += rest * num
        break
print(Sum)

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
a_ls = list(map(int, input().split()))
for i in range(m):
    b,c = map(int,input().split())
    for _ in range(b):
        a_ls.append(c)

a_ls.sort(reverse = True)
print(sum(a_ls[:n]))

Fixed submission:
from collections import Counter
n,m = map(int,input().split())
a_ls = list(map(int, input().split()))
counter = Counter(a_ls)
for i in range(m):
    b,c = map(int,input().split())
    counter[c] += b

cards = sorted(counter.items(), key = lambda x:x[0], reverse=True)
rest = n
Sum = 0
for num, count in cards:
    if count < rest:
        Sum += num * count
        rest -= count
    else:
        Sum += rest * num
        break
print(Sum)

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
a_ls = list(map(int, input().split()))
a_ls.sort()
def isOK(index, key, l):
    if l[index] >= key:
        return True
    else:
        return False
def meguru_search(l, key):
    '''
    key以上の値を持つ最小のインデックスを返す
    '''
    left = -1
    right = len(l)

    while (right - left) > 1:
        mid = left + (right - left) // 2
        # 直感的に記憶　
            # 「含まれている」んだったら「含むを許容（等号付き）する右側」はmidまで動かして問題ない
                # 新しいrightの位置がkeyの位置だとしても、「含む」は許容されてる
            # 「含まれていない」んだったら「含まない(<)左の壁」をそこまで動かしてもkeyを通り過ぎることはない
        if isOK(mid, key, l):
            right = mid
        else:
            left = mid
    
    # whileを抜ける時、rightとleftは一個差で、かつkeyがrightの位置にある
    return right
for i in range(m):
    b,c = map(int,input().split())
    ind = meguru_search(a_ls, c)
    # ind==bより小さい数が書かれたカードの数
    if ind >= b:
        a_ls[:b] = [c] * b
    else:
        a_ls[:ind] = [c] * ind
    a_ls.sort()
print(sum(a_ls))

Fixed submission:
from collections import Counter
n,m = map(int,input().split())
a_ls = list(map(int, input().split()))
counter = Counter(a_ls)
for i in range(m):
    b,c = map(int,input().split())
    counter[c] += b

cards = sorted(counter.items(), key = lambda x:x[0], reverse=True)
rest = n
Sum = 0
for num, count in cards:
    if count < rest:
        Sum += num * count
        rest -= count
    else:
        Sum += rest * num
        break
print(Sum)

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
a_ls = list(map(int, input().split()))
a_ls.sort()
def isOK(index, key, l):
    if l[index] >= key:
        return True
    else:
        return False
def meguru_search(l, key):
    '''
    key以上の値を持つ最小のインデックスを返す
    '''
    left = -1
    right = len(l)

    while (right - left) > 1:
        mid = left + (right - left) // 2
        # 直感的に記憶　
            # 「含まれている」んだったら「含むを許容（等号付き）する右側」はmidまで動かして問題ない
                # 新しいrightの位置がkeyの位置だとしても、「含む」は許容されてる
            # 「含まれていない」んだったら「含まない(<)左の壁」をそこまで動かしてもkeyを通り過ぎることはない
        if isOK(mid, key, l):
            right = mid
        else:
            left = mid
    
    # whileを抜ける時、rightとleftは一個差で、かつkeyがrightの位置にある
    return right
for i in range(m):
    b,c = map(int,input().split())
    ind = meguru_search(a_ls, c)
    # ind==bより小さい数が書かれたカードの数
    if ind >= b:
        a_ls[:b] = [c for _ in range(b)]
    else:
        a_ls[:ind] = [c for _ in range(ind)]
    a_ls.sort()
print(sum(a_ls))



Fixed submission:
from collections import Counter
n,m = map(int,input().split())
a_ls = list(map(int, input().split()))
counter = Counter(a_ls)
for i in range(m):
    b,c = map(int,input().split())
    counter[c] += b

cards = sorted(counter.items(), key = lambda x:x[0], reverse=True)
rest = n
Sum = 0
for num, count in cards:
    if count < rest:
        Sum += num * count
        rest -= count
    else:
        Sum += rest * num
        break
print(Sum)