Submission #514819

#TimeUsernameProblemLanguageResultExecution timeMemory
514819karthikhegde05Knapsack (NOI18_knapsack)C++17
100 / 100
122 ms4932 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; vector<vector<pair<ll, ll>>> V(S+2); vll K(S+2, 0); FOR(i, 0, n-1){ ll v, w, k;cin>>v>>w>>k; V[w].pb({v, k}); K[w]+=k; } for(int i=0; i<=S; i++){ if(!V[i].empty())sort(all(V[i]), [&](pair<ll, ll>& x, pair<ll, ll>& y){return x.first>y.first;}); for(int j=1; j<sz(V[i]); j++){ V[i][j].second += V[i][j-1].second; } } vll dp(S+2, 0); for(int i=1; i<=S; i++){ for(int x=S; x>=0; x--){ ll val = 0;int ptr = 0; for(int k=1; k<=K[i]; k++){ if(x+k*i>S)break; if(k>V[i][ptr].second)ptr++; if(ptr>=sz(V[i]))break; val += V[i][ptr].first; dp[x+k*i] = max(dp[x+k*i], dp[x]+val); } } } 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...