제출 #538716

#제출 시각아이디문제언어결과실행 시간메모리
538716kabikaKnapsack (NOI18_knapsack)C++17
49 / 100
127 ms262144 KiB
#include <iostream> #include <vector> #include <algorithm> #include <math.h> using namespace std; typedef long long ll; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int s, n; cin >> s >> n; if (n == 1) { int v, w; ll k; cin >> v >> w >> k; int m = s / w; if (m <= k) cout << v * m << '\n'; else cout << v * k << '\n'; return 0; } vector<int> v, w; for (int i = 0; i < n; ++i) { int val, wt; ll k; cin >> val >> wt >> k; v.insert(v.end(), k, val); w.insert(w.end(), k, wt); } ll sz = v.size(); vector<vector<int>> dp(sz + 1, vector<int>(s + 1, 0)); for (int i = 1; i <= sz; ++i) { for (int j = 1; j <= s; ++j) { if (j >= w[i - 1]) dp[i][j] = max(dp[i - 1][j - w[i - 1]] + v[i - 1], dp[i - 1][j]); else dp[i][j] = dp[i - 1][j]; } } cout << dp[sz][s] << '\n'; 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...