제출 #1278705

#제출 시각아이디문제언어결과실행 시간메모리
1278705jackofall718Knapsack (NOI18_knapsack)C++20
100 / 100
42 ms1672 KiB
#include <bits/stdc++.h> #define ll long long using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int S, N; cin >> S >> N; vector<vector<pair<int,int>>> A(S+1); // group by weight: (value, count) for (int i = 0; i < N; i++) { int V, W, K; cin >> V >> W >> K; if (W <= S) { A[W].push_back({V, K}); } } vector<ll> dp(S+1, 0); ll answer = 0; for (int w = 1; w <= S; w++) { if (A[w].empty()) continue; sort(A[w].begin(), A[w].end(), greater<pair<int,int>>()); int idx = 0; for (int taken = 0; taken < S / w; taken++) { if (idx >= (int)A[w].size()) break; for (int j = S; j >= w; j--) { dp[j] = max(dp[j], dp[j - w] + A[w][idx].first); answer = max(answer, dp[j]); } A[w][idx].second--; if (A[w][idx].second == 0) idx++; } } cout << answer << "\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...