제출 #395100

#제출 시각아이디문제언어결과실행 시간메모리
395100rocks03Knapsack (NOI18_knapsack)C++14
73 / 100
1084 ms3960 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];

void solve(){
    cin >> S >> N;
    rep(i, 0, N){
        cin >> v[i] >> w[i] >> k[i];
    }
    rep(i, 0, N){
        for(int x = 0; (1 << x) <= k[i] && (1 << x) * w[i] <= S; x++){
            k[i] -= (1 << x);
            ll we = (1 << x) * w[i];
            per(s, S, we){
                dp[s] = max(dp[s], dp[s - we] + (1 << x) * v[i]);
            }
        }
        ll we = k[i] * w[i];
        per(s, S, we){
            dp[s] = max(dp[s], dp[s - we] + k[i] * v[i]);
        }
    }
    cout << *max_element(dp, dp + S + 1);
}

int main(){
    ios_base::sync_with_stdio(false), cin.tie(nullptr);
    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...