Cod sursa(job #796897)

Utilizator RobertBBadea Corneliu Robert RobertB Data 12 octombrie 2012 21:28:38
Problema Energii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.72 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream f("energii.in");
ofstream g("energii.out");

int G, W;
int energie[1001], cost[1001],mat[1001][5001];

int main()
{
	int i,j;
    f>>G>>W;
    for(i = 1; i <= G; i++) 
		f>>energie[i]>>cost[i];
    for(i = 0; i < G; i++)
        for(j = 1; j <= W; j++) 
			mat[i][j] = 10001;
	for(i = 1; i <= G; i++){
        for(j = 0; j <= W; j++){
            mat[i][j] = mat[i-1][j];
            if (j < energie[i])
				mat[i][j] = min(mat[i][j], cost[i]);
            else
                mat[i][j] = min(mat[i][j], mat[i-1][j-energie[i]] + cost[i]);
        }
    }

    if(mat[G][W] != 10001) 
		g<<mat[G][W];
	else
		g<<"-1"<<"\n";

}