Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
A = [int(i) for i in input().split()]
 
L = []
for i in range(m):
    b,c = map(int, input().split())
    L.append((b,c))
    
A = sorted(A)
 
from bisect import bisect_left
import numpy as np
A = np.array(A)
for l in L:
    tmp0 = l[0]
    tmp1 = l[1]
    A_len = bisect_left(A, tmp1)
    if A_len >= tmp0:
        A = np.insert(A, A_len, [tmp1]*tmp0)[tmp0:]
    else:
        A[0:A_len] = tmp1
        
print(sum(A))

Fixed submission:
N, M = map(int, input().split())
A = [int(i) for i in input().split()]

D = {}
for i in range(M):
    b,c = map(int, input().split())
    if c in D:
        D[c] += b
    else:
        D[c] = b
        
for a in A:
    if a in D:
        D[a] += 1
    else:
        D[a] = 1
        
D_sort = sorted(D.items(), reverse=True)
check = 0
ans = 0
for d in D_sort:
    check += d[1]
    if check <= N:
        ans += d[0] * d[1]
    else:
        ans += d[0] * (d[1] - (check - N))
        break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())

A = [int(i) for i in input().split()]

L = []
for i in range(m):
    b,c = map(int, input().split())
    L.append((b,c))
    
A = sorted(A)

from bisect import bisect_left
for l in L:
    tmp0 = l[0]
    tmp1 = l[1]
    A_len = bisect_left(A, tmp1)
    if tmp0 < A_len:
        A = A[tmp0:A_len]+[tmp1] *tmp0+A[A_len:]
    else:
        A = [tmp1] *A_len+A[A_len:]
        
print(sum(A))

Fixed submission:
N, M = map(int, input().split())
A = [int(i) for i in input().split()]

D = {}
for i in range(M):
    b,c = map(int, input().split())
    if c in D:
        D[c] += b
    else:
        D[c] = b
        
for a in A:
    if a in D:
        D[a] += 1
    else:
        D[a] = 1
        
D_sort = sorted(D.items(), reverse=True)
check = 0
ans = 0
for d in D_sort:
    check += d[1]
    if check <= N:
        ans += d[0] * d[1]
    else:
        ans += d[0] * (d[1] - (check - N))
        break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())

A = [int(i) for i in input().split()]

L = []
for i in range(m):
    b,c = map(int, input().split())
    L.append((b,c))
    
A = sorted(A)

from bisect import bisect_left
for l in L:
    tmp0 = l[0]
    tmp1 = l[1]
    A_len = bisect_left(A, tmp1)
    if tmp0 < A_len:
        A = A[tmp0:A_len]+[tmp1] *tmp0+A[A_len:]
    else:
        A = [tmp1] *A_len+A[A_len:]
        
print(sum(A))

Fixed submission:
N, M = map(int, input().split())
A = [int(i) for i in input().split()]

D = {}
for i in range(M):
    b,c = map(int, input().split())
    if c in D:
        D[c] += b
    else:
        D[c] = b
        
for a in A:
    if a in D:
        D[a] += 1
    else:
        D[a] = 1
        
D_sort = sorted(D.items(), reverse=True)
check = 0
ans = 0
for d in D_sort:
    check += d[1]
    if check <= N:
        ans += d[0] * d[1]
    else:
        ans += d[0] * (d[1] - (check - N))
        break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())

A = [int(i) for i in input().split()]

L = []
for i in range(m):
    b,c = map(int, input().split())
    L.append((b,c))
    
A = sorted(A)

from bisect import bisect_left
for l in L:
    tmp0 = l[0]
    tmp1 = l[1]
    A_len = bisect_left(A, tmp1)
    if tmp0 < A_len:
        A = A[tmp0:A_len]+[tmp1 for i in range(tmp0)]+A[A_len:]
    else:
        A = [tmp1 for i in range(A_len)]+A[A_len:]
        
        
print(sum(A))

Fixed submission:
N, M = map(int, input().split())
A = [int(i) for i in input().split()]

D = {}
for i in range(M):
    b,c = map(int, input().split())
    if c in D:
        D[c] += b
    else:
        D[c] = b
        
for a in A:
    if a in D:
        D[a] += 1
    else:
        D[a] = 1
        
D_sort = sorted(D.items(), reverse=True)
check = 0
ans = 0
for d in D_sort:
    check += d[1]
    if check <= N:
        ans += d[0] * d[1]
    else:
        ans += d[0] * (d[1] - (check - N))
        break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())

A = [int(i) for i in input().split()]

L = []
for i in range(m):
    b,c = map(int, input().split())
    L.append((b,c))
    
A = sorted(A)

from bisect import bisect_left
import numpy as np
A = np.array(A)
for l in L:
    tmp0 = l[0]
    tmp1 = l[1]
    A_len = bisect_left(A, tmp1)
    A = np.insert(A, A_len, [tmp1 for i in range(tmp0)])[tmp0:]
    
print(sum(A))

Fixed submission:
N, M = map(int, input().split())
A = [int(i) for i in input().split()]

D = {}
for i in range(M):
    b,c = map(int, input().split())
    if c in D:
        D[c] += b
    else:
        D[c] = b
        
for a in A:
    if a in D:
        D[a] += 1
    else:
        D[a] = 1
        
D_sort = sorted(D.items(), reverse=True)
check = 0
ans = 0
for d in D_sort:
    check += d[1]
    if check <= N:
        ans += d[0] * d[1]
    else:
        ans += d[0] * (d[1] - (check - N))
        break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
A = [int(i) for i in input().split()]

L = []
for i in range(m):
    b,c = map(int, input().split())
    L.append((b,c))
    
A = sorted(A)

from bisect import bisect_left
import numpy as np
A = np.array(A)
for l in L:
    tmp0 = l[0]
    tmp1 = l[1]
    A_len = bisect_left(A, tmp1)
    if A_len >= tmp0:
        A = np.insert(A, A_len, [tmp1 for i in range(tmp0)])[tmp0:]
    else:
        A[0:A_len] = tmp1
        
print(sum(A))

Fixed submission:
N, M = map(int, input().split())
A = [int(i) for i in input().split()]

D = {}
for i in range(M):
    b,c = map(int, input().split())
    if c in D:
        D[c] += b
    else:
        D[c] = b
        
for a in A:
    if a in D:
        D[a] += 1
    else:
        D[a] = 1
        
D_sort = sorted(D.items(), reverse=True)
check = 0
ans = 0
for d in D_sort:
    check += d[1]
    if check <= N:
        ans += d[0] * d[1]
    else:
        ans += d[0] * (d[1] - (check - N))
        break
print(ans)

Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
 
A = [int(i) for i in input().split()]
 
L = []
for i in range(m):
    b,c = map(int, input().split())
    L.append((b,c))
    
A = sorted(A)
 
from bisect import bisect_left
import numpy as np
A = np.array(A)
for l in L:
    tmp0 = l[0]
    tmp1 = l[1]
    A_select = A[0:tmp0]
    A_len = bisect_left(A_select, tmp1)
    A[0:A_len] = tmp1
    A = np.sort(A)
    
    
print(sum(A))

Fixed submission:
N, M = map(int, input().split())
A = [int(i) for i in input().split()]

D = {}
for i in range(M):
    b,c = map(int, input().split())
    if c in D:
        D[c] += b
    else:
        D[c] = b
        
for a in A:
    if a in D:
        D[a] += 1
    else:
        D[a] = 1
        
D_sort = sorted(D.items(), reverse=True)
check = 0
ans = 0
for d in D_sort:
    check += d[1]
    if check <= N:
        ans += d[0] * d[1]
    else:
        ans += d[0] * (d[1] - (check - N))
        break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())

A = [int(i) for i in input().split()]

L = []
for i in range(m):
    b,c = map(int, input().split())
    L.append((b,c))
    
A = sorted(A)

from bisect import bisect_left
import numpy as np
A = np.array(A)
for l in L:
    tmp0 = l[0]
    tmp1 = l[1]
    A_select = A[0:tmp0]
    A_len = bisect_left(A_select, tmp1)
    A[0:A_len] = tmp1
    A = np.sort(A)
    
    
print(sum(A))

Fixed submission:
N, M = map(int, input().split())
A = [int(i) for i in input().split()]

D = {}
for i in range(M):
    b,c = map(int, input().split())
    if c in D:
        D[c] += b
    else:
        D[c] = b
        
for a in A:
    if a in D:
        D[a] += 1
    else:
        D[a] = 1
        
D_sort = sorted(D.items(), reverse=True)
check = 0
ans = 0
for d in D_sort:
    check += d[1]
    if check <= N:
        ans += d[0] * d[1]
    else:
        ans += d[0] * (d[1] - (check - N))
        break
print(ans)