Submission #395102

#TimeUsernameProblemLanguageResultExecution timeMemory
395102rocks03Knapsack (NOI18_knapsack)C++14
100 / 100
454 ms7440 KiB
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ff first
#define ss second
#define pb push_back
#define SZ(x) ((int)(x).size())
#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << ": " << x << " "
#define nl cout << "\n"
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define per(i, a, b) for(int i = (a); i >= (b); i--)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int MAXN = 1e5+100;
int S, N;
ll v[MAXN], w[MAXN], k[MAXN], dp[MAXN];

const int MAXS = 2e3+100;
vector<pll> g[MAXS]; 

void solve(){
    cin >> S >> N;
    rep(i, 0, N){
        cin >> v[i] >> w[i] >> k[i];
        g[w[i]].pb({v[i], k[i]});
    }
    rep(w, 0, S + 1){
        sort(all(g[w]), greater<pll>());
        ll cnt = S;
        for(auto& [v, k] : g[w]){
            k = min(k, cnt);
            cnt -= k;
            for(int x = 0; (1 << x) <= k && (1 << x) * w <= S; x++){
                k -= (1 << x);
                ll we = (1 << x) * w;
                per(s, S, we){
                    dp[s] = max(dp[s], dp[s - we] + (1 << x) * v);
                }
            }
            ll we = k * w;
            per(s, S, we){
                dp[s] = max(dp[s], dp[s - we] + k * v);
            }
            if(cnt < 0) break;
        }
    }
    cout << *max_element(dp, dp + S + 1);
}

int main(){
    ios_base::sync_with_stdio(false), cin.tie(nullptr);
    solve();
    return 0;
}

Compilation message (stderr)

knapsack.cpp: In function 'void solve()':
knapsack.cpp:33:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   33 |         for(auto& [v, k] : g[w]){
      |                   ^
#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...