Cod sursa(job #826207)

Utilizator Alexandru098Costea Vlad Alexandru098 Data 30 noiembrie 2012 14:05:53
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.6 kb
#include<stdio.h>
#define max(x,y) ((x)>(y)?(x):(y))

struct loer
{
	int w,p;
};

int n,g,d[2][10005],curent,last,w,p,j,i;
loer v[5005];

int main()
{
	freopen("rucsac.in","r",stdin);
	freopen("rucsac.out","w",stdout);
	scanf("%d%d",&n,&g);
	for(i=1;i<=n;i++)
	{
		scanf("%d%d",&w,&p);
		v[i].w=w;
		v[i].p=p;
	}
	for(i=1;i<=n;i++)
	{
		curent=i%2;
		last=(i-1)%2;
		for(j=1;j<=g;j++)
		{
			if(v[i].w<=j)
			{
			d[curent][j]=max(d[last][j],d[last][j-v[i].w]+v[i].p);
			}
			else
			{
				d[curent][j]=d[last][j];
			}
		}
	}
	printf("%d",d[curent][g]);
	return 0;
}