제출 #661503

#제출 시각아이디문제언어결과실행 시간메모리
661503asdf1234codingKnapsack (NOI18_knapsack)C++14
100 / 100
41 ms3684 KiB
#include <iostream> #include <algorithm> #include <vector> #include <climits> #include <cstring> using namespace std; #define problemname "ojuzknapsack" #define pii pair<int,int> #define vi vector<int> #define pb push_back #define MOD (int)1e9+7 #define ll long long #define ff first #define ss second bool ckmin(int& a, int b) {return a>b?a=b,true:false;} bool ckmax(int& a, int b) {return a<b?a=b,true:false;} int main () { ios_base::sync_with_stdio(0); cin.tie(0); // freopen (problemname ".in", "r", stdin); freopen(problemname ".out", "w", stdout); int s,n; cin>>s>>n; vector<vector<pii>> stuff(s+1); for(int i=0; i<n; i++) { int v,w,k; cin>>v>>w>>k; stuff[w].pb({v,k}); } for(int i=0; i<=s; i++) { sort(stuff[i].begin(), stuff[i].end(), greater<pii>()); } int dp[s+3]; memset(dp, 0xc0, sizeof(int)*(s+1)); dp[0]=0; for(int i=0; i<=s; i++) { int amntused=0; // amount used by objects with weight i for(pii obj : stuff[i]) { for(int cnt=0; cnt<obj.ss; cnt++) { amntused+=i; if(amntused>s) break; for(int thres=s; thres>=amntused; thres--) { ckmax(dp[thres], dp[thres-i]+obj.ff); } } if(amntused>s) break; } } int ans=0; for(int i=0; i<=s; i++) { ckmax(ans, dp[i]); } cout<<ans<<endl; 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...