#include <bits/stdc++.h>
using namespace std;
struct aaa{
int a,b;
};
int main(){
int t,m;
scanf("%d%d",&t,&m);
aaa c[m+10]{};
for(int i=1;i<=m;i++){
scanf("%d%d",&c[i].a,&c[i].b);//a时间,b价值
}
int d[t+10][2]{};
int jjj=0;
int s=0;
for(int i=1;i<=t;i++){
for(int j=1;j<=m;j++){
if(c[j].a>i){
if(jjj==1){
d[i][1]=max(max(d[i-1][1],d[i][0]),d[i][1]);
}
else{
d[i][0]=max(max(d[i-1][0],d[i][1]),d[i][0]);
}
//d[i][j]=max(max(d[i-1][j],d[i][j-1]),d[i][j]);
}
else{
if(jjj==1){
d[i][1]=max(d[i-c[j].a][1]+c[j].b,max(max(d[i-1][1],d[i][0]),d[i][1]));
}
else{
d[i][0]=max(d[i-c[j].a][0]+c[j].b,max(max(d[i-1][0],d[i][1]),d[i][0]));
}
//d[i][j]=max(d[i-c[j].a][j]+c[j].b,max(max(d[i-1][j],d[i][j-1]),d[i][j]));
}
if(jjj==1){
s=max(s,d[i][1]);
jjj=0;
}
else{
s=max(s,d[i][0]);
jjj=1;
}
//s=max(s,d[i][j]);
}
}
printf("%d",s);
return 0;
}