Submission #1268472

#TimeUsernameProblemLanguageResultExecution timeMemory
1268472herominhsteveKnapsack (NOI18_knapsack)C++20
37 / 100
1 ms328 KiB
#include <bits/stdc++.h>
#define el '\n'
#define FNAME "Bounded Knapsack"
#define allof(x) x.begin(),x.end()
#define allof1(x) x.begin()+1,x.end()
#define mset(x,n) memset(x,(n),sizeof(x))
using namespace std;
const long long MOD = (long long) 1e9+7;
template<class X,class Y> bool minimize(X &a,Y b){ if (a>b) {a=b; return true;} return false;}
template<class X,class Y> bool maximize(X &a,Y b){ if (a<b) {a=b; return true;} return false;}

void setup(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
	if (fopen(FNAME".inp","r")){
		freopen(FNAME".inp","r",stdin);
		freopen(FNAME".out","w",stdout);
	}
}

struct Goods{
    int w;
    long long val;
    Goods(int W,long long V): w(W), val(V) {}
};

int W,n;
vector<Goods> goods;

void init(){
	cin>>W>>n;
    for (int i=0;i<n;i++){
        int w,maxTake;
        long long val;
        cin>>val>>w>>maxTake;
        int pow2=1;
        while (maxTake>0){
            int take = min(pow2,maxTake);
            int newW = w * take;
            long long newVal = val * 1ll * take;
            if (newW <= W) goods.emplace_back(newW,newVal);
            maxTake -= take;
            pow2 <<=1;
        }
    }
}

void sol(){
	vector<long long> dp(W+1,0);
    for (Goods x : goods){
        int w  = x.w;
        long long val = x.val;
        for (int j=W;j>=w;j--){
            maximize(dp[j],dp[j-w] + val);
        }
    }
    cout<<dp[W];
}

int main(){
    setup();
    init();
    sol();
}

Compilation message (stderr)

knapsack.cpp: In function 'void setup()':
knapsack.cpp:16:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |                 freopen(FNAME".inp","r",stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:17:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   17 |                 freopen(FNAME".out","w",stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#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...