Cod sursa(job #514990)

Utilizator marinaMarina Horlescu marina Data 20 decembrie 2010 02:19:57
Problema Energii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.02 kb
//026 Energii
#include <stdio.h>
#include <string.h>
#define MAXG 1001
#define MAXW 5001
#define INPUT "energii.in"
#define OUTPUT "energii.out"
#define INF 10000001

int G, W;
int e[MAXG], c[MAXG];
int cost[MAXW], cost2[MAXW];

int cmin = INF;

int main()
{
	freopen(INPUT, "r", stdin);
	freopen(OUTPUT, "w", stdout);
	
	scanf("%d\n%d\n", &G, &W);
	int i;
	
	for(i = 1; i <= G; i++)
		scanf("%d %d", &e[i], &c[i]);


	memset(cost, -1, sizeof(cost));
	for(i = 1; i <= G; ++i)
	{
		memset(cost2, -1, sizeof(cost2));
		if(e[i] < W) 
		{
			if(cost[e[i]]==-1 || cost[e[i]] > c[i]) cost2[e[i]] = c[i];
			int j;
			for(j = 1; j < W; ++j)
				if(cost[j] >= 0)
				{
					if(cost2[j]==-1) cost2[j] = cost[j];
					if(j + e[i] < W)
					{
						if(cost[j+e[i]]==-1 || cost[j+e[i]] > cost[j]+c[i])
							cost2[j+e[i]] = cost[j] + c[i];
					}
					else if(cmin > cost[j]+c[i]) cmin = cost[j]+c[i];
				}
		}
		else if(cmin > c[i]) cmin = c[i];
		memcpy(cost, cost2, sizeof(cost2));
	}
	
	if(cmin == INF) cmin = -1;
	printf("%d\n", cmin);
	return 0;
}