제출 #991169

#제출 시각아이디문제언어결과실행 시간메모리
991169akkshaysrKnapsack (NOI18_knapsack)C++17
100 / 100
98 ms239328 KiB
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #define fr first #define se second #define rep(i,a,b) for(int i = a; i < (b); ++i) #define rrep(i,a,b) for(int i = a; i > (b); --i) #define sz(x) (int)(x).size() #define all(x) (x).begin(),(x).end() #define IN(i,l,r) (l<i&&i<r) #define pb push_back #define ones __builtin_popcountll using namespace std; using namespace __gnu_pbds; template <class T> using OSTree = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>; typedef pair<int,int> pi; typedef vector<int> vi; typedef vector<long long> vll; typedef long long ll; struct item{ ll v,w,k; }; int S,N; vector<vector<item>> items; ll dp[16400][2001]; int main(){ cin.tie(0)->sync_with_stdio(false); cin >> S >> N; items = vector<vector<item>>(S+1); rep(i,0,N){ ll v,w,k; cin >> v >> w >> k; items[w].pb({v,w,k}); } vector<pi> obj; rep(i,1,S+1){ sort(all(items[i]),[](item& a, item& b){ return a.v > b.v; }); int j = 0, mx = S/i; for(auto& p : items[i]){ if(j + p.k < mx){ rep(i,0,p.k) obj.pb({p.v,p.w}); j += p.k; }else{ rep(i,j,mx) obj.pb({p.v,p.w}); break; } } } // knapsack on obj rep(i,1,sz(obj)+1){ rep(j,obj[i-1].se,S+1){ dp[i][j] = max(dp[i-1][j], dp[i-1][j-obj[i-1].se] + obj[i-1].fr); } } cout << dp[sz(obj)][S] << '\n'; }
#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...