#include<bits/stdc++.h>
using namespace std;
int n,maxw,dp[105][20005],w[105],q[105]; 
int main(){
    cin>>maxw>>n;
    for(int i=1;i<=n;i++) cin>>w[i]>>q[i];
    for(int i=1;i<=n;i++){
        for(int j=1;j<=maxw;j++){
            if(j>=w[i]) dp[i][j]=max(dp[i-1][j],q[i]+dp[i-1][j-w[i]]);
            else dp[i][j]=dp[i-1][j];
        }
    }
    cout<<dp[n][maxw];
    return 0;
}
/**************************************************************
	Problem: 1282
	User: lijinkai
	Language: C++
	Result: Accepted
	Time:60 ms
	Memory:10280 kb
****************************************************************/