Pagini recente » Cod sursa (job #544133) | Cod sursa (job #2797015) | Cod sursa (job #2046955) | Cod sursa (job #1473888) | Cod sursa (job #325977)
Cod sursa(job #325977)
#include <stdio.h>
FILE *f,*s;
int g,w,x,y,i,j,e[5005];
int main()
{
f=fopen("energii.in","r");
s=fopen("energii.out","w");
fscanf(f,"%d\n%d\n",&g,&w);
e[0]=0;
for(i=1;i<=w;i++)
e[i]=-1;
for(i=1;i<=g;i++)
{
fscanf(f,"%d %d\n",&x,&y);
for(j=w;j>=0;j--)
{
if(e[j]!=-1)
{
if(j+x<w)
{
if(e[j+x]==-1||e[j+x]>y+e[j])
e[j+x]=y+e[j];
}
else
{
if(e[w]>y+e[j]||e[w]==-1)
e[w]=y+e[j];
}
}
}
}
fprintf(s,"%d",e[w]);
fclose(s);
return 0;
}