Cod sursa(job #783851)

Utilizator stef1995mmarcu stefan ovidiu stef1995m Data 4 septembrie 2012 11:47:26
Problema Energii Scor 15
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.7 kb
#include<iostream>
#include<fstream>
using namespace std;
const int maxx1=10006,maxx2=10000009,inf=20000000;
int n,m,s,a,b,i,j,poz[maxx1],cost[maxx2],nr,minim=inf;
int main()
{
	freopen("energii.in","r",stdin);
	freopen("energii.out","w",stdout);
	scanf("%d %d\n",&n,&s);
	for(i=1;i<=n;i++)
	{
		scanf("%d %d\n",&a,&b);
		nr=m;
		for(j=0;j<=m;j++)
			if(cost[poz[j]+a]==0)
			{
				nr++;
				poz[nr]=poz[j]+a;
				cost[poz[j]+a]=cost[poz[j]]+b;
			}
			else
				cost[poz[j]+a]=min(cost[poz[j]+a],cost[poz[j]]+b);
		m=nr;
	}
	for(j=1;j<=m;j++)
		if(poz[j]>=s)
			if(cost[poz[j]]<minim)
				minim=cost[poz[j]];
	if(minim==inf)
		printf("-1\n");
	else
		printf("%d\n",minim);
	return 0;
}