제출 #751049

#제출 시각아이디문제언어결과실행 시간메모리
751049hippo123Knapsack (NOI18_knapsack)C++17
0 / 100
1 ms340 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define pr pair<int, int> #define f first #define s second ll n, s; bool cmp(pr a, pr b){ return a.f> b.f; } int main(){ ios::sync_with_stdio(false); cin.tie(0); cin>>n>>s; vector<ll> dp(s+1, LLONG_MIN); vector<vector<pr>> list(2001); for (int i=0; i<=n; i++){ int vi, wi, ki; cin>>vi>>wi>>ki; list[wi].push_back({vi, ki}); } // sort the items per values from top to bottom with the same weight for (int i=1; i<=s; i++){ if(list[i].size()>0) sort(list[i].begin(), list[i].end(), cmp); } // now using the standard knapsack to calculate the problem dp[0]=0; for (int i=1; i<=s; i++){ // convert to weight dim int weight=0; if(!(list[i].size()>0)) continue; // empty weight item for (int j=0; j<(int) list[i].size() && weight<=s; j++){ // # of items ki but it should be also limited to s weight weight+=i; // total weight until this point for (int k=s; k>=0; k--){ if(dp[k-i]>=0 ) { dp[k]=max(dp[k], dp[k-i]+list[i][j].f); } } } } ll mval=0; for (int i=0; i<=s; i++) mval=max(mval, dp[i]); cout<<mval; }
#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...