Pagini recente » Istoria paginii utilizator/georgiana321 | Cod sursa (job #2519484) | Cod sursa (job #1113804) | Cod sursa (job #991584) | Cod sursa (job #2486468)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin ("rucsac.in");
ofstream fout ("rucsac.out");
int n, gr[5010], co[5010], gmax;
int x[10010], y[10010];
int main()
{
int i, j=0;
fin>>n>>gmax;
for(i=1; i<=n; i++)
fin>>gr[i]>>co[i];
for(j=1; j<=n; j++)
{
for(i=1; i<=gmax; i++)
{
if(j%2==1)
{
if(gr[j]<=i)
{
if(y[i-gr[j]]+co[j]>y[i])
x[i]=y[i-gr[j]]+co[j];
else
x[i]=y[i];
}
else
x[i]=y[i];
}
else
{
if(gr[j]<=i)
{
if(x[i-gr[j]]+co[j]>x[i])
y[i]=x[i-gr[j]]+co[j];
else
y[i]=x[i];
}
else
y[i]=x[i];
}
}
}
if(n%2==1) fout<<x[gmax];
else fout<<y[gmax];
fin.close();
fout.close();
return 0;
}