Cod sursa(job #2212184)

Utilizator ruxandramateiMatei Ruxandra ruxandramatei Data 13 iunie 2018 16:00:01
Problema Energii Scor 95
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.97 kb
#include <iostream>
#include <fstream>
#define DMAX 1000000001

using namespace std;

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

int g, energCeruta, energie[1010], cost[1010];
int castig[3][5010];

void citire(){
    in >> g >> energCeruta;
    for(int i = 1; i <= g; i++){
        in >> energie[i] >> cost[i];
    }
    in.close();
}

void intializare(){
    for(int i = 0; i <= energCeruta; i++){
        castig[1][i] = DMAX;
    }
}

void rezolvare(){
    for(int i = 1; i <= g; i++){
        for(int j = 0; j <= energCeruta; j++) {
            if(energie[i] >= j){
                castig[2][j] = min(castig[1][j], cost[i]);
            }else{
                castig[2][j] = min(castig[1][j], castig[1][j - energie[i]] + cost[i]);
            }
        }
        for(int j = 1; j <= energCeruta; j++){
            castig[1][j] = castig[2][j];
        }
    }
    out << castig[2][energCeruta];
}

int main() {
    citire();
    intializare();
    rezolvare();
    return 0;
}