제출 #908571

#제출 시각아이디문제언어결과실행 시간메모리
908571Ronak1808Knapsack (NOI18_knapsack)C++17
100 / 100
135 ms34768 KiB
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <chrono> using namespace __gnu_pbds; using namespace std; using namespace std::chrono; template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template <class T> using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>; const int MOD = 998244353; int add(int x, int y){ return ((x + y) % MOD + MOD) % MOD; } int mul(int x, int y){ return x * 1ll * y % MOD; } int binpow(int x, int y){ int z = 1; while(y){ if(y % 2 == 1) z = mul(z, x); x = mul(x, x); y /= 2; } return z; } int inv(int x){ return binpow(x, MOD - 2); } int divide(int x, int y){ return mul(x, inv(y)); } int main(){ int s, n; cin>>s>>n; map<int, vector<pair<int, int>>>mp; for(int i = 1;i<=n;i++){ int v, w, k; cin>>v>>w>>k; mp[w].push_back({v, k}); } for(auto &x : mp){ sort((x.second).begin(), (x.second).end(), greater<pair<int, int>>()); } vector<vector<long long>>dp(int(mp.size()), vector<long long>(s+1, 0LL)); auto it = mp.begin(); long long res = 0; for(int i = 0;i<int(mp.size());i++){ for(int use = 0; use <= s; use++){ int sum = 0; long long val = 0; dp[i][use] = (i-1 >= 0?dp[i-1][use]:0LL); for(auto &x : it->second){ int v = x.second; int c = x.first; bool done= false; while(v--){ sum += it->first; if(sum > use){ done = true; break; } val += c; dp[i][use] = max(dp[i][use], (i-1 >= 0?dp[i-1][use-sum]:0LL) + val); } if(done)break; } res = max(res, dp[i][use]); } it++; } cout<<res<<"\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...