Submission #1284279

#TimeUsernameProblemLanguageResultExecution timeMemory
1284279tntKnapsack (NOI18_knapsack)C++20
73 / 100
1094 ms720 KiB
#include <bits/stdc++.h>
using namespace std;

#define pb push_back                    
#define ll long long 
#define s second
#define pb push_back
#define f first
#define s second
const long long inf = 2e9 + 7;
const int N = 4e5 + 101;

void solve(){     
    int n,s;
    cin >> s >> n;
    if(n == 1){
        int w,v,k;
        cin >> v >> w >> k;
        int ans = 0,sum = 0;
        while(true){
            if(sum + w > s || w > s || k == 0) break;
            sum += w,ans += v,k--;
        }
        cout << ans;
        return;
    }
    vector <int> dp(s + 10,-inf);
    dp[0] = 0;
    for(int i = 1; i <= n; i++){
        int w,v,k;
        cin >> v >> w >> k;
        k = min(k,s / w);
        int p = 1,cnt = 0;
        while(k > 0){
            int d;
            if(k < p) d = k;
            else d = p;
            int w1 = w * d,v1 = v * d;
            for(int j = s; j >= w1; j--){
                if(dp[j] < dp[j - w1] + v1)dp[j] = dp[j - w1] + v1;
            }
            k -= d;
            p *= 2;
        }
    }
    int mx = 0;
    for(int i = 0; i <= s; i++) mx = max(mx,dp[i]);
    cout  << mx;
}
signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    //freopen("promote.in", "r", stdin);
    //freopen("promote.out", "w", stdout);
    int t = 1;
    while(t--){
     	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...