Code:
#include<iostream> #include<algorithm> #include<cstdio> using namespace std; const int maxn=200000+1; int d[200+2][maxn]; int v[201],m[201],w[201]; int value[100],siz[100]; int count1=0; int main() { int V,N; cin>>N>>V; for(int i=1;i<=N;++i) cin>>v[i]>>w[i]>>m[i]; for(int i=1;i<=N;++i) { if(m[i]==-1) { for(int j=1;j<=V;++j) { d[i][j]=d[i-1][j]; } for(int j=1;j<=V;++j) if(j>=v[i])d[i][j]=max(d[i][j],d[i][j-v[i]]+w[i]); } else { count1=0; int tot=0; for(int t=1;;tot+=t,t*=2) { if(t+tot>=m[i]){ m[i]-=tot;break; } siz[++count1]=t*v[i],value[count1]=t*w[i]; } if(m[i]>0)siz[++count1]=m[i]*v[i],value[count1]=m[i]*w[i]; for(int j=1;j<=V;++j)d[i][j]=d[i-1][j]; for(int k=1;k<=count1;++k) for(int j=V;j>=1&&j>=siz[k];--j) d[i][j]=max(d[i][j],d[i][j-siz[k]]+value[k]); } } cout<<d[N][V]<<endl; return 0; }