Buggy python submission (verdict: Time Limit Exceeded):
import itertools
import math
import sys
import heapq
from collections import Counter
from collections import deque
from fractions import gcd
from functools import reduce
sys.setrecursionlimit(4100000)
from functools import lru_cache
INF = 1 << 60

#ここから書き始める
n, m = map(int, input().split())
a = list(map(int, input().split()))
x = [list(map(int, input().split())) for i in range(m)]
for b, c in x:
    if len(a) > n ** 2:
        break
    a.extend([c] * b)
a.sort(reverse=True)
# print(a)
print(sum(a[:n]))



Fixed submission:
n,m = map(int,input().split())
a = list(map(int,input().split()))
bc = [list(map(int,input().split())) for i in range(m)]
bc.sort(key=lambda x:x[1], reverse=True)
  
for b,c in bc:
  a.extend([c]*b)
  if len(a) > n*2:
    break
a.sort(reverse=True)
print(sum(a[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
import itertools
import math
import sys
import heapq
from collections import Counter
from collections import deque
from fractions import gcd
from functools import reduce
sys.setrecursionlimit(4100000)
from functools import lru_cache
INF = 1 << 60

#ここから書き始める
n, m = map(int, input().split())
a = list(map(int, input().split()))
x = [list(map(int, input().split())) for i in range(m)]
for b, c in x:
    a.extend([c] * b)
a.sort(reverse=True)
# print(a)
print(sum(a[:n]))



Fixed submission:
n,m = map(int,input().split())
a = list(map(int,input().split()))
bc = [list(map(int,input().split())) for i in range(m)]
bc.sort(key=lambda x:x[1], reverse=True)
  
for b,c in bc:
  a.extend([c]*b)
  if len(a) > n*2:
    break
a.sort(reverse=True)
print(sum(a[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
import itertools
import math
import sys
import heapq
from collections import Counter
from collections import deque
from fractions import gcd
from functools import reduce
sys.setrecursionlimit(4100000)
from functools import lru_cache
INF = 1 << 60

#ここから書き始める
n, m = map(int, input().split())
a = sorted(list(map(int, input().split())))
x = [(0, 0)] * m
for i in range(m):
    b, c = map(int, input().split())
    x[i] = (c, b)
x.sort(reverse=True)
for b, c in x:
    a.extend([b] * c)
a.sort(reverse=True)
# print(a)
print(sum(a[:n]))



Fixed submission:
n,m = map(int,input().split())
a = list(map(int,input().split()))
bc = [list(map(int,input().split())) for i in range(m)]
bc.sort(key=lambda x:x[1], reverse=True)
  
for b,c in bc:
  a.extend([c]*b)
  if len(a) > n*2:
    break
a.sort(reverse=True)
print(sum(a[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
import itertools
import math
import sys
import heapq
from collections import Counter
from collections import deque
from fractions import gcd
from sys import stdin
from functools import reduce
sys.setrecursionlimit(4100000)
from functools import lru_cache
INF = 1 << 60

#ここから書き始める
n, m = map(int, input().split())
a = sorted(list(map(int, stdin.readline().split())))
x = [(0, 0)] * m
for i in range(m):
    b, c = map(int, stdin.readline().split())
    x[i] = (c, b)
x.sort(reverse=True)
for i in range(m):
    z = [x[i][0]] * x[i][1]
    a.extend(z)
a.sort()
ans = 0
# print(a)
print(sum(a[len(a) - n:]))


Fixed submission:
n,m = map(int,input().split())
a = list(map(int,input().split()))
bc = [list(map(int,input().split())) for i in range(m)]
bc.sort(key=lambda x:x[1], reverse=True)
  
for b,c in bc:
  a.extend([c]*b)
  if len(a) > n*2:
    break
a.sort(reverse=True)
print(sum(a[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
import itertools
import math
import sys
import heapq
from collections import Counter
from collections import deque
from fractions import gcd
from sys import stdin
from functools import reduce
sys.setrecursionlimit(4100000)
from functools import lru_cache
INF = 1 << 60

#ここから書き始める
n, m = map(int, input().split())
a = sorted(list(map(int, stdin.readline().split())))
x = [()] * m
for i in range(m):
    b, c = map(int, stdin.readline().split())
    x[i] = (c, b)
x.sort(reverse=True)
y = []
for i in range(m):
    z = [x[i][0]] * x[i][1]
    y.extend(z)
a += y
a.sort()
ans = 0
# print(a)
print(sum(a[len(a) - n:]))


Fixed submission:
n,m = map(int,input().split())
a = list(map(int,input().split()))
bc = [list(map(int,input().split())) for i in range(m)]
bc.sort(key=lambda x:x[1], reverse=True)
  
for b,c in bc:
  a.extend([c]*b)
  if len(a) > n*2:
    break
a.sort(reverse=True)
print(sum(a[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
import itertools
import math
import sys
import heapq
from collections import Counter
from collections import deque
from fractions import gcd
from sys import stdin
from functools import reduce
sys.setrecursionlimit(4100000)
from functools import lru_cache
INF = 1 << 60

#ここから書き始める
n, m = map(int, input().split())
a = sorted(list(map(int, stdin.readline().split())))
x = [()] * m
for i in range(m):
    b, c = map(int, stdin.readline().split())
    x[i] = (c, b)
x.sort(reverse=True)
# print("x =", x)
# print("a =", a)
y = []
for i in range(m):
    z = [x[i][0]] * x[i][1]
    y.extend(z)
# print("y =", y)
ans = sum(a)
length = min(n, len(y))
for i in range(length):
    if a[i] >= y[i]:
        break
    ans += y[i] - a[i]
print(ans)


Fixed submission:
n,m = map(int,input().split())
a = list(map(int,input().split()))
bc = [list(map(int,input().split())) for i in range(m)]
bc.sort(key=lambda x:x[1], reverse=True)
  
for b,c in bc:
  a.extend([c]*b)
  if len(a) > n*2:
    break
a.sort(reverse=True)
print(sum(a[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
import itertools
import math
import sys
import heapq
from collections import Counter
from collections import deque
from fractions import gcd
from sys import stdin
from functools import reduce
sys.setrecursionlimit(4100000)
from functools import lru_cache
INF = 1 << 60

#ここから書き始める
n, m = map(int, input().split())
a = sorted(list(map(int, stdin.readline().split())))
x = [()] * m
for i in range(m):
    b, c = map(int, stdin.readline().split())
    x[i] = (c, b)
x.sort(reverse=True)
# print("x =", x)
# print("a =", a)
y = []
for i in range(m):
    z = [x[i][0]] * x[i][1]
    y.extend(z)
# print("y =", y)
ans = sum(a)
length = min(n, len(y))
for i in range(length):
    if a[i] >= y[i]:
        break
    ans += y[i] - a[i]
print(ans)


Fixed submission:
n,m = map(int,input().split())
a = list(map(int,input().split()))
bc = [list(map(int,input().split())) for i in range(m)]
bc.sort(key=lambda x:x[1], reverse=True)
  
for b,c in bc:
  a.extend([c]*b)
  if len(a) > n*2:
    break
a.sort(reverse=True)
print(sum(a[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
import itertools
import math
import sys
import heapq
from collections import Counter
from collections import deque
from fractions import gcd
from functools import reduce
sys.setrecursionlimit(4100000)
from functools import lru_cache
INF = 1 << 60

#ここから書き始める
n, m = map(int, input().split())
a = sorted(list(map(int, input().split())))
x = [[0, 0]] * m
for i in range(m):
    b, c = map(int, input().split())
    x[i] = (c, b)
x.sort(reverse=True)
# print("x =", x)
# print("a =", a)
y = []
for i in range(m):
    z = [x[i][0]] * x[i][1]
    y.extend(z)
# print("y =", y)
ans = sum(a)
length = min(n, len(y))
for i in range(length):
    if a[i] >= y[i]:
        break
    ans += y[i] - a[i]
print(ans)


Fixed submission:
n,m = map(int,input().split())
a = list(map(int,input().split()))
bc = [list(map(int,input().split())) for i in range(m)]
bc.sort(key=lambda x:x[1], reverse=True)
  
for b,c in bc:
  a.extend([c]*b)
  if len(a) > n*2:
    break
a.sort(reverse=True)
print(sum(a[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
import itertools
import math
import sys
import heapq
from collections import Counter
from collections import deque
from fractions import gcd
from functools import reduce
sys.setrecursionlimit(4100000)
from functools import lru_cache
INF = 1 << 60

#ここから書き始める
n, m = map(int, input().split())
a = sorted(list(map(int, input().split())))
x = [[0, 0]] * m
for i in range(m):
    b, c = map(int, input().split())
    x[i] = [c, b]
x.sort(reverse=True)
# print("x =", x)
# print("a =", a)
y = []
for i in range(m):
    z = [x[i][0]] * x[i][1]
    y.extend(z)
# print("y =", y)
ans = sum(a)
length = min(n, len(y))
for i in range(length):
    if a[i] >= y[i]:
        break
    ans += y[i] - a[i]
print(ans)


Fixed submission:
n,m = map(int,input().split())
a = list(map(int,input().split()))
bc = [list(map(int,input().split())) for i in range(m)]
bc.sort(key=lambda x:x[1], reverse=True)
  
for b,c in bc:
  a.extend([c]*b)
  if len(a) > n*2:
    break
a.sort(reverse=True)
print(sum(a[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
import itertools
import math
import sys
import heapq
from collections import Counter
from collections import deque
from fractions import gcd
from functools import reduce
sys.setrecursionlimit(4100000)
from functools import lru_cache
INF = 1 << 60

#ここから書き始める
n, m = map(int, input().split())
a = sorted(list(map(int, input().split())))
x = []
for i in range(m):
    b, c = map(int, input().split())
    x.append([c, b])
x.sort(reverse=True)
# print("x =", x)
# print("a =", a)
y = []
for i in range(m):
    z = [x[i][0]] * x[i][1]
    y += z
# print("y =", y)
ans = sum(a)
length = min(n, len(y))
for i in range(length):
    if a[i] >= y[i]:
        break
    ans += y[i] - a[i]
print(ans)


Fixed submission:
n,m = map(int,input().split())
a = list(map(int,input().split()))
bc = [list(map(int,input().split())) for i in range(m)]
bc.sort(key=lambda x:x[1], reverse=True)
  
for b,c in bc:
  a.extend([c]*b)
  if len(a) > n*2:
    break
a.sort(reverse=True)
print(sum(a[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
import itertools
import math
import sys
import heapq
from collections import Counter
from collections import deque
from fractions import gcd
from functools import reduce
sys.setrecursionlimit(4100000)
from functools import lru_cache
INF = 1 << 60

#ここから書き始める
n, m = map(int, input().split())
a = sorted(list(map(int, input().split())))
x = []
for i in range(m):
    b, c = map(int, input().split())
    x.append([c, b])
x.sort(reverse=True)
# print("x =", x)
# print("a =", a)
ans = sum(a)
for i in range(n):
    flag = True
    for j in range(m):
        if x[j][0] > a[i] and x[j][1] > 0:
            ans += x[j][0] - a[i]
            x[j][1] -= 1
            flag = False
            break
    if flag:
        break
print(ans)


Fixed submission:
n,m = map(int,input().split())
a = list(map(int,input().split()))
bc = [list(map(int,input().split())) for i in range(m)]
bc.sort(key=lambda x:x[1], reverse=True)
  
for b,c in bc:
  a.extend([c]*b)
  if len(a) > n*2:
    break
a.sort(reverse=True)
print(sum(a[:n]))