Submission #963467

#TimeUsernameProblemLanguageResultExecution timeMemory
963467bashNewbieKnapsack (NOI18_knapsack)C++17
100 / 100
64 ms1680 KiB
#include <iostream> #include <vector> #include <algorithm> #include <cstring> #include <utility> using namespace std; #define fast_io ios::sync_with_stdio(0), cin.tie(0) #define pb push_back #define sz(x) (int)x.size() #define rng(x) begin(x), end(x) #define fst first #define snd second template <typename T> using vt = vector<T>; using vi = vt<int>; using pi = pair<int, int>; using vp = vt<pi>; const int N = 2e3+7; int dp[N]; vp wt[N]; vp fin; void solve() { int s, n; cin >> s >> n; for(int i = 0; i < n; i++) { int v, w, c; cin >> v >> w >> c; wt[w].pb({v, c}); } for(int i = 1; i <= s; i++) sort(rng(wt[i]), greater<pi>()); memset(dp, 0xff, sizeof(dp)); dp[0] = 0; int ret = 0; for(int w = 1; w <= s; w++) { if(!sz(wt[w])) continue; int l = sz(wt[w]), p = 0, cp = 0, m = 0; auto [cv, cc] = wt[w][p]; while(p < l && m <= s) { for(int j = s, k = s-w; ~k; j--, k--) { if(~dp[k]) dp[j] = max(dp[j], dp[k]+cv), ret = max(ret, dp[j]); } cp++, m += w; if(cp >= cc) { p++, cp = 0; if(p < l) cv = wt[w][p].fst, cc = wt[w][p].snd; } } } cout << ret << "\n"; } int main() { fast_io; 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...