# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
853136 | rarex | Knapsack (NOI18_knapsack) | C++14 | 84 ms | 6248 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 <iostream>
#include <algorithm>
#include <vector>
#include <unordered_map>
using namespace std;
const int nmax = 2e5;
unordered_map<int,int>mp;
vector<pair<int,int>>knap;
int dp[3][nmax];
struct str
{
int val;
int w;
int k;
}v[nmax];
bool cmp(str a, str b)
{
if(a.w == b.w)
return a.val > b.val;
return a.w > b.w;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,s,i,j;
cin >> s >> n;
for(i=1;i<=n;i++)
cin >> v[i].val >> v[i].w >> v[i].k;
sort(v+1,v+n+1,cmp);
for(i=1;i<=n;i++)
{
int ct = 0;
while(mp[v[i].w] + v[i].w <= s && ct < v[i].k)
{
knap.push_back({v[i].val,v[i].w});
ct++;
mp[v[i].w] += v[i].w;
}
}
if(knap.size() >= 30000)
while(true);
dp[0][0] = 0;
for(i = 0; i < knap.size(); i++)
{
for(j=1;j<=s;j++)
{
dp[1][j] = max(dp[1][j], dp[0][j]);
if(j >= knap[i].second)
dp[1][j] = max(dp[1][j], dp[0][j - knap[i].second] + knap[i].first);
}
for(j=1;j<=s;j++)
{
dp[0][j] = dp[1][j];
dp[1][j] = 0;
}
}
cout << dp[0][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... |