Pagini recente » Cod sursa (job #1823787) | Cod sursa (job #1786757) | Cod sursa (job #2043436) | Cod sursa (job #2307288) | Cod sursa (job #1349732)
#include <fstream>
using namespace std;
ifstream f("rucsac.in");
ofstream g("rucsac.out");
int a[10001],b[10001];
int main()
{
int n,g1,g2,p,i,j;
bool k=false;
f>>n>>g1;
for(i=1;i<=n;i++)
{
f>>g2>>p;
if(k)
{
for(j=1;j<g2;j++)
b[j]=a[j];
for(j=g2;j<=g1;j++)
b[j]=max(a[j],a[j-g2]+p);
k=false;
}
else
{
for(j=1;j<g2;j++)
a[j]=b[j];
for(j=g2;j<=g1;j++)
a[j]=max(b[j],b[j-g2]+p);
k=true;
}
}
if(k)
g<<a[g1];
else
g<<b[g1];
return 0;
}