#include <bits/stdc++.h>
using namespace std;
int f[10005][10005];
struct n
{
int w;
int c;
}a[100005];
int main()
{
int time,count;
cin>>time>>count;
for(int i=1;i<=count;i++)
{
cin>>a[i].w>>a[i].c;
}
for(int i=1;i<=count;i++)
{
for(int j=time;j>0;j--)
{
if(a[i].w<=time)
{
f[i][j]=max(f[i-1][j],f[i-1][j-a[i].w]+a[i].c);
}
else
{
f[i][j]=f[i-1][j];
}
}
}
cout<<f[count][time];
return 0;
}