#include <stdio.h>
long long max (long long a, long long int b) {
return (a > b) ? a : b;
}
int main() {
int t, m;
scanf("%d%d", &t, &m);
long long b [101][1001] = {0};
int time[1001], value[101];
//注意这里从1开始
for(int i = 1; i < m; i++) {
int temp_time, val;
scanf("%d%d", &temp_time, &val);
time[i] = temp_time;
value[i] = val;
}
for(int i = 1; i <= m; i++) {
for(int j = 1; j <= t; j++) {
if(j < time[i]) {
b[i][j] = b [i - 1][j];
} else {
b[i][j] = max((b[i - 1][j - time[i]] + value[i]), b[i - 1][j]);
}
}
}
printf("%lld\n", b[m][t]);
return 0;
}