Submission #532708

#TimeUsernameProblemLanguageResultExecution timeMemory
532708mihhKnapsack (NOI18_knapsack)C++17
100 / 100
155 ms21208 KiB
#include <bits/stdc++.h> #define db(x) cerr << #x <<":"<<x<<" " using namespace std; const int INF=1e9, MOD=1e9+7; const int N=1e5+2; typedef array<int,3>item ; int main(){ int n, kg; cin>>kg>>n; vector<item> a(n+1); unordered_map<int,vector<item>> byWeight; for(int i=1;i<=n;++i){ cin>>a[i][0]>>a[i][1]>>a[i][2]; byWeight[a[i][1]].push_back(a[i]); // auto[v,w,k]=a[i]; // 0 1 2 } int i=0; vector<vector<int>> bst(byWeight.size()+1,vector<int>(kg+1,INT32_MIN)); bst[0][0]=0; for(auto &[w, itms]:byWeight){ ++i; sort(itms.begin(), itms.end(), std::greater<item>()); db(w); for(int j=0;j<=kg;++j){ bst[i][j]=bst[i-1][j]; int profit=0; for(int curr=0,r=1,currUsed=0; curr<(int)itms.size() and r*w<=j; ++r){ profit+=itms[curr][0]; ++currUsed; bst[i][j]=max( bst[i][j], bst[i-1][j-r*w]+profit ); if(currUsed==itms[curr][2]){ ++curr; currUsed=0; } } } } int ans=0; for(int i=1;i<=kg;++i) ans=max(ans,bst[byWeight.size()][i]); cout<<ans; }
#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...