#include <bits/stdc++.h>
#define ll long long
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define ROF(i,a,b) for(int i=a;i>=b;i--)
#define pi pair<int,int>
#define pii pair<int,pi>
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(), x.end()
#define sz(a) (int) a.size()
#define endl '\n'
#define data "Knapsack"
using namespace std;
const ll linf = 1e18;
const int inf = 1e9;
const int MOD = 1e9 + 7, N = 1e5;
void add(int &a, int b)
{
    a += b;
    if(a>=MOD)
        a-=MOD;
    if(a<0)
        a += MOD;
}
int modulo(int x)
{
    if(x<=1)
        return 1;
    return (MOD - MOD/x) * modulo(MOD/x) % MOD;
}
int mul(int a, int b)
{
    return (1ll *a%MOD * b%MOD) % MOD;
}
int S, n;
map< int, vector<pi> > M;
ll dp[2][2003];
void inp(void)
{
    cin >> S >> n;
    FOR(i, 1, n)
    {
        int v, w, k;
        cin >> v >> w >> k;
        if(w <= S && k)
            M[w].pb(make_pair(v, k));
    }
}
void solve(void)
{
    memset(dp, -0x3f, sizeof(dp));
    dp[0][0] = 0;
    int i = 1;
    for(map<int, vector<pi> > ::iterator it = M.begin(); it != M.end(); ++it)
    {
        vector<pi> &V = it -> se;
        int w = it -> fi;
        sort(all(V), greater<pi>());
        int cur = (i & 1);
        int pre = !cur;
        FOR(j, 0, S)
        {
            dp[cur][j] = dp[pre][j];
            int used = 0;
            int W = 0;
            int p = 0;
            ll cost = 0;
            while(W + w <= j && p < sz(V))
            {
                W += w;
                cost += V[p].fi;
                ++used;
                dp[cur][j] = max(dp[cur][j], dp[pre][j - W] + cost);
                if(used == V[p].se)
                {
                    used = 0;
                    ++p;
                }
            }
        }
        ++i;
    }
    cout << *max_element(dp[!(i&1)], dp[!(i&1)]+S+1);
}
signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    if(fopen(data".inp", "r"))
    {
        freopen(data".inp","r",stdin);
        freopen(data".out","w",stdout);
    }
    inp();
    solve();
    return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:105:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  105 |         freopen(data".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:106:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  106 |         freopen(data".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |