#include <iostream>
using namespace std;
const int N = 105;
const int T = 1010;
int t, n, v[N], w[N], f[N][T];
int main() {
cin >> t >> n;
for (int i = 1 ; i <= n ; i ++) {
cin >> v[i] >> w[i];
}
for (int i = 1 ; i <= n ; i ++) {
for (int j = 1; j <= t ; j ++) {
f[i][j] = f[i - 1][j];
if (j >= v[i]) f[i][j] = max(f[i][j], f[i - 1][j - v[i]] + w[i]);
}
}
cout << f[n][t] << endl;
return 0;
}