import math
import statistics
T = []
W = []
T.append(0)
W.append(0)
def dp(m,t):
if m==0 or t==0:
temp = 0
elif t<T[m]:
return dp(m-1,t)
else:
temp1 = dp(m-1,t)
temp2 = W[m]+dp(m-1,t-T[m])
temp = max(temp1,temp2)
return temp
t,m = list(map(int,input().split()))
for i in range(m):
x = list(map(int,input().split()))
T.append(x[0])
W.append(x[1])
print(dp(m,t))