제출 #963466

#제출 시각아이디문제언어결과실행 시간메모리
963466bashNewbieKnapsack (NOI18_knapsack)C++17
100 / 100
57 ms4004 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) 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 add(int j, int s) { sort(rng(wt[j]), greater<pi>()); int m = 0; for(auto [v, c]: wt[j]) { for(int i = 0; i < c && m+j <= s; i++, m+=j) { fin.pb({v, j}); } } } 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 j = 0; j <= s; j++) add(j, s); memset(dp, 0xff, sizeof(dp)); dp[0] = 0; int ret = 0; for(auto [v, w]: fin) { for(int j = s, k = s-w; ~k; j--, k--) { if(~dp[k]) dp[j] = max(dp[j], dp[k]+v), ret = max(ret, dp[j]); } } 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...