#include<bits/stdc++.h>
using namespace std;
int w[1001];
int v[1001];
int dp[101][1001];
int main()
{
int W, N;
cin >> W >> N;
for (int i = 1; i <= N; i++) {
cin >> w[i] >> v[i];
}
for (int i = 0; i <= N; i++) {
dp[i][0] = 0;
}
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= W; j++) {
if (j < w[i]) {
dp[i][j] = dp[i - 1][j];
} else {
dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]);
}
}
}
cout << dp[N][W];
return 0;
}