제출 #1090214

#제출 시각아이디문제언어결과실행 시간메모리
1090214vjudge1Knapsack (NOI18_knapsack)C++17
29 / 100
1 ms760 KiB
#include <bits/stdc++.h> #define int long long #define pii pair<int, int> using namespace std; const int N = 1e5 + 5, MAX = 2e3 + 5, MOD = 1e9 + 7, INF = 1e18 + 5;; int s, n, v[N], w[N], k[N], dp[N]; vector<pii> items[MAX]; vector<pii> fin; signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin >> s >> n; for(int i = 1; i <= n; i++){ cin >> v[i] >> w[i] >> k[i]; items[w[i]].push_back({v[i], k[i]}); } for(int i = 1; i <= s; i++){ sort(items[i].rbegin(), items[i].rend()); int needed = ((s + i - 1) / i); for(pii item : items[i]){ int used = min(needed, item.second); needed -= item.second; int cnt = 1; while(used >= cnt){ fin.push_back({i * cnt, item.first * cnt}); used -= cnt; cnt *= 2; } if(used) fin.push_back({i * used, item.first * used}); if(needed == 0) break; } } for(int i = 0; i <= s; i++) dp[i] = -INF; dp[0] = 0; int sum = 0; for(pii item : fin){ for(int i = min(sum, s - item.first); i >= 0; i--){ dp[i + item.first] = max(dp[i + item.first], dp[i] + item.second); } sum += item.first; } cout << *max_element(dp, dp + s + 1); return 0; }
#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...