Submission #935453

#TimeUsernameProblemLanguageResultExecution timeMemory
935453shinigami_09Knapsack (NOI18_knapsack)C++17
49 / 100
1089 ms66148 KiB
#include <bits/stdc++.h> using namespace std; #define int long long // int dp[2001][100001]; map<pair<int,pair<int,int>>,int>dp; int f(int n, int s, vector<int> &val, vector<int> &w, vector<int> &freq) { if (s == 0) return 0; if (n == 0) return 0; if(dp.find({s,{n,freq[n-1]}})!=dp.end())return dp[{s,{n,freq[n-1]}}]; int ans = 0; if (w[n - 1] <= s and freq[n - 1] > 0) { vector<int> temp = freq; temp[n - 1]--; ans = max(ans, val[n - 1] + f(n, s - w[n - 1], val, w, temp)); ans = max(ans, f(n - 1, s, val, w, freq)); } else { ans = max(ans, f(n - 1, s, val, w, freq)); } return dp[{s,{n,freq[n-1]}}] = ans; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); // memset(dp, -1, sizeof dp); dp.clear(); int s, n; cin >> s >> n; vector<int> val(n), w(n), freq(n); for (int i = 0; i < n; i++) { cin >> val[i] >> w[i] >> freq[i]; } cout << f(n, s, val, w, freq); 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...