#include<bits/stdc++.h>
using namespace std;
const int maxn=5e6+5;
int t,m;
struct node{
int value;
int timee;
}a[maxn];
int dp[maxn];
int maxx,bian;
bool cmp(node a,node b){
return a.timee<b.timee;
}
int main(){
cin>>t>>m;
for(int i=1;i<=m;i++){
int helpt,helpv; cin>>helpt>>helpv;
if(helpt<=t){
++bian;
a[bian].timee=helpt;
a[bian].value=helpv;
}
}
sort(a+1,a+1+bian,cmp);
for(int i=bian;i>=1;i--){
int j=1;
while(t-a[i].timee<=a[j].timee){
dp[i]=max(dp[i],dp[j]+a[j].value);
j++;
}
maxx=max(maxx,dp[i]);
}
cout<<maxx<<endl;
return 0;
}