Submission #876252

#TimeUsernameProblemLanguageResultExecution timeMemory
876252__Davit__Knapsack (NOI18_knapsack)C++17
73 / 100
1053 ms4484 KiB
#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define vr(v) v.begin(),v.end()
#define rv(v) v.rbegin(),v.rend()
#define Code ios_base::sync_with_stdio(false);
#define By cin.tie(NULL);
#define Davit cout.tie(NULL);

using namespace std;
//#include "algo/debug.h"


int main() {
    int S, n;
    cin >> S >> n;
    vector<int> V(n), W(n), K(n);
    map<int, vector<pair<int, int>>> by_weight;
    for (int i = 0; i < n; i++) {
        cin >> V[i] >> W[i] >> K[i];
        K[i] = min(K[i], S / W[i]);
        by_weight[W[i]].pb({V[i], K[i]});
    }
    vector<int> dp(S + 5, 0);
    for (auto x: by_weight) {
        for (auto y: x.ss) {
            for (int cnt = 0; cnt < y.ss; cnt++) {
                for (int k = S; k >= 0; k--) {
                    if (k - x.ff >= 0)dp[k] = max(dp[k], dp[k - x.ff] + y.ff);
                }
            }
        }
    }
    int answer = 0;
    for (int k = S; k >= 0; k--) {
        answer = max(answer, dp[k]);
    }
    cout << answer << endl;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...