#include <iostream>
using namespace std;

int f[110][1010];
int v[110], w[110];
int main() {
    int t, m;
    while (cin >> t >> m) {
        for (int i = 1; i <= m; i++) cin >> v[i] >> w[i];

        for (int i = 1; i <= m; i++) {
            for (int j = 0; 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[m][t] << endl;
    }
    return 0;
}