#include<iostream>
#include<cstdio>
#include<stdio.h>
#include<queue>
using namespace std;
const int maxn={9000};
int tim[maxn]={0};
int w[maxn]={0};
int ans[maxn]={0};
int main(){
freopen("baby.in","r",stdin);
freopen("baby.out","w",stdout);
int t,n;
scanf("%d%d",&t,&n);
for(int i=1;i<=n;++i)
cin>>tim[i]>>w[i];
for(int i=1;i<=n;++i)
for(int j=t;j>=tim[i];j--)
ans[j]=max(ans[j],ans[j-tim[i]]+w[i]);
cout<<ans[t];
return 0;
}