Submission #1170227

#TimeUsernameProblemLanguageResultExecution timeMemory
1170227nguyenkhangninh99Knapsack (NOI18_knapsack)C++20
100 / 100
35 ms10828 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long

const int maxn = 2e3 + 7;

int f[maxn][maxn], dp[maxn];
vector<pair<int, int>> item[maxn];

void solve(){
    int s, n; cin >> s >> n;
    for(int i = 1; i <= n; i++){
        int v, w, k; cin >> v >> w >> k;
        item[w].push_back({v, k});
    }
    
    for(int i = 1; i <= s; i++){
        sort(item[i].begin(), item[i].end(), greater<>());

        int totw = 0, j = 0, sz = item[i].size();
        while(totw <= s && j < sz){
            auto [v, k] = item[i][j];
            j++;
            while(totw <= s && k > 0){
                totw += i;
                k--;
                f[i][totw / i] = f[i][(totw / i) - 1] + v;
            }
        }
    }
    for(int w = 1; w <= 2000; w++){
        for(int i = s; i >= 1; i--){
            for(int j = 1; i >= w * j; j++) dp[i] = max(dp[i - w * j] + f[w][j], dp[i]);
        }
   }

    cout << *max_element(dp, dp+s+1);
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    solve();
}
#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...