Cod sursa(job #457937)

Utilizator crushackPopescu Silviu crushack Data 22 mai 2010 11:44:33
Problema Energii Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.74 kb
#include <stdio.h>
#define lung 1001

int m[lung][lung*5],n;
int cst[lung][lung*5];
struct gen
{int e,c;} a[lung];
int rucsac(int,int);

int main()
{
	int e,i,s;s=0;
	freopen("energii.in","r",stdin);
	freopen("energii.out","w",stdout);
	scanf("%d%d",&n,&e);
	for (i=0;i<n;i++)
		scanf("%d%d",&a[i].e,&a[i].c),s+=a[i].e;
	printf("%d\n",rucsac(e,s));
	return 0;
}

int rucsac(int e,int l)
{
	int i,j;
	for (i=1;i<n;i++)
		for (j=1;j<=l;j++)
			if (a[i].c<j)
			{
				m[i][j]= (m[i-1][j]>m[i-1][j-a[i].c]+a[i].e) ? m[i-1][j] : m[i-1][j-a[i].c]+a[i].e;
				cst[i][j]=a[i].c;
				cst[i][j]+= (m[i-1][j]>m[i-1][j-a[i].c]+a[i].e) ? cst[i-1][j] : cst[i-1][j-a[i].c];
				if (m[i][j]>=e)
					return cst[i][j];
			}
	return -1;
}