Submission #633131

#TimeUsernameProblemLanguageResultExecution timeMemory
633131AlmaKnapsack (NOI18_knapsack)C++17
73 / 100
734 ms262144 KiB
#include <bits/stdc++.h> using namespace std; #define fi first #define se second using ll = long long; using ii = pair<int,int>; const int INF = 1e9; const ll LLINF = 1e18; using vi = vector<int>; using vvi = vector<vi>; void setIO (string fileName) { ios::sync_with_stdio(false); cin.tie(NULL); if (fileName != "std") { freopen((fileName + ".in").c_str(), "r", stdin); freopen((fileName + ".out").c_str(), "w", stdout); } } int S, N; vi v, w, k; vector<vector<ll>> DP; ll knapsack (int i, int rem) { if (i == N or rem == 0) return 0; ll &ans = DP[i][rem]; if (ans != -1) return ans; if (rem < w[i]) return ans = knapsack(i+1, rem); for (int x = 0; x <= k[i] and rem >= w[i]*x; x++) { ans = max(ans, v[i]*x + knapsack(i+1, rem - w[i]*x)); } return ans; } int main() { setIO("std"); cin >> S >> N; v = w = k = vi(N); for (int i = 0; i < N; i++) { cin >> v[i] >> w[i] >> k[i]; k[i] = min(k[i], S / w[i]); } DP = vector<vector<ll>>(N, vector<ll>(S+1, -1)); cout << knapsack(0, S) << '\n'; return 0; }

Compilation message (stderr)

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