Submission #1088630

#TimeUsernameProblemLanguageResultExecution timeMemory
1088630shidou26Knapsack (NOI18_knapsack)C++14
100 / 100
274 ms4036 KiB
#include <bits/stdc++.h> using namespace std; #define endl '\n' #define all(v) v.begin(), v.end() typedef long long ll; typedef pair<int, int> ii; typedef pair<ll, int> li; const int N = 2008; int s, n; map<int, vector<ii>> items; void prepare() { } void input() { cin >> s >> n; for(int i = 1; i <= n; i++) { int v, w, k; cin >> v >> w >> k; items[w].push_back(ii(v, k)); } } void process() { vector<ll> dp, prev_dp; prev_dp.resize(s + 1, -1e18); dp.resize(s + 1, -1e18); prev_dp[0] = 0; ll best = 0; for(pair<int, vector<ii>> p : items) { sort(all(p.second), greater<ii>()); for(int j = 0; j <= s; j++) { dp[j] = prev_dp[j]; int total = 0; ll profit = 0; for(int k = 0; k < (int)p.second.size(); k++) { int v, a; tie(v, a) = p.second[k]; while(a > 0 && (total + 1) * p.first <= j) { total++; profit += v; a--; dp[j] = max(dp[j], prev_dp[j - total * p.first] + profit); } } best = max(best, dp[j]); } prev_dp = dp; } cout << best << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); #define task "grouping" if(fopen(task".INP", "r")) { freopen(task".INP", "r", stdin); freopen(task".OUT", "w", stdout); } prepare(); int testcase = 1; // cin >> testcase; for(int i = 1; i <= testcase; i++) { input(); process(); } return 0; }

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:60:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   60 |         freopen(task".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:61:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   61 |         freopen(task".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...