#include<cstdio>
#define syy myson
using namespace std;
int t,m,w[10010],v[10010],f[100010],i,j;
int max(int a,int b)
{
return a>b?a:b;
}
int Main()
{
freopen("medic.in","r",stdin);freopen("medic.out","w",stdout);
scanf("%d%d",&t,&m);
for(i=1;i<=m;i++)
scanf("%d%d",&w[i],&v[i]);
for(i=1;i<=m;i++)
for(j=t;j>=w[i];j--)
f[j]=max(f[j],f[j-w[i]]+v[i]);
printf("%d",f[t]);
return 0;
}
int main(){;}
int syy=Main();