# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
853117 | rarex | Knapsack (NOI18_knapsack) | C++14 | 334 ms | 262144 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 = 2e6;
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.val == b.val)
return a.w < b.w;
return a.val > b.val;
}
int main()
{
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].val] < s && ct < v[i].k)
{
knap.push_back({v[i].val,v[i].w});
ct++;
mp[v[i].val] += v[i].w;
}
}
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... |