Submission #613568

#TimeUsernameProblemLanguageResultExecution timeMemory
613568anirudh001Knapsack (NOI18_knapsack)C++14
0 / 100
1 ms212 KiB
#include <bits/stdc++.h> #define ll long long using namespace std; const ll N = 1e6; int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ll s, n; cin >> s >> n; map<ll, vector<pair<ll, ll>>> mp; for(int i = 0; i<n; i++){ ll v, w, k; cin >> v >> w >> k; if(w<=s && k>0){ mp[w].push_back({v, k}); } } ll m = mp.size(); ll ans = 0; vector<vector<ll>> dp(m+1, vector<ll>(s+1, LONG_LONG_MIN)); dp[0][0] = 0; ll i = 1; for(auto x: mp){ auto items = x.second; auto w = x.first; sort(items.begin(), items.end(), greater<pair<ll, ll>>()); for(int j=0; j<=s; j++){ dp[i][j] = dp[i-1][j]; ll copies = 0, k = 0, cur = 0, profit = 0; while((copies+1)*w<=j && k<items.size()){ copies++; profit+=items[k].first; if(dp[i-1][j-copies*w]!=LONG_LONG_MIN){ dp[i][j] = max(dp[i][j], dp[i-1][j-copies*w] + profit); } cur++; if(cur==items[k].second){ cur = 0; k++; } } ans = max(dp[i][j],ans); } i++; } cout << ans << endl; return 0; }

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:37:39: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   37 |             while((copies+1)*w<=j && k<items.size()){
      |                                      ~^~~~~~~~~~~~~
knapsack.cpp:9:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    9 |     freopen("input.txt", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:10:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   10 |     freopen("output.txt", "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...