#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn = 2e3 + 7;
int f[maxn][maxn], dp[maxn];
vector<pair<int, int>> item[maxn];
void solve(){
    int s, n; cin >> s >> n;
    for(int i = 1; i <= n; i++){
        int v, w, k; cin >> v >> w >> k;
        item[w].push_back({v, k});
    }
    
    for(int i = 1; i <= s; i++){
        sort(item[i].begin(), item[i].end());
        int totw = 0, j = 0, sz = item[i].size();
        while(totw <= s && j < sz){
            auto [x, k] = item[i][j];
            j++;
            while(totw <= s && k > 0){
                totw += i;
                k--;
                int cur = totw / i;
                f[i][cur] = f[i][cur - 1] + x;
            }
        }
    }
    for(int w = 1; w <= 2000; w++){
        for(int i = s; i >= 1; i--){
            for(int j = 1; i >= w * j; j++){
                dp[i] = max(dp[i - w * j] + f[w][j], dp[i]);
            }
        }
   }
    cout << *max_element(dp, dp+s+1);
}
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    solve();
}
| # | 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... |