# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
523503 | amin | Knapsack (NOI18_knapsack) | C++14 | 10 ms | 1580 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>
using namespace std;
long long a[1000];
long w[5000],va[5000],t[5000];
long long dp[5000],d[5000];
int main()
{
long n;
long m;
cin>>m;
cin>>n;
vector<pair<long long,long long >>v[5000];
for(long i=0;i<n;i++)
{
cin>>va[i]>>w[i]>>t[i];
v[w[i]].push_back({-va[i],t[i]});
}
for(long i=0;i<=m;i++)
{
sort(v[i].begin(),v[i].end());
}
for(long i=0;i<=m;i++)
{
for(long y=0 ;y<v[i].size();y++)
{
v[i][y].first*=-1;
//cout<<v[i][y].first;
}
dp[i]=-1;
d[i]=-1;
}
dp[0]=0;
d[0]=0;
long long x=0;
long long sum=0;
for(long i=0;i<=m;i++)
{
for(long y=0;y<=m;y++)
{
dp[y]=d[y];
}
if(v[i].size()>0)
for(long y=0;y<=m;y++)
{
if(dp[y]!=-1)
{
x=0;
long long b=0;
sum=v[i][b].second;
for(long u=i;u+y<=m;u+=i)
{
x+=v[i][b].first;
d[y+u]=max(d[y+u],dp[y]+x);
sum--;
if(sum<=0)
{
b++;
if(b>=v[i].size())
break;
sum=v[i][b].second;
}
}
}
}
}
long long ans=0;
for(long i=0;i<=m;i++)
{
ans=max(ans,d[i]);
}
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... |