Cod sursa(job #2183050)

Utilizator SqueekDanielTodasca Daniel SqueekDaniel Data 22 martie 2018 19:35:10
Problema Energii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.75 kb
#include <iostream>
#include <fstream>
#define dimn 1005
#define dimw 5005

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

int G, W;
int cant[dimn], cost[dimn];
int eg[dimw];

void citire() {
    f >> G >> W;
    for (int i=1; i<=G; i++)
        f >> cant[i] >> cost[i];
}
void rezolvare() {
    for (int i=1; i<=W; i++)
        eg[i] = 1e9;

    for (int i=1, j; i<=G; i++)
        for (j=W; j>=1; j--)
            if(j < cant[i])
                eg[j] = std::min(eg[j], cost[i]);
            else
                eg[j] = std::min(eg[j], eg[j-cant[i]] + cost[i]);


    if(eg[W] != 1e9)
        g << eg[W] << "\n";
    else
        g << "-1\n";
}

int main()
{
    citire();
    rezolvare();

    return 0;
}