제출 #756399

#제출 시각아이디문제언어결과실행 시간메모리
756399asdasdqwerKnapsack (NOI18_knapsack)C++14
73 / 100
422 ms262144 KiB
#include <bits/stdc++.h> using namespace std; int main() { int s, n; cin >> s >> n; vector<int64_t> dp(s+1, 0); vector<int> div(s+1); if (n == 1) { int64_t v, w, k; cin >> v >> w >> k; int times = min(s / w, k); cout << v * times << "\n"; return 0; } // calculate div for (int i = 1; i <= s; i++) { div[i] = s / i; } vector<vector<int>> value(s+1, vector<int>()); for (int i = 0; i < n; i++) { int v, w, k; cin >> v >> w >> k; int m = min(div[w], k); for (int j = 0; j < m; j++) { value[w].push_back(v); } } for (int i = 1; i <= s; i++) { sort(value[i].begin(), value[i].end(), greater<int>()); div[i] = min(div[i], (int)value[i].size()); } for (int i = s; i > 0; i--) { for (int j = 0; j < div[i]; j++) { for (int k = s; k >= i; k--) { if (dp[k] < dp[k - i] + value[i][j]) { dp[k] = max(dp[k], dp[k - i] + value[i][j]); } } } } cout << dp[s] << "\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...