Submission #857157

#TimeUsernameProblemLanguageResultExecution timeMemory
857157neodoomerKnapsack (NOI18_knapsack)C++14
73 / 100
173 ms262144 KiB
#include<bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define F first #define S second #define pi pair<int,int> int s; int knapsack ( vector<pi>& vw ){ int n=vw.size(); ll dp[n+1][s+1]; for(int i=0;i<=n;i++) for(int w=0;w<=s;w++) dp[i][w]=-1e18; ll ans=0; dp[0][0]=0; for(int i=1;i<=n;i++) { for(int w=0;w<=s;w++) { dp[i][w]=dp[i-1][w]; if(w-vw[i-1].S>=0) dp[i][w]=max(dp[i][w],dp[i-1][w-vw[i-1].S]+vw[i-1].F); ans=max(ans,dp[i][w]); } } return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0); int n; cin>>s>>n; vector< vector<pi> > a(s+1); vector<int> counter(s+1); for(int i=0;i<n;i++) { int v,w,k; cin>>v>>w>>k; counter[w]+=k; a[w].pb({v,k}); } vector<pi> important; for(int i=1;i<=s;i++) { if(a[i].empty())continue; sort(a[i].begin(),a[i].end()); int need = s/i +1; for(int j=a[i].size()-1;j>=0;j--) { int x=min(need,a[i][j].S); for(int z=0;z<x;z++) important.pb({a[i][j].F,i}),need--; } } cout<<knapsack(important); 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...