Submission #1003105

#TimeUsernameProblemLanguageResultExecution timeMemory
1003105fryingducKnapsack (NOI18_knapsack)C++17
73 / 100
1098 ms17632 KiB
// #pragma GCC optimize("Ofast,unroll-loops")
#include "bits/stdc++.h"
using namespace std;

#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()

#define yes cout << "YES\n"
#define no cout << "NO\n"

#ifdef LOCAL
#define debug(val) cerr << "["#val << " = " << (val) << "] "
#define slash() cerr << "\n-----------\n"
#define time() cerr << "Time is: " << 1000 * clock()/CLOCKS_PER_SEC << " ms\n"
#else
#define debug(val) 1407
#define slash() 1407
#define time() 1407
#endif

const string filename = "";

void IO(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    if(filename.size()){
        string in = filename + ".inp";
        string out = filename + ".out";
        freopen(in.c_str(), "r", stdin);
        freopen(out.c_str(), "w", stdout);
    }
}
int n, W;
vector<long long> v, w;
void solve(){
    cin >> W >> n;
    for(int i=0;i<n;i++){
        int a, b, c; cin >> b >> a >> c;
        int bits = 1;
        while(c - bits >= 0){
            if(1ll * a * bits <= W) {
                v.push_back(1ll * b * bits);
                w.push_back(1ll * a * bits);
            }
            c -= bits;
            bits *= 2;
        }
        if(c){
            if(1ll * c * a <= W) {
                v.push_back(1ll * c * b);
                w.push_back(1ll * c * a);
            }
        }
    }
    vector<long long> dp(W+1);
    for(int i=0;i< (int)v.size();i++){
        for(int j=W;j>=w[i];j--){
            if(j - w[i] >= 0) dp[j] = max(dp[j], dp[j-w[i]] + v[i]);
        }
    }
    cout << dp[W];
}
signed main(){
    IO();
	int test = 1;
	/* cin >> test; */
	for(int i=1;i<=test;i++){
		// cout << "Case " << "#" << i << ": ";
		solve();
	}
	return 0;
}

Compilation message (stderr)

knapsack.cpp: In function 'void IO()':
knapsack.cpp:29:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   29 |         freopen(in.c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:30:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   30 |         freopen(out.c_str(), "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...