Buggy python submission (verdict: Time Limit Exceeded):

n, m = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
result = sum(a)
b_ = []
for i in range(m):
    b, c = map(int, input().split(' '))
    tmp = [c] * b
    b_ += tmp
result = a + b_
result.sort(reverse=True)
print(sum(result[:n]))

Fixed submission:

n, m = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
result = sum(a)
b_ = []
for i in range(m):
    b, c = map(int, input().split(' '))
    b_ += [(b,c)]
for i in range(n):
    b_ += [(1, a[i])]

from operator import itemgetter
b_.sort(key=itemgetter(1), reverse=True)

count = n
res = 0
for j, k in b_:
    if count > j:
        res += k * j
        count -= j
    elif count <= j:
        res += k * count
        break
print(res)

Buggy python submission (verdict: Runtime Error):

n, m = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
result = sum(a)
b_ = []
for i in range(m):
    b, c = map(int, input().split(' '))
    tmp = [c] * b
    b_ += tmp
result = a + b_
result = result[:n].sort(reverse=True)
print(sum(result[:n]))

Fixed submission:

n, m = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
result = sum(a)
b_ = []
for i in range(m):
    b, c = map(int, input().split(' '))
    b_ += [(b,c)]
for i in range(n):
    b_ += [(1, a[i])]

from operator import itemgetter
b_.sort(key=itemgetter(1), reverse=True)

count = n
res = 0
for j, k in b_:
    if count > j:
        res += k * j
        count -= j
    elif count <= j:
        res += k * count
        break
print(res)

Buggy python submission (verdict: Time Limit Exceeded):

n, m = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
result = sum(a)
b_ = []
for i in range(m):
    b, c = map(int, input().split(' '))
    tmp = [c] * b
    b_ += tmp
result = a + b_
result.sort(reverse=True)
print(sum(result[:n]))

Fixed submission:

n, m = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
result = sum(a)
b_ = []
for i in range(m):
    b, c = map(int, input().split(' '))
    b_ += [(b,c)]
for i in range(n):
    b_ += [(1, a[i])]

from operator import itemgetter
b_.sort(key=itemgetter(1), reverse=True)

count = n
res = 0
for j, k in b_:
    if count > j:
        res += k * j
        count -= j
    elif count <= j:
        res += k * count
        break
print(res)

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
result = sum(a)
b_ = []
for i in range(m):
    b, c = map(int, input().split(' '))
    tmp = [c] * b
    b_ += tmp
b_.sort(reverse=True)
a.sort()
if len(b_) > n:
    b_ = b_[:n]
for j,k in zip(a, b_):
    if j < k:
        a[a.index(j)] = k
    else:
        break
print(sum(a))


Fixed submission:

n, m = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
result = sum(a)
b_ = []
for i in range(m):
    b, c = map(int, input().split(' '))
    b_ += [(b,c)]
for i in range(n):
    b_ += [(1, a[i])]

from operator import itemgetter
b_.sort(key=itemgetter(1), reverse=True)

count = n
res = 0
for j, k in b_:
    if count > j:
        res += k * j
        count -= j
    elif count <= j:
        res += k * count
        break
print(res)

Buggy python submission (verdict: Time Limit Exceeded):

n, m = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
result = sum(a)
b_ = []
for i in range(m):
    b, c = map(int, input().split(' '))
    tmp = [c] * b
    b_ += tmp
b_.sort(reverse=True)
a.sort()
if len(b_) > n:
    b_ = b_[:n]
for i,(j,k) in enumerate(zip(a, b_)):
    if j < k:
        a[i] = k
    else:
        break
print(sum(a))


Fixed submission:

n, m = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
result = sum(a)
b_ = []
for i in range(m):
    b, c = map(int, input().split(' '))
    b_ += [(b,c)]
for i in range(n):
    b_ += [(1, a[i])]

from operator import itemgetter
b_.sort(key=itemgetter(1), reverse=True)

count = n
res = 0
for j, k in b_:
    if count > j:
        res += k * j
        count -= j
    elif count <= j:
        res += k * count
        break
print(res)

Buggy python submission (verdict: Time Limit Exceeded):

n, m = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
result = sum(a)
b_ = []
for i in range(m):
    b, c = map(int, input().split(' '))
    tmp = [c] * b
    b_ += tmp
b_.sort(reverse=True)
a.sort()
if len(b_) > n:
    b_ = b_[:n]
for i,(j,k) in enumerate(zip(a, b_)):
    if j < k:
        a[i] = k
    else:
        break
print(sum(a))


Fixed submission:

n, m = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
result = sum(a)
b_ = []
for i in range(m):
    b, c = map(int, input().split(' '))
    b_ += [(b,c)]
for i in range(n):
    b_ += [(1, a[i])]

from operator import itemgetter
b_.sort(key=itemgetter(1), reverse=True)

count = n
res = 0
for j, k in b_:
    if count > j:
        res += k * j
        count -= j
    elif count <= j:
        res += k * count
        break
print(res)

Buggy python submission (verdict: Time Limit Exceeded):

n, m = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
result = sum(a)
b_ = []
for i in range(m):
    b, c = map(int, input().split(' '))
    tmp = [c] * b
    b_ += tmp
b_.sort(reverse=True)
a.sort()
if len(b_) > n:
    b_ = b_[:n]
for i,(j,k) in enumerate(zip(a, b_)):
    if j < k:
        a[i] = k
print(sum(a))

Fixed submission:

n, m = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
result = sum(a)
b_ = []
for i in range(m):
    b, c = map(int, input().split(' '))
    b_ += [(b,c)]
for i in range(n):
    b_ += [(1, a[i])]

from operator import itemgetter
b_.sort(key=itemgetter(1), reverse=True)

count = n
res = 0
for j, k in b_:
    if count > j:
        res += k * j
        count -= j
    elif count <= j:
        res += k * count
        break
print(res)

Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
result = sum(a)
b_ = {}
for i in range(m):
    b, c = map(int, input().split(' '))
    b_[c] = b_.get(c, 0)
    b_[c] += b
if max(b_.keys()) < min(a):
    print(result)
else:
    a.sort()
    for j in range((len(a)-1)//2 + 2):
        t = max(b_.keys())
        if a[j] < t:
            a[j] = t
            b_[t] -= 1
        if b_[t] == 0:
            b_.pop(t)
        if b_ == {}:
            break
        t = max(b_.keys())        
        if a[n - j -1] < t:
            a[n-j-1] = t
            b_[t] -= 1
        if b_[t] == 0:
            b_.pop(t)
        if b_ == {}:
            break
    print(sum(a))

Fixed submission:

n, m = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
result = sum(a)
b_ = []
for i in range(m):
    b, c = map(int, input().split(' '))
    b_ += [(b,c)]
for i in range(n):
    b_ += [(1, a[i])]

from operator import itemgetter
b_.sort(key=itemgetter(1), reverse=True)

count = n
res = 0
for j, k in b_:
    if count > j:
        res += k * j
        count -= j
    elif count <= j:
        res += k * count
        break
print(res)

Buggy python submission (verdict: Runtime Error):
a = list(map(int, input().split(' ')))
result = sum(a)
b_ = {}
for i in range(m):
    b, c = map(int, input().split(' '))
    b_[c] = b_.get(c, 0)
    b_[c] += b
if max(b_.keys()) < min(a):
    print(result)
else:
    a.sort()
    for j in range((len(a)-1)//2 + 2):
        t = max(b_.keys())
        if a[j] < t:
            a[j] = t
            b_[t] -= 1
        if b_[t] == 0:
            b_.pop(t)
        if b_ == {}:
            break
        t = max(b_.keys())        
        if a[n - j -1] < t:
            a[n-j-1] = t
            b_[t] -= 1
        if b_[t] == 0:
            b_.pop(t)
        if b_ == {}:
            break
    print(sum(a))

Fixed submission:

n, m = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
result = sum(a)
b_ = []
for i in range(m):
    b, c = map(int, input().split(' '))
    b_ += [(b,c)]
for i in range(n):
    b_ += [(1, a[i])]

from operator import itemgetter
b_.sort(key=itemgetter(1), reverse=True)

count = n
res = 0
for j, k in b_:
    if count > j:
        res += k * j
        count -= j
    elif count <= j:
        res += k * count
        break
print(res)

Buggy python submission (verdict: Time Limit Exceeded):

n, m = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
result = sum(a)
b_ = {}
for i in range(m):
    b, c = map(int, input().split(' '))
    b_[c] = b_.get(c, 0)
    b_[c] += b
if max(b_.keys()) < min(a):
    print(result)
else:
    for j, k in enumerate(a):
        t = max(b_.keys())
        if k < t:
            a[j] = t
            b_[t] -= 1
        if b_[t] == 0:
            b_.pop(t)
        if b_ == {}:
            break
    print(sum(a))

Fixed submission:

n, m = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
result = sum(a)
b_ = []
for i in range(m):
    b, c = map(int, input().split(' '))
    b_ += [(b,c)]
for i in range(n):
    b_ += [(1, a[i])]

from operator import itemgetter
b_.sort(key=itemgetter(1), reverse=True)

count = n
res = 0
for j, k in b_:
    if count > j:
        res += k * j
        count -= j
    elif count <= j:
        res += k * count
        break
print(res)