t, n = map(int, input().split(" "))#01背包
time = [0 for i in range(n)]
v = [0 for i in range(n)]
dp = [0 for i in range(t + 1)]
for i in range(n):
    time[i], v[i] = map(int, input().split(" "))
for i in range(n):
    for j in range(t, time[i] - 1, -1):
        dp[j] = max(dp[j], dp[j - time[i]] + v[i])
print(dp.pop())