# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
766621 | fdnfksd | Knapsack (NOI18_knapsack) | C++14 | 54 ms | 7456 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 int long long
using namespace std;
using ll = long long;
const ll maxN=2e5;
ll s,n,v[maxN],w[maxN],k[maxN];
priority_queue<pair<int,int>>pq[2006];
#define fi first
#define se second
#define pb push_back
ll dp[2006];
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> s >> n;
for(int i=1;i<=n;i++)
{
cin >> v[i] >> w[i] >> k[i];
pq[w[i]].push({v[i],k[i]});
}
vector<pair<int,int>> vec;
for(int i=1;i<=s;i++)
{
ll need=s/i;
while(pq[i].size()>0)
{
ll x=min(pq[i].top().se,need);
need-=x;
for(int j=1;j<=x;j++) vec.pb({i,pq[i].top().fi});
auto tp=pq[i].top();
pq[i].pop();
tp.se-=x;
if(tp.se>0) pq[i].push(tp);
if(need==0) break;
}
}
for(int i=0;i<vec.size();i++)
{
for(int j=s;j>=vec[i].fi;j--) dp[j]=max(dp[j],dp[j-vec[i].fi]+vec[i].se);
}
ll ans=0;
for(int j=0;j<=s;j++) ans=max(ans,dp[j]);
cout << ans;
}
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... |