Submission #1286636

#TimeUsernameProblemLanguageResultExecution timeMemory
1286636ducmanh2612Knapsack (NOI18_knapsack)C++20
73 / 100
1096 ms8684 KiB
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int S, N; cin >> S >> N; vector<long long> v(N), w(N), k(N); for (int i = 0; i < N; i++) cin >> v[i] >> w[i] >> k[i]; // --- Gộp vật có cùng (w, v) --- map<pair<long long,long long>, long long> merged; for (int i = 0; i < N; i++) { if (w[i] > S || k[i] == 0) continue; merged[{w[i], v[i]}] += k[i]; } vector<long long> dp(S + 1, 0); for (auto &[p, cnt] : merged) { long long wi = p.first, vi = p.second; long long Ki = min(cnt, (long long)S / wi); // binary splitting for (long long j = 1; Ki > 0; j <<= 1) { long long tmp = min(j, Ki); long long weight = tmp * wi; long long value = tmp * vi; for (int s = S; s >= weight; s--) { dp[s] = max(dp[s], dp[s - weight] + value); } Ki -= tmp; } } cout << *max_element(dp.begin(), dp.end()) << "\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...