#include<bits/stdc++.h>
#define N 10000000
#define LL long long
using namespace std;
LL t,m,A[N],V[N];
LL f[N];
int main()
{
cin>>t>>m;
for(int i=1;i<=m;i++)
{
cin>>A[i]>>V[i];
}
for(int j=1;j<=m;j++)
{
for(int k=1;k*A[j]<=t;k++)
{
f[k]=max(f[k],f[k-A[j]]+V[j]);
}
}
cout<<f[t]<<endl;
return 0;
}