#include<bits/stdc++.h> using namespace std; int main(){ int T, M; while(cin >> T >> M){ int dp[T+1]; int time[M+1], value[M+1]; for(int i = 1; i <= M; i++)cin >> time[i] >> value[i]; memset(dp, 0, sizeof(dp)); for(int i = 1; i <= M; i++){ for(int j = T; j >=1; j--){ if(j >= time[i]) dp[j] = max(dp[j-time[i]]+value[i], dp[j]); } } cout << dp[T] << endl; } }