Pagini recente » Cod sursa (job #2862426) | Cod sursa (job #1952194) | Cod sursa (job #580222) | Cod sursa (job #928075) | Cod sursa (job #1795629)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
int n,g,mat[5][10005],w,p;
int main()
{
fin>>n>>g;
for(int i=1;i<=n;i++)
{
fin>>w>>p;
for(int j=1;j<=g;j++)
{
if(i%2==0)
{
if(j>=w)
mat[2][j]=max(mat[1][j],mat[1][j-w]+p);
else mat[2][j]=mat[1][j];
}
else
{
if(j>=w)
mat[1][j]=max(mat[2][j],mat[2][j-w]+p);
else mat[1][j]=mat[2][j];
}
}
}
if(n%2==0)
fout<<mat[2][g]<<"\n";
else fout<<mat[1][g]<<"\n";
return 0;
}