Submission #538712

#TimeUsernameProblemLanguageResultExecution timeMemory
538712kabikaKnapsack (NOI18_knapsack)C++14
49 / 100
135 ms262144 KiB
#include <iostream> #include <vector> #include <algorithm> #include <math.h> using namespace std; typedef long long ll; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int s, n; cin >> s >> n; if (n == 1) { int v, w; ll k; cin >> v >> w >> k; int m = s / w; if (m <= k) cout << v * m << '\n'; else cout << v * k << '\n'; return 0; } vector<int> v, w; for (int i = 0; i < n; ++i) { int val, wt; ll k; cin >> val >> wt >> k; v.insert(v.end(), k, val); w.insert(w.end(), k, wt); } ll sz = v.size(); vector<vector<int>> dp(sz + 1, vector<int>(s + 1, 0)); for (int i = 1; i <= sz; ++i) { for (int j = 1; j <= s; ++j) { if (j >= w[i - 1]) dp[i][j] = max(dp[i - 1][j - w[i - 1]] + v[i - 1], dp[i - 1][j]); else dp[i][j] = dp[i - 1][j]; } } cout << dp[sz][s] << '\n'; return 0; }
#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...