#include<cstdio>
#include<algorithm>
using namespace std;
int a[110],b[1010],c[110];
int main()
{
freopen("medic.in","r",stdin);
freopen("medic.out","w",stdout);
int t,m,ans={0};
scanf("%d%d",&t,&m);
for (int i=0;i!=m;++i)
scanf("%d%d",&c[i],&a[i]);
for (int i=0;i!=m;++i)
for (int j=t;j>=0;--j)
{
if (c[i]<=j)
b[j]=max(b[j],b[j-c[i]]+a[i]);
}
for (int i=0;i<=t;++i)
ans=max(ans,b[i]);
printf("%d\n",ans);
return 0;
}