#include<bits/stdc++.h>
using namespace std;
int w[100000],f[100000],v[100000];
int main(){
int t,m;
cin>>t>>m;
for(int i=1;i<=m;i++){
cin>>w[i]>>v[i];
}
for(int i=1;i<=m;i++){
for(int j=t;j>=w[i];j--){
if(j>=w[i])f[j]=max(f[j],f[j-w[i]]+v[i]);
}
}
cout<<f[t];
return 0;
}