Pagini recente » Rating Rus Gabriela (Gabriela94) | Cod sursa (job #2681590) | Cod sursa (job #18569) | Cod sursa (job #1002336)
#include <fstream>
using namespace std;
ifstream f("rucsac.in");
ofstream g("rucsac.out");
int short gmax,pmax,i,nr,gg[1000000],pp[1000000],h,hp,hg,n;
int main()
{
f>>n>>gmax;pmax=0;nr=0;
for(h=1;h<=n;++h)
{
f>>hg>>hp;
for(i=nr;i>=1;--i)
if(gg[i]+hg<=gmax && gg[i]>0) {gg[++nr]=gg[i]+hg;pp[nr]=pp[i]+hp;}
gg[++nr]=hg;
pp[nr]=hp;
}
for(i=1;i<=nr;++i) if(pp[i]>pmax) pmax=pp[i];
g<<pmax<<'\n';
return 0;
}