Cod sursa(job #441524)

Utilizator Teodor94Teodor Plop Teodor94 Data 12 aprilie 2010 22:51:17
Problema Energii Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.53 kb
#include<cstdio>

const int N=1<<13;

int v[N];

int main()
{
	freopen("energii.in","r",stdin);
	freopen("energii.out","w",stdout);
	int n,w;
	scanf("%d%d",&n,&w);
	for (int i=1;i<=w;i++)
		v[i]=-1;
	for (int i=1;i<=n;i++)
	{
		int e,c;
		scanf("%d%d",&e,&c);
		for (int j=w;j>=e+1;j--)
			if (v[j-e]!=-1)
			{
				if (v[j]!=-1)
					v[j]=v[j-e]+c;
				else
				if (v[j-e]+c<v[j])
					v[j]=v[j-e]+c;
			}
		for (int j=e;j>0;j--)
			if (v[j]==-1 || c<v[j])
				v[j]=c;
	}
	printf("%d\n",v[w]);
	return 0;
}