#include <iostream>

using namespace std;

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

// dp数组在主函数外定义,元素自动初始化0
int dp[MAXN][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 = 1; j <= c; ++j) {
                if (j>=w[i]){
                    dp[i][j] = max(dp[i-1][j], dp[i-1][j-w[i]]+v[i]);
                } else{
                    dp[i][j] = dp[i-1][j];
                }
            }
        }

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