Buggy python submission (verdict: Time Limit Exceeded):
import bisect
n, m = list(map(int, input().split(' ')))
aaa = list(map(int, input().split(' ')))
aaa = sorted([-a for a in aaa])
len_aaa = len(aaa)

cccbbb = []
for i in range(m):
    b, c = list(map(int, input().split(' ')))
    cccbbb.append((-c, b))
cccbbb = sorted(cccbbb)
for c, b in cccbbb:
    index = bisect.bisect_left(aaa, c)
    if index < len_aaa:
        if b < len_aaa - index:
            ccc = [c] * b
            aaa = (aaa[:index] + ccc + aaa[index:len(aaa) - b])
        else:
            ccc = [c] * (len_aaa - index)
            aaa = (aaa[:index] + ccc)
    else:
        break
print(abs(sum(aaa)))

Fixed submission:
import bisect
n, m = list(map(int, input().split(' ')))
aaa = list(map(int, input().split(' ')))
aaa = sorted([-a for a in aaa])[::-1]
len_aaa = len(aaa)

cccbbb = []
for i in range(m):
    b, c = list(map(int, input().split(' ')))
    cccbbb.append((-c, b))
cccbbb = sorted(cccbbb)
ccc = []
for c, b in cccbbb:
    ccc += [c] * b
    if len(ccc) > len_aaa:
        break
ans = 0
for i in range(len_aaa):
    c = ccc[i] if i < len(ccc) else 0
    ans += min(aaa[i], c)
print(abs(ans))

Buggy python submission (verdict: Runtime Error):
import bisect
n, m = list(map(int, input().split(' ')))
aaa = list(map(int, input().split(' ')))
aaa = sorted([-a for a in aaa])
len_aaa = len(aaa)

cccbbb = []
for i in range(m):
    b, c = list(map(int, input().split(' ')))
    cccbbb.append((-c, b))
cccbbb = sorted(cccbbb)
for c, b in cccbbb:
    index = bisect.bisect_left(aaa, c)
    if index < len_aaa:
        if b < len_aaa - index + 1:
            ccc = [c] * b
            aaa = (aaa[:index] + ccc + aaa[index:len(aaa) - b])
        else:
            ccc = [c] * (len_aaa - index)
            aaa = (aaa[:index] + ccc)
        print(aaa)
    else:
        break
print(abs(sum(aaa)))

Fixed submission:
import bisect
n, m = list(map(int, input().split(' ')))
aaa = list(map(int, input().split(' ')))
aaa = sorted([-a for a in aaa])[::-1]
len_aaa = len(aaa)

cccbbb = []
for i in range(m):
    b, c = list(map(int, input().split(' ')))
    cccbbb.append((-c, b))
cccbbb = sorted(cccbbb)
ccc = []
for c, b in cccbbb:
    ccc += [c] * b
    if len(ccc) > len_aaa:
        break
ans = 0
for i in range(len_aaa):
    c = ccc[i] if i < len(ccc) else 0
    ans += min(aaa[i], c)
print(abs(ans))

Buggy python submission (verdict: Runtime Error):
import bisect
n, m = list(map(int, input().split(' ')))
aaa = list(map(int, input().split(' ')))
aaa = sorted([-a for a in aaa])
len_aaa = len(aaa)

cccbbb = []
for i in range(m):
    b, c = list(map(int, input().split(' ')))
    cccbbb.append((-c, b))
cccbbb = sorted(cccbbb)
for c, b in cccbbb:
    index = bisect.bisect_left(aaa, c)
    if index < len_aaa:
        ccc = [c] * min(b, len_aaa - index)
        aaa = (aaa[:index] + ccc + aaa[index:max(index, len_aaa - min(b, len_aaa - index))])
        print(aaa)
    else:
        break
print(abs(sum(aaa)))

Fixed submission:
import bisect
n, m = list(map(int, input().split(' ')))
aaa = list(map(int, input().split(' ')))
aaa = sorted([-a for a in aaa])[::-1]
len_aaa = len(aaa)

cccbbb = []
for i in range(m):
    b, c = list(map(int, input().split(' ')))
    cccbbb.append((-c, b))
cccbbb = sorted(cccbbb)
ccc = []
for c, b in cccbbb:
    ccc += [c] * b
    if len(ccc) > len_aaa:
        break
ans = 0
for i in range(len_aaa):
    c = ccc[i] if i < len(ccc) else 0
    ans += min(aaa[i], c)
print(abs(ans))

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
n, m = list(map(int, input().split(' ')))
aaa = list(map(int, input().split(' ')))
aaa = sorted([-a for a in aaa])
len_aaa = len(aaa)

cccbbb = []
for i in range(m):
    b, c = list(map(int, input().split(' ')))
    cccbbb.append((-c, b))
cccbbb = sorted(cccbbb)
for c, b in cccbbb:
    index = bisect.bisect_left(aaa, c)
    if index < len_aaa:
        ccc = [c] * min(b, len_aaa - index + 1)
        aaa = (aaa[:index] + ccc + aaa[index:])[:len_aaa]
    else:
        break
print(abs(sum(aaa)))

Fixed submission:
import bisect
n, m = list(map(int, input().split(' ')))
aaa = list(map(int, input().split(' ')))
aaa = sorted([-a for a in aaa])[::-1]
len_aaa = len(aaa)

cccbbb = []
for i in range(m):
    b, c = list(map(int, input().split(' ')))
    cccbbb.append((-c, b))
cccbbb = sorted(cccbbb)
ccc = []
for c, b in cccbbb:
    ccc += [c] * b
    if len(ccc) > len_aaa:
        break
ans = 0
for i in range(len_aaa):
    c = ccc[i] if i < len(ccc) else 0
    ans += min(aaa[i], c)
print(abs(ans))

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
n, m = list(map(int, input().split(' ')))
aaa = list(map(int, input().split(' ')))
len_aaa = len(aaa)
aaa = sorted([-a for a in aaa])

for i in range(m):
    b, c = list(map(int, input().split(' ')))
    ccc = [-c] * min(b, len_aaa)
    index = bisect.bisect_left(aaa, -c)
    aaa = (aaa[:index] + ccc + aaa[index:])[:len_aaa]
print(abs(sum(aaa)))

Fixed submission:
import bisect
n, m = list(map(int, input().split(' ')))
aaa = list(map(int, input().split(' ')))
aaa = sorted([-a for a in aaa])[::-1]
len_aaa = len(aaa)

cccbbb = []
for i in range(m):
    b, c = list(map(int, input().split(' ')))
    cccbbb.append((-c, b))
cccbbb = sorted(cccbbb)
ccc = []
for c, b in cccbbb:
    ccc += [c] * b
    if len(ccc) > len_aaa:
        break
ans = 0
for i in range(len_aaa):
    c = ccc[i] if i < len(ccc) else 0
    ans += min(aaa[i], c)
print(abs(ans))

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
n, m = list(map(int, input().split(' ')))
aaa = list(map(int, input().split(' ')))
len_aaa = len(aaa)
aaa = sorted([-a for a in aaa])

for i in range(m):
    b, c = list(map(int, input().split(' ')))
    ccc = [-c] * b
    index = bisect.bisect_left(aaa, -c)
    aaa = (aaa[:index] + ccc + aaa[index:])[:len_aaa]
print(abs(sum(aaa)))

Fixed submission:
import bisect
n, m = list(map(int, input().split(' ')))
aaa = list(map(int, input().split(' ')))
aaa = sorted([-a for a in aaa])[::-1]
len_aaa = len(aaa)

cccbbb = []
for i in range(m):
    b, c = list(map(int, input().split(' ')))
    cccbbb.append((-c, b))
cccbbb = sorted(cccbbb)
ccc = []
for c, b in cccbbb:
    ccc += [c] * b
    if len(ccc) > len_aaa:
        break
ans = 0
for i in range(len_aaa):
    c = ccc[i] if i < len(ccc) else 0
    ans += min(aaa[i], c)
print(abs(ans))

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
n, m = list(map(int, input().split(' ')))
aaa = list(map(int, input().split(' ')))
len_aaa = len(aaa)
aaa = sorted([-a for a in aaa])

for i in range(m):
    b, c = list(map(int, input().split(' ')))
    index = bisect.bisect_left(aaa, -c)
    for j in range(b):
        aaa.insert(index, -c)
    aaa = aaa[:len_aaa]
print(abs(sum(aaa)))

Fixed submission:
import bisect
n, m = list(map(int, input().split(' ')))
aaa = list(map(int, input().split(' ')))
aaa = sorted([-a for a in aaa])[::-1]
len_aaa = len(aaa)

cccbbb = []
for i in range(m):
    b, c = list(map(int, input().split(' ')))
    cccbbb.append((-c, b))
cccbbb = sorted(cccbbb)
ccc = []
for c, b in cccbbb:
    ccc += [c] * b
    if len(ccc) > len_aaa:
        break
ans = 0
for i in range(len_aaa):
    c = ccc[i] if i < len(ccc) else 0
    ans += min(aaa[i], c)
print(abs(ans))

Buggy python submission (verdict: Time Limit Exceeded):
n, m = list(map(int, input().split(' ')))
aaa = list(map(int, input().split(' ')))
len_aaa = len(aaa)
# 全部混ぜて長さがaの分だけ多きい方から取る
for i in range(m):
    b, c = list(map(int, input().split(' ')))
    ccc = [c] * b
    aaa = sorted(ccc + aaa)[::-1][:len_aaa]
print(sum(aaa))

Fixed submission:
import bisect
n, m = list(map(int, input().split(' ')))
aaa = list(map(int, input().split(' ')))
aaa = sorted([-a for a in aaa])[::-1]
len_aaa = len(aaa)

cccbbb = []
for i in range(m):
    b, c = list(map(int, input().split(' ')))
    cccbbb.append((-c, b))
cccbbb = sorted(cccbbb)
ccc = []
for c, b in cccbbb:
    ccc += [c] * b
    if len(ccc) > len_aaa:
        break
ans = 0
for i in range(len_aaa):
    c = ccc[i] if i < len(ccc) else 0
    ans += min(aaa[i], c)
print(abs(ans))

Buggy python submission (verdict: Time Limit Exceeded):
n, m = list(map(int, input().split(' ')))
aaa = list(map(int, input().split(' ')))
aaa = sorted(aaa)
len_aaa = len(aaa)
cccbbb = []
for i in range(m):
    b, c = list(map(int, input().split(' ')))
    cccbbb += [c] * b
    cccbbb = sorted(cccbbb)[::-1]
    if len(cccbbb) > len(aaa):
        cccbbb = cccbbb[:len_aaa]
len_cccbbb = len(cccbbb)
if len_aaa < len_cccbbb:
    print(sum([max(a, c) for a, c in zip(aaa, cccbbb[:len_aaa])]))
elif len_aaa >= len_cccbbb:
    print(sum([max(a, c) for a, c in zip(aaa, cccbbb + [0] * (len_aaa - len_cccbbb))]))


Fixed submission:
import bisect
n, m = list(map(int, input().split(' ')))
aaa = list(map(int, input().split(' ')))
aaa = sorted([-a for a in aaa])[::-1]
len_aaa = len(aaa)

cccbbb = []
for i in range(m):
    b, c = list(map(int, input().split(' ')))
    cccbbb.append((-c, b))
cccbbb = sorted(cccbbb)
ccc = []
for c, b in cccbbb:
    ccc += [c] * b
    if len(ccc) > len_aaa:
        break
ans = 0
for i in range(len_aaa):
    c = ccc[i] if i < len(ccc) else 0
    ans += min(aaa[i], c)
print(abs(ans))

Buggy python submission (verdict: Time Limit Exceeded):
n, m = list(map(int, input().split(' ')))
aaa = list(map(int, input().split(' ')))
r = 100000
cccbbb = []
for i in range(m):
    b, c = list(map(int, input().split(' ')))
    cccbbb += [c] * b
aaa = sorted(aaa)
cccbbb = sorted(cccbbb)[::-1]
len_cccbbb = len(cccbbb)
len_aaa = len(aaa)
#print(aaa)
#print(cccbbb)
if len_aaa < len_cccbbb:
    #print(([max(a, c) for a, c in zip(aaa, cccbbb[:len_aaa])]))
    print(sum([max(a, c) for a, c in zip(aaa, cccbbb[:len_aaa])]))
elif len_aaa >= len_cccbbb:
    #print(([max(a, c) for a, c in zip(aaa, cccbbb + [0] * (len_aaa - len_cccbbb))]))
    print(sum([max(a, c) for a, c in zip(aaa, cccbbb + [0] * (len_aaa - len_cccbbb))]))


Fixed submission:
import bisect
n, m = list(map(int, input().split(' ')))
aaa = list(map(int, input().split(' ')))
aaa = sorted([-a for a in aaa])[::-1]
len_aaa = len(aaa)

cccbbb = []
for i in range(m):
    b, c = list(map(int, input().split(' ')))
    cccbbb.append((-c, b))
cccbbb = sorted(cccbbb)
ccc = []
for c, b in cccbbb:
    ccc += [c] * b
    if len(ccc) > len_aaa:
        break
ans = 0
for i in range(len_aaa):
    c = ccc[i] if i < len(ccc) else 0
    ans += min(aaa[i], c)
print(abs(ans))