Cod sursa(job #462579)

Utilizator crushackPopescu Silviu crushack Data 11 iunie 2010 19:09:16
Problema Energii Scor 15
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.65 kb
#include <stdio.h>
#define Gmax 1001
#define Wmax 5001

int G,W,min=-1;
bool a[Gmax][Wmax];
struct generator
{int e,c;} v[Gmax];

void citire()
{
	int i;
	freopen("energii.in","r",stdin);
	scanf("%d%d",&G,&W);
	for (i=0;i<G;i++)
		scanf("%d%d",&v[i].e,&v[i].c);
	fclose(stdin);
}

void pd(int x,int s,int c)
{
	if (s<=0)
	{
		if (c<min || min==-1)
			min=c;
		return;
	}
	if (!a[x][s] && x>=0)
		pd(x-1,s-v[x].e,c+v[x].c),
		pd(x-1,s,c),
		a[x][s]=true;
}


void scriere()
{
	freopen("energii.out","w",stdout);
	printf("%d\n",min);
	fclose(stdout);
}

int main()
{
	citire();
	pd(G-1,W,0);
	scriere();
	return 0;
}