In a certain sort of 01 backpack, it must be wrong to directly 01 the backpack, because the question is subject to some restrictions, you need to sort by Q-P from small to general items, as to why is it unclear?
View code
# Include <iostream>
# Include <algorithm>
Using Namespace STD;
Int DP [ 5005 ];
Struct Good
{
Int P, Q, V;
} G [ 505 ];
Bool CMP (good A, good B)
{
Return A. q-a.p <B. q-b.p;
}
Int Main ()
{
Int N, m;
While (Scanf ( " % D " , & N, & M) = 2 )
{
For ( Int I =0 ; I <n; I ++)
Scanf ( " % D " , & G [I]. P, & G [I]. Q, & G [I]. V );
Sort (G, G + N, CMP );
Memset (DP, 0 , Sizeof (DP ));
For ( Int I = 0 ; I <n; I ++)
{
For (Int J = m; j> = 0 ; J --)
{
If (J <G [I]. q | j-G [I]. P < 0 )
Continue ;
DP [J] = max (DP [J], DP [J-G [I]. p] + G [I]. V );
}
}
Int Ans = 0 ;
For ( Int I =0 ; I <= m; I ++)
If (DP [I]> ans)
Ans = DP [I];
Printf ( " % D \ n " , ANS );
}
Return 0 ;
}