#include <iostream> #include <cstring> using namespace std; const int M = 1010; const int N = 110; int dp[N][M]; int c[N]; int v[N]; int main() { int n, m; while (cin >> m >> n) { memset(dp, 0, sizeof(dp)); for (int i = 1; i <= n; i++) { cin >> c[i] >> v[i]; } for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ dp[i][j]=dp[i-1][j]; if(j>=c[i]){ dp[i][j]=max(dp[i][j],dp[i-1][j-c[i]]+v[i]); } } } cout<<dp[n][m]<<endl; } return 0; }