# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
759824 | sofija6 | Knapsack (NOI18_knapsack) | C++14 | 1075 ms | 5044 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
#define MAXN 100010
#define MAXS 2010
using namespace std;
set<pair<ll,ll> > val[MAXS];
vector<ll> cnt[MAXN];
ll dp[MAXS],v[MAXN],w[MAXN],k[MAXN];
int main()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
ll s,n,ans=0;
cin >> s >> n;
for (ll i=1;i<=n;i++)
{
cin >> v[i] >> w[i] >> k[i];
for (ll j=1;j<=k[i] && j*w[i]<=s;j++)
{
val[j*w[i]].insert({j*v[i],i});
if (val[j*w[i]].size()*j*w[i]>s)
val[j*w[i]].erase(*(val[j*w[i]].begin()));
}
}
for (ll i=1;i<=s;i++)
{
for (auto j : val[i])
cnt[j.second].push_back(i/w[j.second]);
}
for (ll i=1;i<=n;i++)
{
for (ll j=s;j>=0;j--)
{
for (ll l : cnt[i])
{
if (j+l*w[i]>s)
break;
dp[j+l*w[i]]=max(dp[j+l*w[i]],dp[j]+l*v[i]);
}
}
}
for (ll i=0;i<=s;i++)
ans=max(ans,dp[i]);
cout << ans;
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... |