#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
    int s, n;
    cin >> s >> n;
    vector<vector<pair<int, int>>> perW(s + 1);
    for (int i = 0; i < n; ++i)
    {
        int v, w, k;
        cin >> v >> w >> k;
        perW[w].emplace_back(v, k);
    }
    vector<pair<int, int>> items{};
    for (int i = 1; i <= s; ++i)
    {
        sort(perW[i].begin(), perW[i].end(), greater<pair<int, int>>{});
        int wPerType = 0;
        for (pair<int, int> item : perW[i])
        {
            for (int j = 0; j < item.second; ++j)
            {
                wPerType += i;
                if (wPerType > s)
                {
                    break;
                }
                items.emplace_back(i, item.first);
            }
            if (wPerType > s)
            {
                break;
            }
        }
    }
    vector<int> dp(s + 1);
    for (int i = 0; i < items.size(); ++i)
    {
        for (int j = s; j >= items[i].first; --j)
        {
            dp[j] = max(dp[j], dp[j - items[i].first] + items[i].second);
        }
    }
    cout << dp[s] << '\n';
    return 0;
}
| # | 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... |