Cod sursa(job #679043)

Utilizator PatrunjelFMIAnita Liviu Patrunjel Data 12 februarie 2012 18:12:49
Problema Energii Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.76 kb
#include<fstream>
using namespace std;

ifstream fin("energii.in");
ofstream fout("energii.out");

int W,G,C[1002],E[1002],cost[1002][5002];
int const inf=32000000;

void cit(){
    int i;
    fin>>G>>W;
    for(i=1;i<=G;i++){
        fin>>E[i]>>C[i];
        if(E[i]>W)  E[i]=W;
    }

    for(i=1;i<=W;i++)   cost[0][i]=inf;
}

void calc(){
    int i,j,k,s_max=0;
    for(i=1;i<=G;i++){
        for(j=1;j<=W;j++){
            k=cost[i-1][j-E[i]]+C[i];
            if(k<inf){
                if(k<cost[i-1][j])  cost[i][j]=k;
                else    cost[i][j]=cost[i-1][j];
            }
            else    cost[i][j]=cost[i-1][j];
        }
    }
}

int main(){
    cit();
    calc();
    if(cost[G][W] != inf) fout<<cost[G][W]<<endl;
    else    fout<<-1;
    return 0;
}