# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
493259 | HaYoungJoon | Knapsack (NOI18_knapsack) | C++14 | 2 ms | 1996 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[10000][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 = 0; j <= S; j++)
if (j - items[i].first >= 0) dp[i][j] = max(dp[i-1][j],dp[i-1][j - items[i].first ] + items[i].second);
else dp[i][j] = dp[i][j-1];
cout << dp[items.size()-1][S];
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... |