#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
int n,m;
int w[999],c[999];
int f[9999];
int main()
{
freopen("knapsack.in","r",stdin);
freopen("knapsack.out","w",stdout);
scanf("%d%d",&m,&n);
for(int i=1;i<=n;i++)
{
scanf("%d%d",&w[i],&c[i]);
}
for(int i=1;i<=n;i++)
for(int j=w[i];j<=m;j++){
f[j]=max(f[j],f[j-w[i]]+c[i]);
}
printf("max=%d",f[m]);
fclose(stdin);
fclose(stdout);
return 0;
}