제출 #725404

#제출 시각아이디문제언어결과실행 시간메모리
725404Mohamed_Kachef06Knapsack (NOI18_knapsack)C++17
100 / 100
815 ms177032 KiB
#include <bits/stdc++.h> using namespace std; #define MODY ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #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[22001][2001]; int knapsack(int i , int w){ if (i==-1) 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>()); 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...