제출 #863422

#제출 시각아이디문제언어결과실행 시간메모리
863422tienbinhKnapsack (NOI18_knapsack)C++17
100 / 100
62 ms4948 KiB
#include <bits/stdc++.h> using namespace std; int s , n; const int N = 1e5 + 7; int w[N] , v[N] , k[N]; vector<pair<int ,int>> adj[2007]; long long dp[2][2007]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> s >> n; for(int i = 1 ; i <= n ; i++){ cin >> v[i] >> w[i] >> k[i]; adj[w[i]].emplace_back(v[i] , k[i]); } for(int i = 1; i <= 2000 ; i ++) sort(adj[i].rbegin() , adj[i].rend()); n = 0; for(int i = 1 ; i <= 2000 ; i++){ int lim = s / i; for(int j = 0, h = 0; j < lim; j++) { if (h == (int)adj[i].size()) break; w[++n] = i; v[n] = adj[i][h].first; adj[i][h].second--; if (!adj[i][h].second) h++; } } memset(dp , 0xef , sizeof dp); dp[0][0] = 0; for(int i = 1 ; i <= n ; i++){ for(int val = s ; val >= 0 ; val--) { dp[i & 1][val] = dp[~i & 1][val]; if (val >= w[i]) dp[i & 1][val] = max(dp[i & 1][val], dp[~i & 1][val - w[i]] + v[i]); } } long long ans = numeric_limits<long long>::min(); for (int i = 0; i <= s; i++) ans = max(ans, dp[n & 1][i]); cout << ans << '\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...