Cod sursa(job #428159)

Utilizator borsoszalanBorsos Zalan borsoszalan Data 28 martie 2010 22:10:48
Problema Energii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.55 kb
#include<stdio.h>
int s, x[1000000]; 
int main(){
	FILE*f=fopen("energii.in","r");
	FILE*g=fopen("energii.out","w");
	int v[1002], w[1002], s, i, j, p, nr, w1;
	fscanf(f,"%d%d",&nr,&w1);
	s=0;
	for (i=0;i<nr;i++){
		fscanf(f,"%d%d",&v[i],&w[i]);
		s+=v[i];
	}
	if (s<w1) fprintf(g,"-1\n");
	else {
	x[0]=1;
	for (i=0;i<nr;i++)
		for (j=w1;j>=0;j--)
			if (x[j]&&((w[i]+x[j]<x[j+v[i]])||(x[j+v[i]]==0)))
				x[j+v[i]]=w[i]+x[j];
	p=x[w1];
	for (i=w1+1;i<=s;i++)
		if ((x[i]<p)&&(x[i]!=0)) p=x[i];
	fprintf(g,"%d\n",p-1);
	}
	fclose(f);
	fclose(g);
	return 0;
}