1 #include <bits/stdc++.h>
2 #define _for(i,a,b) for(int i = (a);i < b;i ++)
3 typedef long long ll;
4 using namespace std;
5 int N,m;
6 inline ll read()
7 {
8 ll ans = 0;
9 char ch = getchar(), last = ' ';
10 while(!isdigit(ch)) last = ch, ch = getchar();
11 while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
12 if(last == '-') ans = -ans;
13 return ans;
14 }
15 inline void write(ll x)
16 {
17 if(x < 0) x = -x, putchar('-');
18 if(x >= 10) write(x / 10);
19 putchar(x % 10 + '0');
20 }
21 int dp[26][30009];
22 int a[2][30];
23 int main()
24 {
25 N = read(),m = read();
26 _for(i,1,m+1)
27 _for(j,0,2)
28 a[j][i] = read();
29 memset(dp,0,sizeof(dp));
30 _for(i,1,m+1)
31 {
32 _for(j,0,N+1)
33 {
34 if(a[0][i] <= j)
35 dp[i][j] = max(dp[i-1][j],dp[i-1][j-a[0][i]]+a[0][i]*a[1][i]);
36 else
37 dp[i][j] = dp[i-1][j];
38 }
39 }
40 write(dp[m][N]);
41 return 0;
42 }