Cod sursa(job #690095)

Utilizator Slash95Vlad I. Slash95 Data 25 februarie 2012 10:48:22
Problema Energii Scor 95
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.7 kb
#include <iostream>
#include <fstream>
using namespace std;

int g, w, i, j;
int en[1001], cost[1001], costm[5001][5001];

int min(int x, int y){
	if (x<y) return x;
	else return y;}

int main (void) {
	ifstream in("energii.in");
	ofstream out("energii.out");
	in>>g;
	in>>w;
	for (i=1;i<=g;i++) {
		in>>en[i];
		in>>cost[i];}
	for (i=0;i<=g;i++)
		for (j=0;j<=w;j++) {
			costm[i][j]=5001;
			if (j==0) costm[i][j]=0;}
	for (i=1;i<=g;i++)
		for (j=1;j<=w;j++)
			if (en[i]<=j) costm[i][j]=min(costm[i-1][j],costm[i-1][j-en[i]]+cost[i]);
			else costm[i][j]=min(costm[i-1][j],cost[i]);
	if (costm[g][w]==5001) out<<"-1";
	else out<<costm[g][w];
	in.close ();
	out.close ();
    return 0;}