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>
using namespace std;
struct element
{
int v,w,k;
};
element num[1000001],newv[1000001];
int dp[1000001],dp2[1000001];
bool fr[2001];
bool cmp(element x, element y)
{
if(x.w<y.w)
return 1;
if(y.w<x.w)
return 0;
if(x.v<y.v)
return 0;
if(y.v<x.v)
return 1;
return 0;
}
int main()
{
int s,n;
cin>>s>>n;
for(int i=0;i<n;i++)
{
cin>>num[i].v>>num[i].w>>num[i].k;
fr[num[i].w]=1;
}
sort(num,num+n,cmp);
int poz=0,cnt=0;
bool bl=0;
for(int i=0;i<n;i++)
{
if(i==0 || num[i].w!=num[i-1].w)
{
cnt=0;
bl=0;
for(int j=1;j<=num[i].k;j++)
{
if(cnt+1>s/num[i].w)
break;
cnt++;
poz++;
newv[poz]=num[i];
}
}
else
{
if(cnt+1>s/num[i].w)
continue;
for(int j=1;j<=num[i].k;j++)
{
if(cnt+1>s/num[i].w)
break;
cnt++;
poz++;
newv[poz]=num[i];
}
}
}
for(int i=1;i<=poz;i++)
{
for(int j=0;j<=s;j++)
{
if(j+newv[i].w>s)
continue;
dp2[j+newv[i].w]=max(dp2[j+newv[i].w], dp[j]+newv[i].v);
}
for(int j=1;j<=s;j++)
{
dp[j]=dp2[j];
}
}
int maxim=-1;
for(int i=1;i<=s;i++)
{
if(dp[i]>maxim)
maxim=dp[i];
}
cout<<maxim;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:34:10: warning: variable 'bl' set but not used [-Wunused-but-set-variable]
34 | bool bl=0;
| ^~
# | 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... |