제출 #976177

#제출 시각아이디문제언어결과실행 시간메모리
976177IrateKnapsack (NOI18_knapsack)C++17
73 / 100
158 ms262144 KiB
#include <bits/stdc++.h>
using namespace std;
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int S, n;
    cin >> S >> n;
    vector<pair<long long, int>> v = {{0, 0}};
    for(int i = 1;i <= n;++i){
        int val, w, k;
        cin >> val >> w >> k;
        k = min(k, S / w);
        for(int j = 0;(1 << j) <= k;++j){
            v.push_back({(1LL << j) * val, (1 << j) * w});
            k -= (1 << j);
        }
        if(k)v.push_back({1LL * k * val, k * w});
    }
    n = (int)v.size() - 1;
    vector<vector<long long>> dp(n + 1, vector<long long>(S + 1, -1e18));
    dp[0][0] = 0;
    for(int i = 1;i <= n;++i){
        for(int j = 0;j <= S;++j){
            dp[i][j] = dp[i - 1][j];
            if(j - v[i].second >= 0)dp[i][j] = max(dp[i][j], dp[i - 1][j - v[i].second] + v[i].first);
        }
    }
    long long mx = 0;
    for(int i = 0;i <= S;++i){
        mx = max(mx, dp[n][i]);
    }
    cout << mx << '\n';
}
#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...