제출 #1308277

#제출 시각아이디문제언어결과실행 시간메모리
1308277ishat_jhaKnapsack (NOI18_knapsack)C++17
73 / 100
376 ms327680 KiB
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif

#define int int64_t

int32_t main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int nn, w;
    cin >> w >> nn;
    vector<pair<int, pair<int, int>>> vv(nn + 1);
    int n = 0;
    for(int i = 1; i <= nn; i++) {
        cin >> vv[i].first >> vv[i].second.first >> vv[i].second.second;
        vv[i].second.second = min(vv[i].second.second, w / vv[i].second.first);
        n += vv[i].second.second;
    }
    if(nn == 1) {
        cout << min(vv[1].second.second, w / vv[1].second.first) * vv[1].first;
        return 0;
    }
    vector<pair<int, int>> v;
    for(int i = 1; i <= nn; i++) {
        for(int j = 1; j <= vv[i].second.second; j++) {
            v.push_back(make_pair(vv[i].second.first, vv[i].first));
        }
    }
    vector<vector<int>> dp(2, vector<int> (w + 1, 0LL));
    vector<int> dummy(w + 1, 0LL);
    for(int i = 0; i < n; i++) {
        for(int j = 0; j <= w; j++) {
            dp[1][j] = max(dp[0][j], dp[1][j]);
            if(j + v[i].first <= w) {
                dp[1][j + v[i].first] = max(dp[1][j + v[i].first], dp[0][j] + v[i].second);
            }
        }
        swap(dp[0], dp[1]);
        dp[1] = dummy;
    }
    cout << *max_element(dp[0].begin(), dp[0].end()) << endl;

    return 0;
}
#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...