#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int maxn = 1010;
int dp[maxn], v[maxn], p[maxn];//v[i]:分数;p[i]:菜价

int main(){
    int c, n;
    while(~scanf("%d %d", &c, &n)){
        for(int i = 0; i < n; i++){
            scanf("%d %d", &p[i], &v[i]);//菜价,评价分数
        }
        memset(dp, 0, sizeof(dp));
        for(int i = 0; i < n; i++){
            for(int j = c; j >= p[i]; j--){
                dp[j] = max(dp[j], dp[j - p[i]] + v[i]);
            }
        }
        printf("%d\n", dp[c]);
    }
    return 0;
}