#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mod = 1e9 + 7; // 998244353
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int S, N;
cin >> S >> N;
vector<int> V(N), W(N), K(N);
for (int i = 0; i < N; i++)
{
cin >> V[i] >> W[i] >> K[i];
}
vector<int> dp(S + 1);
dp[0] = 0;
for (int i = 0; i < N; i++)
{
for (int k = 0; k < K[i]; k++)
{
for (int j = S; j >= W[i]; j--)
{
dp[j] = max(dp[j], dp[j - W[i]] + V[i]);
}
}
}
cout << dp[S] << endl;
return 0;
}