Submission #1355881

#TimeUsernameProblemLanguageResultExecution timeMemory
1355881Zone_zoneeKnapsack (NOI18_knapsack)C++20
73 / 100
1096 ms2448 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int S = 2010;

vector<pair<ll, ll>> a[S];
ll dp[S];
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int s, n;
    cin >> s >> n;
    for(int i = 1; i <= n; ++i){
        ll v, w, k;
        cin >> v >> w >> k;
        a[w].push_back({v, k});
    }
    for(int i = 1; i <= s; ++i){
        sort(a[i].begin(), a[i].end());
        reverse(a[i].begin(), a[i].end());
        for(auto [v, k] : a[i]){
            int cur = i;
            for(int x = 1; x <= k && cur <= s; ++x, cur += i){
                for(int sz = s; sz >= i; sz--){
                    dp[sz] = max(dp[sz], dp[sz-i] + v);
                }
            }
            if(cur >= s) break;
        }
    }
    cout << dp[s] << '\n';
}
/*
 3
100 1 3
50 1 4
*/
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...