Submission #967828

#TimeUsernameProblemLanguageResultExecution timeMemory
967828vjudge1Knapsack (NOI18_knapsack)C++17
100 / 100
57 ms3924 KiB
#include<bits/stdc++.h> #include<ext/numeric> #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update #define Fast ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr) using namespace std; using namespace __gnu_cxx; using namespace __gnu_pbds; #define all(s) s.begin(),s.end() #define rall(s) s.rbegin(),s.rend() template<class T> using ordered_set = tree<T, null_type , less<T> ,rb_tree_tag , tree_order_statistics_node_update> ; typedef long long ll; const int N=5e2+3,M=2e7+3,LG=__lg(N)+2,MOD=998244353; void solve() { int n,s,V,W,K; cin>>s>>n; vector<vector<pair<int,int>>>bestW(s+1); for (int i = 0; i <n ; ++i) { cin >> V >> W >> K; bestW[W].emplace_back(V,K); } vector<int>v,w,dp(s+1,INT_MIN); for (int i = 1; i <=s ; ++i) { sort(all(bestW[i])); for (int j = 0; j <s&&!bestW[i].empty() ; j+=i) { v.push_back(bestW[i].back().first); w.push_back(i); if(!--bestW[i].back().second) bestW[i].pop_back(); } } n=v.size(); dp[0]=0; for (int i = 0; i <n ; ++i) { for (int j = s; j >=w[i] ; --j) { dp[j]=max(dp[j],dp[j-w[i]]+v[i]); } } cout<<*max_element(all(dp))<<'\n'; } int main() { Fast; int tc = 1; //cin >> tc; for (int i = 1; i <= tc; ++i) { // cout<<"Case #"<<i<<": "; solve(); } }
#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...