제출 #514813

#제출 시각아이디문제언어결과실행 시간메모리
514813karthikhegde05Knapsack (NOI18_knapsack)C++17
73 / 100
1088 ms3296 KiB
#include<bits/stdc++.h> using namespace std; #include<ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; template<class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define ook order_of_key #define fbo find_by_order typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<pii> vpii; typedef vector<long long> vll; #define PI acos(-1.0) #define EPS 1e-9 #define INF 1000000000 #define setbits(x) __builtin_popcountll(x) #define zrobits(x) __builtin_ctzll(x) #define pb push_back #define mkp make_pair #define sqr(a) ((a) * (a)) #define sz(a) int((a).size()) #define all(a) (a).begin(), (a).end() #define forn(i, n) for (int i = 0; i < int(n); ++i) #define FOR(i, l, r) for(int i=int(l); i<=int(r); i++) #define ROF(i, l, r) for(int i=int(r); i>=int(l); --i) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int MOD = 1e9+7; void print_out(int x){ cout<<"Case #"<<x<<": "; } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, S; cin>>S>>n; vll V(n), W(n), K(n); vi unlim, lim; FOR(i, 0, n-1){ cin>>V[i]>>W[i]>>K[i]; if(K[i]*W[i]>S)unlim.pb(i); else lim.pb(i); } vll dp(S+2, 0); for(int x=1; x<=S; x++){ for(int i=0; i<sz(unlim); i++){ if(x-W[unlim[i]]>=0)dp[x] = max(dp[x], dp[x-W[unlim[i]]]+V[unlim[i]]); } } for(int i: lim){ for(int x=S; x>=0; x--){ for(int k=1; k<=K[i]; k++){ if(x+k*W[i]>S)break; dp[x+k*W[i]] = max(dp[x+k*W[i]], dp[x]+k*V[i]); } } } ll ans = 0; for(int x=S; x>=0; x--){ ans = max(ans, dp[x]); } cout<<ans<<"\n"; 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...