import itertools
import copy

k,n = [int(i) for i in input().split(' ')]
c = [int(i) for i in input().split(' ')]

matrix = [[0 for i in range(k)] for j in range(n + 4)]

for i in range(k):
  for j in range(0, c[i]):
    matrix[j][i] = 1

#for i in range(len(matrix)):
  #for j in range(len(matrix[i])):
    #print(matrix[i][j], end="")
  #print("")

teams = []
for i in range(n):
  size, cost = [int(i) for i in input().split(' ')]
  teams += [(size, cost)]

res = 0
  
for p in itertools.permutations(teams):
  cur = list(p)
  row = 0
  col = 0
  idx = 0
  ok = True
  totalProfit = 0
  for liska in p:
    while row < n + 4 and matrix[row][col] == 0:
      col += 1
      if col == k:
        col = 0
        row += 1
    if row == n + 4:
      break

    totalProfit += liska[1]

    for i in range(liska[0]):
      if matrix[row][col] == 0:
        ok = False
        break
      col += 1
      if col == k:
        col = 0
        row += 1

    while row < n + 4 and matrix[row][col] == 0:
      col += 1
      if col == k:
        col = 0
        row += 1
    if row == n + 4:
      break



  if ok and (row == n + 4 or matrix[row][col] == 0):
    res = max(res, totalProfit)

print(res)


