Submission #1350574

#TimeUsernameProblemLanguageResultExecution timeMemory
1350574vjudge1Knapsack (NOI18_knapsack)C++17
100 / 100
842 ms16868 KiB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;

#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define f first
#define s second

#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define per(i, a, b) for (int i = (b) - 1; i >= (a); --i)

void fast_io() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
}

#ifdef LOCAL
#define debug(x) cerr << #x << " = " << (x) << endl;
#else
#define debug(x)
#endif

const int INF = 1e9 + 7;
const ll LINF = 1e18;

vector<pll> use;
ll w,n;
ll dp[2010];

void solve() {
    cin>>w>>n;
    rep(i,0,n){
        ll val,wei,k;
        cin>>val>>wei>>k;
        for(ll j=1;j<=k;j*=2){
            if(wei*j>w)break;
            use.pb({val*j,wei*j});
            k-=j;
        }
        if(k>0){
            if(wei*k>w)continue;
            use.pb({val*k,wei*k});
        }
    }
    for(auto [val,wei]:use){
        per(j,0,w+1){
            if(j-wei<0)continue;
            dp[j]=max(dp[j],dp[j-wei]+val);
        }
    }
    cout<<dp[w];
}

int main() {
    fast_io();
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
    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...