제출 #745282

#제출 시각아이디문제언어결과실행 시간메모리
745282Azm1tKnapsack (NOI18_knapsack)C++17
73 / 100
1073 ms22444 KiB
#include "bits/stdc++.h"
using namespace std;
#define forn for (int i = 0; i < n; i++)
#define int long long
#define ed "\n"
#define cyes cout << "YES\n"
#define cno cout << "NO\n"
#define pb push_back
#define deb cout << "Hi\n"
#define pint pair<int, int>
const long long MOD = 1000000007;


void dbg_out(){cerr<<"\n";}
template<typename Head,typename... Tail>
void dbg_out(Head H,Tail... T){cerr<<' '<<H;dbg_out(T...);}
#define debug(...) cerr<<"("<<#__VA_ARGS__<<"):",dbg_out(__VA_ARGS__)

//-------------------------------------------------------------------------------------------------------------------------------------------------------------------//


int32_t main(){

    std::ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int w, n; cin>>w>>n;
    vector<array<int, 3>> v;
    forn{
        int vi, wi, ki; cin>>vi>>wi>>ki;
        v.pb({vi, wi, ki});
    }

    vector<array<int, 2>> v2;
    for(auto val: v){
        auto[vi, wi, ki] = val;
        for(int i = 0; i <= 34; i++){
            int x = (1LL << i);
            if(ki < x) break;
            ki -= x;
            v2.pb({vi*x, wi*x});
        }
        if(ki > 0) v2.pb({vi*ki, wi*ki});
    }

    n = v2.size();
    int dp[2][w+1];
    for(int i = 0; i < 2; i++){
        for(int j = 0; j <= w; j++) dp[i][j] = 0;
    }

    for(int i = 0; i < n; i++){
        for(int j = 1; j <= w; j++){
            if(i == 0){
                if(v2[i][1] == j) dp[0][j] = v2[i][0];
            }
            else{
                int cur, prev;
                if(i&1) cur = 1, prev = 0;
                else prev = 1, cur = 0;
                dp[cur][j] = dp[prev][j];
                if(j - v2[i][1] >= 0) dp[cur][j] = max(dp[cur][j], dp[prev][j-v2[i][1]] + v2[i][0]);
            }
        }
    }

    int ans = 0;
    for(int i = 0; i <= w; i++) ans = max({ans, dp[0][i], dp[1][i]});

    cout << ans << ed;


}
#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...