#include<iostream>
using namespace std;
const int N = 30, M = 30010;
int f[N][M], w[N], v[N];
int main()
{
int n, m;
cin >> m >> n;
for(int i = 1;i <= n; i++) cin >> v[i] >> w[i];
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
f[i][j] = f[i - 1][j];
if(v[i] <= j) f[i][j] = max(f[i][j], f[i - 1][j - v[i]] + w[i] * v[i]);
}
}
cout << f[n][m] << endl;
return 0;
}