#include<bits/stdc++.h>
using namespace std;
int t,m;
struct med{
int t,v;
};
med c[10000];
bool cmp(med x,med y){
if(x.t!=y.t) return x.t<y.t;
else if(x.t==y.t) return x.v>y.v;
}
int sum;
int main(){
cin>>t>>m;
for(int i=1;i<=m;i++){
cin>>c[i].t>>c[i].v;
}
sort(c+1,c+m+1,cmp);
for(int i=1;i<=m;i++){
if(c[i].t<t){
t-=c[i].t;
sum+=c[i].v;
}else if(c[i].t==t){
t=0;
sum+=c[i].v;
}
}
cout<<sum<<endl;
return 0;
}