#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <unordered_set>
#include <unordered_map>
#include <set>
#include <algorithm>
#include <cctype>
#include <limits>
#include <iomanip>
#include <stack>
#include <numeric>
#include <map>
#include <queue>
#include <cfloat>
#include <climits>
using namespace std;
using ll = long long;
using ull = unsigned long long;
const ll MOD = 1e9 + 7;
const ll MAX_SAFE_LL = 1e18;
const ll MAX_SAFE = 1e9;
const ll NEG_MAX_SAFE = -(1LL << 60);
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int s, n;
cin >> s >> n;
vector<vector<pair<int, int>>> items(s + 1);
vector<ll> dp(s + 1);
for(int i = 0; i < n; ++i) {
int v, w, k;
cin >> v >> w >> k;
items[w].push_back({v, min(k, s / w)});
}
for(int i = 1; i <= s; ++i) {
sort(items[i].begin(), items[i].end(), greater());
int mv = s;
for(auto& p : items[i]) {
int v = p.first, k = p.second;
while(k && mv >= i) {
for(int j = s; j >= i; --j) {
dp[j] = max(dp[j], dp[j - i] + v);
}
mv -= i;
--k;
}
if(mv < i) break;
}
}
cout << dp[s] << '\n';
return 0;
}