Submission #548560

#TimeUsernameProblemLanguageResultExecution timeMemory
548560AlexandruabcdeKnapsack (NOI18_knapsack)C++14
12 / 100
1 ms212 KiB
#include <bits/stdc++.h> using namespace std; constexpr int NMAX = 1e5 + 5; constexpr int SUMMAX = 2005; typedef long long LL; struct Item { int value; int weight; int copies; bool operator < (const Item &other) const { return (weight < other.weight || (weight == other.weight && value < other.value)); } }; int S, N; Item V[NMAX]; LL dp[SUMMAX]; int main () { cin >> S >> N; for (int i = 1; i <= N; ++ i ) cin >> V[i].value >> V[i].weight >> V[i].copies; sort(V+1, V+N+1); for (int i = 1; i <= N; ++ i ) { LL cnt = 0; int dr = i; while (dr <= N && V[dr].weight == V[i].weight) { cnt += 1LL * V[dr].copies; ++ dr; } int ind = i; int w = V[i].weight; for (int nr = 1; ind < dr && 1LL * nr <= cnt && nr <= S / V[i].weight; ++ nr ) { for (int s = S; s >= w; -- s ) dp[s] = max(dp[s], dp[s - w] + 1LL * V[ind].value); -- V[ind].copies; if (V[ind].copies == 0) ++ ind; } i = dr - 1; } cout << dp[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...