Submission #1328946

#TimeUsernameProblemLanguageResultExecution timeMemory
1328946melissakKnapsack (NOI18_knapsack)C++20
100 / 100
37 ms1692 KiB
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <unordered_set>
#include <unordered_map>
#include <set>
#include <algorithm>
#include <cctype>
#include <limits>
#include <iomanip>
#include <stack>
#include <numeric>
#include <map>
#include <queue>
#include <cfloat>
#include <climits>

using namespace std;
using ll = long long;
using ull = unsigned long long;
const ll MOD = 1e9 + 7;
const ll MAX_SAFE_LL = 1e18;
const ll MAX_SAFE = 1e9;
const ll NEG_MAX_SAFE = -(1LL << 60);

ll findGcd(ll a, ll b) {
    if(b > a) swap(a, b);
    if(!b) return a;
    return findGcd(b, a % b);
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int s, n;
    cin >> s >> n;
    vector<vector<pair<int, int>>> items(s + 1);
    vector<ll> dp(s + 1);
    for(int i = 0; i < n; ++i) {
        int v, w, k;
        cin >> v >> w >> k;
        items[w].push_back(make_pair(v, min(k, s / w)));
    }
    for(int i = 1; i <= s; ++i) {
        if(items[i].empty()) continue;
        sort(items[i].begin(), items[i].end(), greater<pair<int, int>>());
        int mw = s;
        for(auto& p : items[i]) {
            while(p.second && mw >= i) {
                for(int j = s; j >= i; --j) {
                    dp[j] = max(dp[j], dp[j - i] + p.first);
                }
                p.second--;
                mw -= i;
            }
            if(mw < i) break;
        }
    }
    cout << dp[s] << '\n';
    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...