Submission #528310

#TimeUsernameProblemLanguageResultExecution timeMemory
528310theysoldtheworldKnapsack (NOI18_knapsack)C++14
73 / 100
783 ms262148 KiB
#include <bits/stdc++.h> using namespace std; struct Item { long long v , w , k; }; int main() { ios::sync_with_stdio(false); cin.tie(0); int S , N; cin >> S >> N; vector<Item> a(N); vector<pair<long long , long long>> who; for (int i = 0 ; i < N ; i++) { cin >> a[i].v >> a[i].w >> a[i].k; a[i].k = min(a[i].k , S * 1LL); for (int j = 0 ; j < a[i].k ; j++) { who.emplace_back(a[i].v , a[i].w); } } vector<long long> dp(S + 1); vector<bool> Reachable(S + 1); Reachable[0] = true; for (int i = 0 ; i < (int)who.size() ; i++) { for (int j = S ; j >= 0 ; j--) { if (j + who[i].second <= S && Reachable[j]) { Reachable[who[i].second + j] = true; dp[who[i].second + j] = max(dp[who[i].second + j] , dp[j] + who[i].first); } } } cout << *max_element(dp.begin(),dp.end()) << '\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...