Submission #725192

#TimeUsernameProblemLanguageResultExecution timeMemory
725192Mohamed_Kachef06Knapsack (NOI18_knapsack)C++17
17 / 100
3 ms3708 KiB
#include <bits/stdc++.h> using namespace std; #define MODY ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define int long long #define pii pair<int , int> #define A first #define B second #define pb push_back int n , s; vector<vector<pii>> vec(2001); vector<pii> a; int dp[100][2001]; int knapsack(int i , int w){ if (i==0) return 0 ; if (~dp[i][w]) return dp[i][w]; int x = 0 , y = 0; if (w>=a[i].A) x = knapsack(i-1 , w-a[i].A) + a[i].B; y = knapsack(i-1 , w); return dp[i][w] = max(x, y); } signed main(){ memset(dp , -1 , sizeof dp); cin >> s >> n; for (int i = 1 ; i<=n ; i++){ int v , w , k; cin >> v >> w >> k; vec[w].pb({v , k}); } for (int i = 1 ; i<=s ; i++) sort(vec[i].begin() , vec[i].end() , greater<pii>()); a.pb({0 , 0}); for (int i = 1 ; i<=s ; i++){ int copies = 0; for (auto j : vec[i]){ while(copies < s/i && j.B) { a.pb({i , j.A}); j.B--; copies++; } } } cout << knapsack(a.size()-1 , s); }
#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...