Cod sursa(job #679181)

Utilizator PatrunjelFMIAnita Liviu Patrunjel Data 12 februarie 2012 20:59:22
Problema Energii Scor 55
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.78 kb
#include<fstream>
using namespace std;

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

long long W,G,C[1002],E[1002],cost[1002][5002];
long long const inf=3200000000000;

void cit(){
    long long 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(){
    long long i,j,k;
    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;
}