#include<bits/stdc++.h> using namespace std; int timer[101],value[101],dp[10001][10001]={0}; int main(){ int m,n,num; freopen("medic.in","r",stdin); freopen("medic.out","w",stdout); cin>>m>>n; for(int i=1;i<=n;i++){ cin>>timer[i]>>value[i]; } for(int i=1;i<=n;i++){ for(int j=1;j<=m;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[n][m]<<endl; }