#include <iostream>

using namespace std;

const int MAXN = 101;
const int MAXP = 1001;

// dp数组在主函数外定义,元素自动初始化0
int dp[MAXP], v[MAXN], w[MAXN];

int main(){
    int c,n;
    while (cin>>c>>n){

        for (int i = 1; i <= n; ++i) {
            cin>>w[i]>>v[i];
        }

        // 一维遍历
        for (int i = 1; i <= n; ++i) {
            for (int j = c; j >= w[i]; --j) {
                dp[j] = max(dp[j], dp[j-w[i]]+v[i]);
            }
        }

        cout<<dp[c]<<endl;
    }
    return 0;
}