#include<bits/stdc++.h> using namespace std; int timer[101],value[101],dp[10001][10001]={0}; int T,M; int num=0; int main(){ freopen("medic.in","r",stdin); freopen("medic.out","w",stdout); cin>>T>>M; for(int i=1;i<=M;i++){ cin>>timer[i]>>value[i]; num+=timer[i]; } if(num<T) T=num; for(int i=1;i<=M;i++){ for(int j=1;j<=T;j++){ if(timer[i]<=j) dp[i][j]=max(dp[i-1][j],dp[i-1][j-timer[i]]+value[i]); else dp[i][j]=dp[i-1][j]; } } cout<<dp[M][T]<<endl; }