Cod sursa(job #678860)

Utilizator PatrunjelFMIAnita Liviu Patrunjel Data 12 februarie 2012 14:41:51
Problema Energii Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.93 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++){
        s_max+=E[i];
        for(j=1;j<=W;j++){
            if(E[i]<=j){
                if(cost[i-1][j-E[i]]!=inf)  k=cost[i-1][j-E[i]]+C[i];
                else    k=C[i];

                if(j>s_max) cost[i][j]=k;
                else{
                    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;
}