# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
493262 | HaYoungJoon | Knapsack (NOI18_knapsack) | C++14 | 83 ms | 3036 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll dp[2001];
typedef pair<ll,ll> pii;
vector<pii > pre[2001],items;
bool cmp(pii A, pii B) {
return A.first > B.first;
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
int n,S;
cin >> S >> n;
ll v,k,w;
for (int i = 1; i <= n; i++) {
cin >> v >> w >> k;
pre[w].push_back({v,k});
}
items.push_back({0,0});
for (int w = 1; w <= S; w++) {
if (pre[w].empty()) continue;
sort(pre[w].begin(),pre[w].end(),cmp);
ll weight = S;
for (int i = 0; i < pre[w].size() && weight > 0; i++) {
for (int j = 1; j <= pre[w][i].second && weight > 0; j++) {
weight -= w;
if (weight < 0) break;
items.push_back({w,pre[w][i].first});
}
}
}
for (int i = 1; i < items.size(); i++)
for (int j = S; j >= 0; j--)
if (j - items[i].first >= 0) dp[j] = max(dp[j],dp[j - items[i].first ] + items[i].second);
ll res = 0;
for (int i = 1; i <= S; i++) res = max(res,dp[i]);
cout << res;
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |